简体   繁体   English

JPA获取实体对象的id

[英]JPA get id of entity object

Does anyone know how I can do the equivalent of this in hibernate: 有谁知道我怎么能在休眠中做相同的事情:

session.getIdentifier(instance);

with JPA? 与JPA?

EntityManager has a contains method but that's pretty much it! EntityManager有一个contains方法,但就是这样!

I'm writing some code that acts as a transformer between entities and data stored in a session (so rather than a serialized object being stored just the class name and the id is stored). 我正在编写一些代码,它们充当实体和存储在会话中的数据之间的转换器(因此,而不是仅存储类名并且存储id的序列化对象)。

在JPA 2.0中,您可以编写

em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(instance);

Does anyone know how I can do the equivalent of this in hibernate (...) with JPA? 有没有人知道我如何用JPA在hibernate(...)中做相同的操作?

JPA 1.0 doesn't have an equivalent so if you're stuck with JPA 1.0, you'll have to use Hibernate's API: obtain the Session from the EntityManager and use Session#getIdentitifier(Object) . JPA 1.0没有等效的,所以如果你坚持使用JPA 1.0,你将不得不使用Hibernate的API:从EntityManager获取Session并使用Session#getIdentitifier(Object)

For example, with JBoss (yes, getDelegate() is not portable ): 例如,使用JBoss(是的, getDelegate() 不可移植 ):

org.hibernate.Session session = (Session)manager.getDelegate();
session.getIdentifier(myEntity);

If you are using JPA 2.0, then use PersistenceUnitUtil#getIdentity(Object) as suggested by axtavt . 如果您使用的是JPA 2.0,则按照axtavt的建议使用PersistenceUnitUtil#getIdentity(Object) But that's not available in JPA 1.0. 但这在JPA 1.0中不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM