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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4120414/
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-10-30 04:50:47  来源:igfitidea点击:

How can I use generated value within composite keys?

javahibernateormannotationscomposite-key

提问by Nick

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?

我有两个类 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;
 } 
}

回答by Pascal Thivent

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

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

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

据我所知,至少在标准 JPA 和 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.

8.4. 组件作为复合标识符

...

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

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

但是在实践中情况可能会有所不同(请参阅HHH-2060和/或此线程以获取将 CompositeUserType 与 IdentifierGenerator 一起使用的替代方法)。

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

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

2.2.3.2.4. Partial identifier generation

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

...

You can also generate properties inside an @EmbeddedIdclass.

2.2.3.2.4. 部分标识符生成

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

...

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

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

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

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

虽然我没有任何实际经验。

References

参考

回答by Michail Nikolaev

It is possible to declared your own generatorfor @EmbeddedIdto use sequence for Id generation.

可以为@EmbeddedId声明您自己的生成器以使用序列来生成 Id。

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

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