【问题标题】:Hibernate criteria returns empty list but data exists. Using annotationHibernate 条件返回空列表,但数据存在。使用注释
【发布时间】:2013-11-27 20:29:30
【问题描述】:

我不明白为什么这个查询没有给我任何结果。我知道数据存在于表中。 执行查询时,列表变量“results”为空。我是否正确实现了复合键?

我什至尝试使用@EmbeddedId 来完成此操作,但返回的列表仍然是空的。

        Session sess = HibernateUtil.getSession();
        Criteria criteria = sess.createCriteria(Employee.class);
        criteria.add(Restrictions.eq("employeeId", 255847208));
        criteria.add(Restrictions.eq("serialId", 461));
        List<Employee> results = criteria.list();

主键类

public class EmpPrmryKey implements Serializable {
        private Integer employeeId;
        private Integer serialId;
        //getters and setters
}

POJO 映射到表:

@Entity
@IdClass(EmpPrmryKey.class)
@Table(name = "EMPLOYEE")
public class Employee{

    private EmpPrmryKey compositeId;

    @Id
    @Column(name = "employee_id")
    private Integer employeeId; 

    @Id
    @Column(name = "serial_id")
    private Integer serialId;

    //getters and setters
}

【问题讨论】:

    标签: java database hibernate annotations


    【解决方案1】:

    对于那些仍在寻找这个问题的答案的人,请检查您的休眠配置 xml 并确保在那里声明您的休眠实体。

    【讨论】:

      【解决方案2】:

      我刚刚遇到了几乎相同的问题,而我的问题是在 applicationContext.xml 中我为扫描 sessionFactory 指定了错误的包名称。有趣的是,Eclipse 没有向我显示任何异常和错误,因此调试起来非常困难:

      <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
          <property name="dataSource" ref="dataSource" />
          <property name="packagesToScan" value="{{Wrong Package Name}}" />
      </bean>
      

      【讨论】:

        【解决方案3】:

        首先,我猜你在 Employee 类中不需要private EmpPrmryKey compositeId;

        我创建了具有相同实体的本地项目,并且您提供的相同查询有效(它返回了预期结果)。因此,如果结果仍然为空,那么您可以检查您是否正在查找正确的表或数据库。

        【讨论】:

          【解决方案4】:

          添加到@nikita;s 的答案,请检查 sessionFactory bean id (applicationContext.xml) 中名称为 annotatedClasses 的属性。 Tt 会是这样的:

          <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="annotatedClasses">
              <list>
                <value>com.your.package.Employee</value>
              </list>
            </property>
          </bean>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-06-05
            • 2012-10-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多