0

我有一个 springapp-dao.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: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 中自动连接会话工厂和在 ContactServiceImpl 中自动连接 ContactDAO 时,它根本没有连接。然后我使用了 setter 和 getter,如上所示。然后它开始注入所需的依赖项。现在我的问题是当我是 ContactDAOImpl 的 listContact() 方法时,(首先我必须在工厂调用 open connection 而不是 getConnection 但这不起作用),它会抛出以下异常

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

我需要创建 hbm 文件吗

4

1 回答 1

2

你需要告诉 LocalSessionFactoryBean 你把你的类放在哪里,所以无论你的 Contacts 类在什么包中:

<property name="packagesToScan" value="your.orm.package"/>
于 2012-11-27T23:28:40.613 回答