6

我有一些 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()
}
4

1 回答 1

5

您可以使用 的referencedColumnName属性,如此@JoinColumn所述。因此,对于您的示例,映射应如下所示:

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

协会.java:

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_uuidsub_uuid列中association。这是使用org.hibernate.common:hibernate-commons-annotations:4.0.5.Finalorg.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Finalorg.hibernate:hibernate-core:4.3.10.Finalorg.hibernate:hibernate-entitymanager:4.3.10.Final针对 PostgreSQL 9.4.4 进行的测试。

于 2015-08-17T16:57:03.963 回答