2012-09-29 52 views
0

當MBean調用以下getAll方法時,此方法返回空。Hibernate + Spring + JSF:Criteria.List()返回空

@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(); 
} 

}

Book類:

@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表中插入記錄,並注入所有實例都OK。

回答

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"/> 

是否缺少這些條目?

+0

我添加了屬性並看到這個錯誤:「元素類型」屬性「的內容必須匹配」null「。」。添加標籤映射後,不會發生錯誤,但不起作用。我的xml在src/main/resources/META-INF(我正在使用maven)。謝謝。 – vctlzac

+0

它應該與此一起工作。請在pastebin中添加您的代碼,並讓我知道,以便我們可以進一步調查 –

+0

+1剛解決我的問題,謝謝!忘記這一點,我感到有點愚蠢! –