具有Hibernate Envers的复合表

时间:2015-01-21 17:12:00

标签: java hibernate jpa hibernate-envers

我有一个带有复合表的应用程序,其中包含一个额外的列。一切正常,直到我们添加Hibernate Envers(@Audited)。

  

org.hibernate.MappingException:无法在no.pack.response.ResponseDomainCode中读取responseDomainCodes的mapped属性

我很乐意在必要时提供更详细的信息,但是,目前我不确定相关的内容。

这些表看起来像这样,是一个非常标准的复合键表,有一个额外的列。

数据库架构

+-----------+---------+
|   CODE    |  TYPE   |
+-----------+---------+
| category  | VARCHAR |
| code      | VARCHAR |
+-----------+---------+
            |
            |
+----------------------+---------+
| RESPONSE_DOMAIN_CODE |  TYPE   |
+----------------------+---------+
| response_domain_id   | KEY     |
| code_id              | KEY     |
| rank                 | VARCHAR |
+----------------------+---------+
            |
            |
+--------------------+------+
|  RESPONSE_DOMAIN   | TYPE |
+--------------------+------+
| response_domain_id | PK   |
| response_kind_id   | FK   |
+--------------------+------+

ResponseDomain.java

@Entity
@Table(name = "responseDomain")
public class ResponseDomain implements Serializable {

    @Id
    @Column(name = "responseDomain_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

   @ManyToOne
   @JoinColumn(name = "respons_kind_id")
   private ResponseKind responseKind;

   @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.responseDomain",  cascade = CascadeType.ALL)
    private Set<ResponseDomainCode> responseDomainCodes = new HashSet<>();

 //Omitted rest.
}

Code.java

@Entity
@Table(name = "code")
public class Code implements Serializable {

    @Id
    @Column(name = "code_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String category;

    private  String code;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.code", cascade = CascadeType.ALL)
    private Set<ResponseDomainCode> responseDomainCodes = new HashSet<>();

    //Omitted rest
}

ResponseDomainCode.java

@Entity
@Table(name = "responseDomain_code")
@AssociationOverrides(value = {
        @AssociationOverride(name = "pk.responseDomain",
                joinColumns = @JoinColumn(name = "responseDomain_id")),
        @AssociationOverride(name = "pk.code",
                joinColumns = @JoinColumn(name = "code_id"))
})
public class ResponseDomainCode implements Serializable {

    @EmbeddedId
    private ResponseDomainCodeId pk = new ResponseDomainCodeId();

    @Column(name = "rank")
    private String rank;

    public ResponseDomainCodeId getPk() {
        return pk;
    }

    public void setPk(ResponseDomainCodeId pk) {
        this.pk = pk;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    @Transient
    public ResponseDomain getResponseDomain() {
        return getPk().getResponseDomain();
    }

    public void setResponseDomain(ResponseDomain responseDomain) {
        this.getPk().setResponseDomain(responseDomain);
    }

    @Transient
    public Code getCode() {
        return getPk().getCode();
    }

    public void setCode(Code code) {
        this.getPk().setCode(code);
    }

    //Omitted rest
}

ResponseDomainCodeId.java

@Embeddable
public class ResponseDomainCodeId implements Serializable {

    @ManyToOne
    private ResponseDomain responseDomain;

    @ManyToOne
    private Code code;

    public ResponseDomainCodeId() {
    }

    public ResponseDomain getResponseDomain() {
        return responseDomain;
    }

    public void setResponseDomain(ResponseDomain responseDomain) {
        this.responseDomain = responseDomain;
    }

    public Code getCode() {
        return code;
    }

    public void setCode(Code code) {
        this.code = code;
    }

    //Omitted rest
}

1 个答案:

答案 0 :(得分:3)

在@adamw的帮助下,我设法通过改变我的映射来解决这个问题。

不是使用复合键,而是生成了具有自己唯一ID的表。

+----------------------+------------+
| RESPONSE_DOMAIN_CODE |    TYPE    |
+----------------------+------------+
| id                   | PK(BIGINT) |
| response_domain_id   | BIGINT     |
| code_id              | BIGINT     |
| rank                 | VARCHAR    |
+----------------------+------------+

现在我没有使用@Embeddable和@EmbeddedId,而是在任何一方都有@ManyToOne和@OneToMany注释,并根据ResponseDomain进行查询。

这样就可以在这样的关系中启用使用Hibernate Envers的完整版审计控制。

我希望这会对某些人有所帮助。