我有一些Hibernate / JPA注释(但实际上并不知道区别)允许我创建一个关联类。此类将两个与一个对象相关的项组合在一起。我最初使用@JoinTable
,但意识到我需要更多关于元数据的元数据,因此必须将代码转换为另一种对象类型。
目前我使用@Id
标记我的对象的ID
列,并使用@NaturalId (mutable = false)
标记String uuid
。
我的关联类正在使用@ManyToOne
并且创建表很好,但是当我查看它时,表使用@Id
字段作为映射列。我更希望这个关联类使用@NaturalId uuid
来轻松地将关系/关联转移到其他系统。
如何让关系使用正确的标识符?
作为参考,我的数据库和Java代码如下所示:
AssociationsTable
----------------------------------------------
| ID | META DATA | ID ASSOC. 1 | ID ASSOC. 2 |
----------------------------------------------
| 1 | stuff | 1 | 2 |
----------------------------------------------
Objects
------------------------------
| ID | META DATA | UUID |
------------------------------
| 1 | stuff | FOO-123 |
------------------------------
| 2 | stuff | BAR-456 |
------------------------------
Association Class{
ObjA main;
ObjA sub;
@ManyToOne
getMain()
@ManyToOne
getSub()
}
ObjA Class{
Long id;
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
@XmlElement
@Column(name = "ID", unique = true, nullable = false)
getId()
String uuid;
@NaturalId (mutable = false)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "uuid", unique = true)
getUUID()
}
答案 0 :(得分:3)
您可以使用referencedColumnName
的{{1}}属性,如上所述here。因此,对于您的示例,映射应如下所示:
<强> ObjA.java:强>
@JoinColumn
<强> Association.java:强>
package hello;
import org.hibernate.annotations.NaturalId;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class ObjA {
Long id;
String uuid;
public ObjA() {
}
public ObjA(String uuid) {
this.uuid = uuid;
}
@Id
@GeneratedValue
@Column(name = "obj_a_id", unique = true, nullable = false)
Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NaturalId(mutable = false)
@Column(name = "uuid", unique = true, nullable = false)
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
已成功测试这些映射,以存储package hello;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Association {
Long id;
ObjA main;
ObjA sub;
public Association() {
}
public Association(ObjA main, ObjA sub) {
this.main = main;
this.sub = sub;
}
@Id
@GeneratedValue
@Column(name = "association_id", unique = true, nullable = false)
Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name = "main_uuid", referencedColumnName = "uuid", nullable = false)
public ObjA getMain() {
return main;
}
public void setMain(ObjA main) {
this.main = main;
}
@ManyToOne
@JoinColumn(name = "sub_uuid", referencedColumnName = "uuid", nullable = false)
public ObjA getSub() {
return sub;
}
public void setSub(ObjA sub) {
this.sub = sub;
}
}
表的uuid
和obja
列中main_uuid
表的sub_uuid
列的值。这是针对PostgreSQL 9.4.4使用association
,org.hibernate.common:hibernate-commons-annotations:4.0.5.Final
,org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final
和org.hibernate:hibernate-core:4.3.10.Final
进行测试的。