Rakesh
Rakesh

Reputation: 365

JPA Hibernate Find by ID & Version

The find method in JPA only accepts primary key, is there a way to include the Version property to find the entity.

Upvotes: 2

Views: 2073

Answers (1)

Alex Barnes
Alex Barnes

Reputation: 7218

Why would you want to use the version property to find an entity? If you're using a surrogate primary key field (as recommended by Hibernate) you should be able to identify a row by the id alone.

The Version field is used by hibernate to ensure that a loaded entity is not changed between loading and persisting.

If you really wanted to use the id and version you would need a query:

    Query query = session.createQuery("select from MyTable where id = ? and version =?");
    query.setParameter(0, myId);
    query.setParameter(1, myVersion);
    query.uniqueResult();

Upvotes: 2

Related Questions