如何在复合键中使用生成的值? [英] How can I use generated value within composite keys?

查看:27
本文介绍了如何在复合键中使用生成的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有多对一关系的类 documentlog 和 documentversion(主键:int doc_id 和 int docVersionID).我使用了一个名为 CompundKey 的复合键类来管理复合主键.我需要自动增加 docversionID 但我不能这样做.你能帮我解决这方面的问题吗?

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;
 } 
}

推荐答案

关于这个主题的文档有点混乱...

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

据我所知,至少在标准 JPA 和 Hibernate Core 中,复合键总是必须由应用程序分配(即非生成):

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:

...

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

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

但在实践中情况可能会有所不同(请参阅 HHH-2060 和/或 this thread 用于替代使用 CompositeUserType 和 IdentifierGenerator).

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).

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

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

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

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.

...

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

You can also generate properties inside an @EmbeddedId class.

(并且还请阅读 Hibernate 团队反对使用此功能的警告).

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

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

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

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