JPA何时设置@GeneratedValue @Id [英] When does the JPA set a @GeneratedValue @Id

查看:226
本文介绍了JPA何时设置@GeneratedValue @Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JPA实体,它使用生成的 long ID作为主键:

I have a simple JPA entity that uses a generated long "ID" as its primary key:

@Entity
public class Player {
   private long id;

   protected Player() {
     // Do nothing; id defaults to 0L
   }


   @GeneratedValue
   @Id
   public long getId() {
      return id;
   }

   protected void setId(final long id) {
      this.id = id;
   }
   // Other code
}

在某些时候在这种类型的对象的生命周期中,JPA必须调用 setId()来记录生成的ID值。我的问题是,发生这种情况时,哪里有说明这个的文档。我查看了JPA规范并找不到明确的声明。

At some point in the life-cycle of an object of this type the JPA must call setId() to record the generated ID value. My question is, when does this happen, and where is the documentation that states this. I've looked through the JPA Specification and can not find a clear statement.

JPA规范说明(重点补充):

The JPA Specification says (emphasis added):


托管实体实例是具有持久性标识的实例,当前与持久性上下文相关联。

A managed entity instance is an instance with a persistent identity that is currently associated with a persistence context.

是否试图说该对象必须托管以使其 @Id 显着?
EntityManager.persist()的文档说(强调添加)它使实例托管和持久,所以这样做意味着该方法设置了 @Id ?或者直到你打电话给 EntityTransaction.commit()

Is that trying to say that the the object must be managed to have its @Id significant? The documentation for EntityManager.persist() says (emphasis added) it makes "an instance managed and persistent", so does that mean that the @Id is set by that method? Or is it not until you call EntityTransaction.commit()?

@时对于不同的JPA提供程序,Id 的设置可能不同,也许可能针对不同的生成策略。但是最安全的(可移植的,符合规范的)假设是什么,你可以在生命周期的最早点设置它?

When the @Id is set might be different for different JPA providers, and perhaps for different generation strategies. But what is the safest (portable, specification conforming) assumption that you can make about the earliest point in the lifecycle that it has been set?

推荐答案

调用.persist()不会自动设置id值。您的JPA提供程序将确保在最终将实体写入db之前设置它。因此,您可以假设在提交事务时将分配id。但这不是唯一可能的情况。当你打电话.flush()时也会发生同样的情况。

calling .persist() will not automatically set the id value. Your JPA provider will ensure that it is set before the entity is finally written to db. So you are right to assume that the id will be assigned when the transaction is committed. But this is not the only possible case. When you call .flush() the very same will happen.

托马斯

更新:关注Geek的请评论。 - >如果使用GenerationType.Identity,则在将实体写入db之前,提供程序不会设置id。在这种情况下,ID生成在db级别的插入过程中发生。无论如何,JPA提供者将确保事后更新实体,并且生成的id将在@Id带注释的属性中可用。

Update: Pay attention to Geek's comment, please. -> If GenerationType.Identity is used, the id will not be set by the provider before the entity is written to db. In this case id generation happens during the insert process on db level. Anyway, the JPA provider will ensure that the entity is updated afterwards and the generated id will be available in the @Id annotated property.

这篇关于JPA何时设置@GeneratedValue @Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆