无法从db获取实体

时间:2014-12-01 23:11:01

标签: hibernate criteria spring-4

我有一个postgree db。而春天+冬眠。当我不按任何标准获得实体时,它会返回0长度列表。看起来某些依赖项被遗漏,或者像实体一样未被识别为实体。

我的maven配置:     ...

   <spring.version>4.0.3.RELEASE</spring.version>
   <hibernate.version>3.6.9.Final</hibernate.version>
....   
<!-- Spring framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!--Spring and Transactions-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Spring ORM support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901.jdbc4</version>
    </dependency>

我的春季配置

<context:component-scan base-package="com.helper.api,com.helper.db.model"/>

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

<bean id="hibernate3AnnotatedSessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="eyeDataSource" />
    <property name="annotatedPackages">
        <list>
            <value>com.helper.db.model</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.url">jdbc:postgresql://localhost:5432/xxxxxxxxx_db</prop>
            <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
            <prop key="hibernate.connection.username">xxxxxxxxx</prop>
            <prop key="hibernate.connection.password">qwerty123</prop>
        </props>
    </property>
</bean>

问题是

session.createSQLQuery("SELECT * FROM account").list()

这是返回正确的数据。我在表中有1条记录。那是归来的 但这很简单的SQL。当我将其改为相同的标准时

session.createCriteria(AccountEntity.class).list()

它没有任何回报。 AccountEntity:

@Entity
@Table(name = "account", schema = "public", catalog = "XXXXXXXX_db")
public class AccountEntity {
    private int mId;
    private String mName;
    private String mPassword;
    private Collection<AccountInProductEntity> mAccountInProductsById;
    private Collection<ProductEntity> mProductsById;

    @Id
    @Column(name = "id", nullable = false, insertable = true, updatable = true)
    public int getId() {
        return mId;
    }

    public void setId(int id) {
        mId = id;
    }

    @Basic
    @Column(name = "name", nullable = false, insertable = true, updatable = true, length = 250)
    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    @Basic
    @Column(name = "password", nullable = false, insertable = true, updatable = true, length = 50)
    public String getPassword() {
        return mPassword;
    }

    public void setPassword(String password) {
        mPassword = password;
    }

    @Override
    public boolean equals(Object o) {
       ....
    }

    @Override
    public int hashCode() {
        ....
    }

    @OneToMany(mappedBy = "accountByAccountId")
    public Collection<AccountInProductEntity> getAccountInProductsById() {
        return mAccountInProductsById;
    }

    public void setAccountInProductsById(Collection<AccountInProductEntity> accountInProductsById) {
        mAccountInProductsById = accountInProductsById;
    }

    @OneToMany(mappedBy = "accountByOwnerId")
    public Collection<ProductEntity> getProductsById() {
        return mProductsById;
    }

    public void setProductsById(Collection<ProductEntity> productsById) {
        mProductsById = productsById;
    }
}

1 个答案:

答案 0 :(得分:0)

我的错误是在spring config中

   <property name="annotatedPackages">
       <list>
          <value>com.tac.helper.db.model</value>
        </list>
   </property>

而不是

    <property name="packagesToScan">
        <list>
            <value>com.tac.helper.db.model</value>
        </list>
    </property>