Hibernate + Spring + JSF:Criteria.List()返回空

时间:2012-09-29 04:22:25

标签: spring hibernate java-ee

当MBean调用以下getAll方法时,此方法返回空。

@Repository("bookRepository")
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public class BookRepositoryImpl implements BookRepository {

@Autowired
private SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
public List<Book> getAll() {

    return getSession().createCriteria(Book.class).list();
}

private Session getSession() {
    return sessionFactory.getCurrentSession();
}

}

图书课程:

@Entity
@Table(name = "book")
public class Book implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 2581522135048868175L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "title", unique = true, nullable = false)
    private String title;
    @Column(name = "author", nullable = false)
    private String author;
    @Column(name = "isbn_code", unique = true, nullable = false, length = 10)
    private String isbnCode;
    @Column(name = "year")
    private Integer year;
    @Column(name = "price")
    private Double price;


    // Constructors

// Getters and Setters

}

应用程序上下文:

<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" 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.0.xsd 
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:annotation-config /> <!-- bean definitions go here -->
<context:component-scan base-package="br.com.myproject.faces">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<context:component-scan base-package="br.com.myproject.service.impl">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Service" />
</context:component-scan>

<context:component-scan base-package="br.com.myproject.domain.model">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Repository" />
</context:component-scan>

<!-- HIBERNATE configuration INIT -->

<context:property-placeholder location="classpath:hibernate.properties" />

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>org.camelcode.model</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <!-- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> -->
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
</bean>

<!-- HIBERNATE configuration END -->

Ps:我在book表中插入记录,所有注入的实例都没问题。

1 个答案:

答案 0 :(得分:1)

你的hibernate-config.xml

中有这个条目吗?
<property name="packagesToScan">
            <list>
                <value>Here you need to mention package name where your book class resides</value>
            </list>
        </property>

或者需要提及下面的映射类

<mapping class="package.Book"/>

您是否遗漏了这些条目?