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,并与当前会话关联,否则将引发异常.但我认为这是合乎逻辑的.