我怎样才能使用组合键内产生价值? [英] How can I use generated value within composite keys?
问题描述
我有两个类documentlog和documentversion(与主键:INT DOC_ID和int docVersionID)与多到一的关系。我使用了一种名为CompundKey复合键类来管理的复合主键。我需要自动增量docversionID,但我不能这样做。能否请你帮我在这方面?
@Entity
@Table(NAME =Documentversion,模式=DocumentManagement)
公共类DocumentVersion实现Serializable { 私人CompoundKey ID;
私人列表< DocumentLog> documentLog; @OneToMany(的mappedBy =documentVersion,targetEntity = DocumentLog.class,
级联= {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
公开名单< DocumentLog> getDocumentLog(){
返回documentLog;
}
公共无效setDocumentLog(列表< DocumentLog> documentLog){
this.documentLog = documentLog;
} @EmbeddedId
@AttributeOverride(NAME =DOC_ID,列= @列(名称=DOC_ID))
公共CompoundKey的getId(){
返回ID;
}
公共无效SETID(CompoundKey ID){
this.id = ID;
}
}
该文档是关于该主题...
有点混乱据我所知,组合键总是要的分配的应用程序(即非生成的)至少与标准JPA也是Hibernate的核心:
8.4.组件作为联合标识符
...
您不能使用一个IdentifierGenerator
产生组合关键字。代替
应用程序必须分配它自己
标识符。
块引用>但事情可能会在不同的做法(见 HHH-2060 和/或位使用使用CompositeUserType有一个IdentifierGenerator一起替代这个线程)。
现在,最令人困惑的部分,从Hibernate注解3.5文档:
2.2.3.2.4。部分标识符生成
Hibernate支持自动
代的部分标识符的
属性。只需使用
@GeneratedValue
上一个注释或
几个ID属性。
...
您还可以生成一个
@EmbeddedId
类中的属性。
块引用>(和也请阅读反对使用此功能从Hibernate团队的警告)。
我没有任何与它的实际经验,但。
参考
- Hibernate的核心参考指南
- Hibernate注解3.5参考指南
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...
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
- Hibernate Core Reference Guide
- Hibernate Annotations 3.5 Reference Guide
这篇关于我怎样才能使用组合键内产生价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!