0

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

追伸:本のテーブルにレコードを挿入しましたが、挿入されたすべてのインスタンスは問題ありません。

4

1 に答える 1

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

これらのエントリがありませんか?

于 2012-09-29T05:55:12.860 に答える