Hibernate criteria returns an empty list, but data exists. Using annotations

I cannot understand why this query does not give me any result. I know that data exists in a table. The variable " results" is empty when the request is executed. Am I implementing the composite key correctly?

I even tried using @EmbeddedIdto do this, but the returned list is still empty.

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

Primary Key Class

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

POJO mapped to table:

@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
}
+4
source share
4 answers

For those who are still looking for the answer to this question, like me, please check your xibernate config xml and make sure your hibernate object is declared there.

+1

, , 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>
+1

, , private EmpPrmryKey compositeId; Employee.

, , ( ). , , , .

0

By adding @nikita to the answer, please check the property named annotatedClasses in the sessionFactory bean file (applicationContext.xml). Tt will be something like this:

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

All Articles