5

I can't figure out why this query isn't giving me any result. I know the data exists in the table. The list variable "results" is empty when the query is executed. Am I implementing the composite key correctly?

I even tried using the @EmbeddedId to get this done but the returned list was 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 Answers 4

3

For those who are still looking for an answer to this question as I was, Please check your hibernate config xml and make sure your hibernate entity is declared there.

Sign up to request clarification or add additional context in comments.

Comments

1

I just had almost identical problem and my issue was that in applicationContext.xml I had a wrong package name specified for scanning for sessionFactory. Interestingly, Eclipse did not show me any exceptions, errors, so to debug it was extremely hard:

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

Comments

0

First of all, I guess you don't need private EmpPrmryKey compositeId; in Employee class.

I created local project with the same entities and the same query you provided worked (it returned expected result). So, If result still empty then you may check if you are looking correct table or database.

Comments

0

Adding to @nikita;s answer, please check for the property with name annotatedClasses in the sessionFactory bean id (applicationContext.xml). Tt would 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.