如何在组合键中使用生成的值?
我有两个具有多对一关系的类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文档在这个主题上有点令人困惑...
据我所知,组合键总是必须由应用程序分配(即非生成),至少使用标准 JPA,但也包括 Hibernate Core:
但实际情况可能会有所不同(请参阅 HHH-2060 和/或此线程作为使用 CompositeUserType 和 IdentifierGenerator 的替代方案)。
现在,最令人困惑的部分来自 Hibernate Annotations 3.5 文档:
(另请阅读 Hibernate 团队针对使用此功能的警告)。
不过我没有任何实践经验。
参考资料
8.4。作为复合标识符的组件
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:
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:
(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
可以声明您自己的 生成器 用于 @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.