Java 如何获取任何 JPA 实体的主键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3328813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:12:28  来源:igfitidea点击:

How to get the primary key of any JPA entity?

javaeclipselinkjpa-2.0

提问by simpatico

For every @Entity I need to perform the following:

对于每个@Entity,我需要执行以下操作:

public <Entity> boolean insert(final Entity entity){
    if (em.find(entity.getClass(), entity.getId()) == null) {
        et.begin();
        em.persist(entity);
        et.commit();
        return true;
    }
    return false;
}

That is persist the entity if it didn't exist, and know if it did or not exist. With Entity I'm trying to reach @Entity, although I realize it's not an inheritance relationship. What class can I use to refer to every JPA entity? I could just create an interface/abstract class MyEntities and have all of them inherit, but is that the way? I'm hoping for less code. Also, I'd expect to be able to extract the primary key of every entity, as I attempt in .getId().

如果实体不存在,则持久化该实体,并知道它是否存在。使用 Entity 我试图联系@Entity,尽管我意识到这不是继承关系。我可以使用什么类来引用每个 JPA 实体?我可以只创建一个接口/抽象类 MyEntities 并让它们都继承,但是这样吗?我希望代码更少。此外,我希望能够提取每个实体的主键,就像我在 .getId() 中尝试的那样。

采纳答案by Gordon Yorke

This functionality was added in JPA 2.0. Simply call:

此功能是在 JPA 2.0 中添加的。只需调用:

Object id = entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity);

回答by Mike Q

The common interface approach is what I use and it works fine. Using pure JPA I don't see a way of getting the identifier.

通用接口方法是我使用的,并且工作正常。使用纯 JPA 我看不到获取标识符的方法。

Have a look at merge(). I've not used it much myself but I think

看看merge()。我自己用的不多,但我认为

Hibernate has ways of doing this

Hibernate 有办法做到这一点

Serializable id = session.getIdentifier(entity);

But this is not JPA standard.

但这不是 JPA 标准。

回答by Roman

Read an article about Generic Dao approach.

阅读一篇关于Generic Dao 方法的文章。

I don't clearly understand your problem, but if you want to get entity id - just get it. Its available after persist method is complete i.e.

我不太清楚您的问题,但是如果您想获得实体 ID - 只需获得它。它在persist方法完成后可用,即

em.persist(entity);
et.commit();
int id = entity.getId()

I usually make a class AbstractEntitywith field idand its accessors and inherit all my other entities from this class.

我通常AbstractEntity使用字段id及其访问器创建一个类,并从该类继承所有其他实体。

The only problem with this approach is that if you'll need to make any of your entities Serializable, you'll have to make AbstractEntityserializable i.e. all other entities will become serializable. Otherwise field idwill not be serialized in the entity which should be serializable.

这种方法的唯一问题是,如果您需要使任何实体可序列化,则必须使AbstractEntity所有其他实体可序列化,即所有其他实体都将变为可序列化。否则字段id将不会在应该可序列化的实体中被序列化。

回答by ngCoder

Another way of fetching entity ID is:

另一种获取实体 ID 的方法是:

Session session = entityManager.unwrap(Session.class);
Object entityId = session.getId(entity);

This approach uses extractPrimaryKeyFromObject()method from ObjectBuilderclass.

此方法使用ObjectBuilder类中的extractPrimaryKeyFromObject()方法。