Composite table 与 Hibernate Envers
Composite table with Hibernate Envers
我有一个应用程序,其复合 table 包含一个额外的列。一切正常,直到我们添加 Hibernate Envers (@Audited)。
org.hibernate.MappingException: Unable to read the mapped by attribute for responseDomainCodes in no.pack.response.ResponseDomainCode
如有必要,我很乐意提供更详细的信息,但是,目前我不确定哪些信息是相关的。
tables 看起来像这样,是一个非常标准的组合键 table,多了一个列。
数据库架构
+-----------+---------+
| 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
}
响应域Code.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
}
在@adamw 的帮助下,我通过更改映射设法解决了这个问题。
生成了具有自己唯一 ID 的 table,而不是使用复合键。
+----------------------+------------+
| RESPONSE_DOMAIN_CODE | TYPE |
+----------------------+------------+
| id | PK(BIGINT) |
| response_domain_id | BIGINT |
| code_id | BIGINT |
| rank | VARCHAR |
+----------------------+------------+
现在我不再使用@Embeddable 和@EmbeddedId,而是在两侧使用@ManyToOne 和@OneToMany 注释,并基于ResponseDomain 进行查询。
这也可以在这样的关系中使用 Hibernate Envers 启用完整版本审计控制。
我希望这对某些人有所帮助。
我有一个应用程序,其复合 table 包含一个额外的列。一切正常,直到我们添加 Hibernate Envers (@Audited)。
org.hibernate.MappingException: Unable to read the mapped by attribute for responseDomainCodes in no.pack.response.ResponseDomainCode
如有必要,我很乐意提供更详细的信息,但是,目前我不确定哪些信息是相关的。
tables 看起来像这样,是一个非常标准的组合键 table,多了一个列。
数据库架构
+-----------+---------+
| 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
}
响应域Code.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
}
在@adamw 的帮助下,我通过更改映射设法解决了这个问题。
生成了具有自己唯一 ID 的 table,而不是使用复合键。
+----------------------+------------+
| RESPONSE_DOMAIN_CODE | TYPE |
+----------------------+------------+
| id | PK(BIGINT) |
| response_domain_id | BIGINT |
| code_id | BIGINT |
| rank | VARCHAR |
+----------------------+------------+
现在我不再使用@Embeddable 和@EmbeddedId,而是在两侧使用@ManyToOne 和@OneToMany 注释,并基于ResponseDomain 进行查询。
这也可以在这样的关系中使用 Hibernate Envers 启用完整版本审计控制。
我希望这对某些人有所帮助。