微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 读取Hibernate实体标识值

Hibernate是否有用于读取实体身份字段值的API?在我的例子中,这将是由@Id注释的字段返回的Serializable值.例如,假设我有一个Person实体:

class Person {
    @Id private long id;
    // ... other fields,getters/setters,etc ...
}

Person p = new Person();
p.setId(42L);

Hibernate.unkNownFunction(p); // returns 42L

当然,我可以阅读注释以自己找到@Id字段,但这似乎可能是内置的东西.

最佳答案
session.getIdentifier(object)

Return the identifier value of the given entity as associated with this session. An exception is thrown if the given entity instance is transient or detached in relation to this session.

该对象需要具有ID,并与当前会话关联,否则将引发异常.但我认为这是合乎逻辑的.

原文地址:https://www.jb51.cc/java/437764.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐