开发者

JPA Hibernate Find by ID & Version

开发者 https://www.devze.com 2023-03-07 15:20 出处:网络
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 y

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

精彩评论

暂无评论...
验证码 换一张
取 消