SneakyMummin
SneakyMummin

Reputation: 711

Hibernate, search by primary key returns all from table

I have this issue with Hibernate that when i try to retrieve unique result using criteria hibernate returns all the content from the table.

Session session = HibernateUtil.beginTransaction();
Customer c = new Customer();
c.setCustId(custId);
Example ex = Example.create(c);
Criteria criteria = HibernateUtil.getSession().createCriteria(Customer.class);
criteria.add(ex);

Customer customer = (Customer)criteria.uniqueResult();

HibernateUtil.commitTransaction();
HibernateUtil.closeSession();

However querying the table with:

Customer customer = (Customer)session
                    .createSQLQuery("select * from customer_ where custid = :id")
                    .addEntity(Customer.class)
                    .setInteger("id", custId)
                    .uniqueResult();

returns correct entry. custId is the table's primary key. And the Customer class contains 2 @OneToMany mappings.

Do I need to add something to the criteria example above??

Upvotes: 1

Views: 862

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

The documentation says:

Version properties, identifiers and associations are ignored.

(emphasis mine)

Why not simply using Session.get() if you have the identifier?

Upvotes: 2

Related Questions