[英]JPA : Re-use Id generated from a sequence for a new version of the same object having composite PK (ID + VERSION)
I have entity A with composite PK [ id(generated from sequence) + version ] .我有复合 PK [id(从序列生成) + 版本]的实体A。
For a brand new record I want to pick the id from a sequence defined in the DB side.对于一个全新的记录,我想从数据库端定义的序列中选择 id。 Lets say its created like below
可以说它的创建如下
ID VERSION
1 0
Next time, I want a new version of the same Id to be created like below下一次,我想创建一个相同 ID 的新版本,如下所示
ID VERSION
1 0
1 1
Note: in the second case I don't want it to be generated by the sequence generator, coz I want to manually provide it.注意:在第二种情况下,我不希望它由序列生成器生成,因为我想手动提供它。
Is it possible in JPA/Hibernate?在 JPA/Hibernate 中可以吗? If possible could someone please tell how to do it?
如果可能的话,有人可以告诉我该怎么做吗?
Many thanks in advance!提前谢谢了!
Hibernate ORM doesn't support the generation of id with composite keys . Hibernate ORM 不支持使用复合键生成 id 。
You can probably run a native SQL when you create a new object.当您创建新的 object 时,您可能可以运行本机 SQL。 With PostgreSQL for example:
以 PostgreSQL 为例:
Long id = (Long) em.createNativeQuery("SELECT nextval('mysequence')").getSingleResult();
Long version = ...;
EmbeddedId id = new EmbeddedId(id, version);
Where EmebeddedId is the composite key of your entity:其中 EmbeddedId 是您的实体的复合键:
@Entity
class Example {
@Id
EmbeddedId id;
...
}
@Embeddable
class EventId implements Serializable {
Long id;
Long version;
...
}
Where mysequence
is the name of a sequence on the database.其中
mysequence
是数据库中序列的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.