如何在组合键中使用生成的值?

发布于 2024-09-30 21:52:26 字数 905 浏览 1 评论 0原文

我有两个具有多对一关系的类 documentlog 和 documentversion(带有主键:int doc_id 和 int docVersionID)。我使用了一个名为 CompundKey 的复合键类来管理复合主键。我需要自动递增 docversionID 但我无法做到这一点。您能在这方面帮助我吗?

@Entity
@Table(name = "Documentversion", schema = "DocumentManagement")
public class DocumentVersion implements Serializable { 

 private CompoundKey id;
 private List<DocumentLog> documentLog;

 @OneToMany(mappedBy="documentVersion", targetEntity=DocumentLog.class,  
   cascade ={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
 public List<DocumentLog> getDocumentLog() {
  return documentLog;
 }
 public void setDocumentLog(List<DocumentLog> documentLog) {
  this.documentLog = documentLog;
 }

 @EmbeddedId 
 @AttributeOverride(name="doc_Id", column=@Column(name="doc_Id") )
 public CompoundKey getId() {
  return id;
 }
 public void setId(CompoundKey id) {
  this.id = id;
 } 
}

I have two classes documentlog and documentversion(with primary keys: int doc_id and int docVersionID) with a many-to-one relationship. I used a composite key class called CompundKey to manage the compound primary key. I need to auto increment docversionID but I am not able to do that. Could you please help me in this regard?

@Entity
@Table(name = "Documentversion", schema = "DocumentManagement")
public class DocumentVersion implements Serializable { 

 private CompoundKey id;
 private List<DocumentLog> documentLog;

 @OneToMany(mappedBy="documentVersion", targetEntity=DocumentLog.class,  
   cascade ={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
 public List<DocumentLog> getDocumentLog() {
  return documentLog;
 }
 public void setDocumentLog(List<DocumentLog> documentLog) {
  this.documentLog = documentLog;
 }

 @EmbeddedId 
 @AttributeOverride(name="doc_Id", column=@Column(name="doc_Id") )
 public CompoundKey getId() {
  return id;
 }
 public void setId(CompoundKey id) {
  this.id = id;
 } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里寻她 2024-10-07 21:52:26

文档在这个主题上有点令人困惑...

据我所知,组合键总是必须由应用程序分配(即非生成),至少使用标准 JPA,但也包括 Hibernate Core:

8.4。作为复合标识符的组件

...

您不能使用 IdentifierGenerator
生成复合密钥。反而
应用程序必须分配自己的
标识符。

但实际情况可能会有所不同(请参阅 HHH-2060 和/或此线程作为使用 CompositeUserType 和 IdentifierGenerator 的替代方案)。

现在,最令人困惑的部分来自 Hibernate Annotations 3.5 文档:

2.2.3.2.4。部分标识符生成

Hibernate支持自动
一些标识符的生成
特性。只需使用
一个或多个上的 @GeneratedValue 注释
几个 id 属性。

...

您还可以在 @EmbeddedId 类中生成属性。

(另请阅读 Hibernate 团队针对使用此功能的警告)。

不过我没有任何实践经验。

参考资料

The documentation is a bit confusing on this topic...

To my knowledge, composite keys always had to be assigned by the application (i.e. non generated) at least with standard JPA but also Hibernate Core:

8.4. Components as composite identifiers

...

You cannot use an IdentifierGenerator
to generate composite keys. Instead
the application must assign its own
identifiers.

But things might be a bit different in practice (see HHH-2060 and/or this thread for an alternative using a CompositeUserType together with an IdentifierGenerator).

Now, the most confusing part, from the Hibernate Annotations 3.5 documentation:

2.2.3.2.4. Partial identifier generation

Hibernate supports the automatic
generation of some of the identifier
properties. Simply use the
@GeneratedValue annotation on one or
several id properties.

...

You can also generate properties inside an @EmbeddedId class.

(and please also read the warning from the Hibernate Team against using this feature).

I don't have any practical experience with it though.

References

长梦不多时 2024-10-07 21:52:26

可以声明您自己的 生成器 用于 @EmbeddedId 使用 ID 序列一代。

另外,您需要声明 假实体 自动创建序列。

It is possible to declared your own generator for @EmbeddedId to use sequence for Id generation.

Also, you'll need to declare fake entity to create sequence automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文