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 f开发者_开发技巧ind the entity.
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();
精彩评论