2012-05-27 3 views
0

У меня есть один springapp-dao.xml, который выглядит, как показано нижекак настроить спящий режим 4 с пружиной 3,1

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd"> 

    <jee:jndi-lookup id="masterDS" jndi-name="java:jboss/datasources/MySqlDS" /> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="masterDS" /> 
     <property name="hibernateProperties"> 
     <props> 
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
     <prop key="hibernate.show_sql">true</prop> 
     </props> 
</property> 
    </bean> 
</beans> 

Тогда я один springapp-service.xml, который, как следовать

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd "> 
    <bean id="txManager" 
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="masterDS" /> 
    </bean> 
    <tx:advice id="txAdvice" transaction-manager="txManager"> 
    <tx:attributes> 
     <tx:method name="get*" read-only="true" propagation="REQUIRED"/> 
     <tx:method name="*" propagation="REQUIRED"/> 
    </tx:attributes> 
</tx:advice> 
    <aop:config> 
     <aop:pointcut expression="execution(* service.*.*(..))" 
      id="service" /> 
     <aop:advisor advice-ref="txAdvice" pointcut-ref="service" /> 
    </aop:config> 
    <bean id="contactDAO" class="dao.ContactDAOImpl"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
    <bean id="contactService" class="service.ContactServiceImpl"> 
    <property name="contactDAO" ref="contactDAO"/> 
    </bean> 
</beans> 

Тогда у меня есть один DAOImpl

@Repository("ContactDAO") 
@Transactional(readOnly = false) 
public class ContactDAOImpl implements ContactDAO { 
private SessionFactory sessionFactory; 

    public SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public void setSessionFactory(SessionFactory sessionFactory) { 
     this.sessionFactory = sessionFactory; 
    } 



public List<Contacts> listContact() { 

      return sessionFactory.openSession().createQuery("from Contacts") 
        .list(); 
     } 

} 

Тогда у меня есть один POJO, который правильно отображается в таблице Контакты

@Entity 
@Table(name = "CONTACTS") 
public class Contacts { 

    @Id 
    @Column(name = "ID") 
    @GeneratedValue 
    // The @GeneratedValue annotation says that this value will be determined by 
    // the datasource, not by the code. 
    private Integer id; 

    @Column(name = "FIRSTNAME") 
    private String firstname; 

    @Column(name = "LASTNAME") 
    private String lastname; 

    @Column(name = "EMAIL") 
    private String email; 

    @Column(name = "TELEPHONE") 
    private String telephone; 
    @Column(name = "CREATED") 
    private String created; 

........

Тогда у меня есть один класс обслуживания

@Service 
public class ContactServiceImpl implements ContactService { 


    private ContactDAO contactDAO; 

    public ContactDAO getContactDAO() { 
     return contactDAO; 
    } 

    public void setContactDAO(ContactDAO contactDAO) { 
     this.contactDAO = contactDAO; 
    } 

................... .

Теперь у меня есть 2 вопроса Во-первых, когда я пытался авторизовать сессионную фабрику в ContactsDAOImpl и ContactDAO в ContactServiceImpl, он вообще не подключался. Затем я использовал сеттер и геттеры, как вы можете видеть выше. Затем он начал вводить требуемые зависимости. Теперь моя проблема, когда я listContact() метод ContactDAOImpl (во-первых, я должен был назвать открытое соединение на заводе вместо GetConnection но этого Безразлично работы), она бросает следующий exceptiopn

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: Contacts is not mapped [from Contacts] 

ли я нужен создать HBM FIL

ответ

2

вы должны сказать LocalSessionFactoryBean, где вы положили свои классы, так что любой пакет ваши контакты класс находится в:

<property name="packagesToScan" value="your.orm.package"/>