复合主键:
public class PKClass implements Serializable
{
private static final long serialVersionUID = -2092390836751020965L;
public PKClass(){}
public PKClass(long actId, long actAttId){
this.actvId = actId;
this.actvAtId = actAttId;
}
private long actvId;
private long actvAtId;
// getters, setters, equals, hashcode
}
实体类1:
@Entity @Table(name = "Table1")
@IdClass(PKClass.class)
public class AtLinkData implements Serializable
{
@Id @Column(name = "ACT_ID", nullable = false)
private long actvId;
@Id @Column(name = "ACT_AT_ID")
private long actvAtId;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, , mappedBy = "atLinkData")
@JoinColumn(name = "ACT_AT_ID")
private AtData atData;
// getters, setters
}
实体类2:
@Entity @Table(name = "Table2")
public class AtData implements Serializable
{
@TableGenerator(name = "somename", table = "sometable", pkColumnName = "somecolumn", valueColumnName = "valuename", pkColumnValue = "Table2", allocationSize = 1)
@Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "somename")
@Column(name = "ACT_AT_ID")
private Long id;
// other variables
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ACT_AT_ID")
private AtLinkData atLinkData;
// getters setters
}
我一直在尝试不同的东西,但是上面的代码,我收到了错误:
Caused by: org.hibernate.MappingException: broken column mapping for: atLinkData.id of: com.test.database.AtData
答案 0 :(得分:4)
@OneToOne(的mappedBy = “活动促销”)
使用JoinColumns
注释而不是JoinColumn
。
@OneToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "ACTVID", referencedColumnName = "ACTVID"),
@JoinColumn(name = "ACTVATID", referencedColumnName = "ACTVATID")
})
private AtLinkData atLinkData;
AtData
实体的数据库表,您必须添加两个外键列ACTVID
和ACTVATID
。
<强>更新强>
如果您无法在Table 2
中添加新列,请在mappedBy
实体使用AtData
属性,而不是AtLinkData
。请尝试以下。
AtLinkData.java
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "ACT_AT_ID")
private AtData atData;
AtData.java
@OneToOne(fetch = FetchType.LAZY, mappedBy="atData")
private AtLinkData atLinkData;
答案 1 :(得分:0)
以下是我解决它的方法:
AtLinkData.java
@Id
@GeneratedValue(generator = "foreignGenerator")
@org.hibernate.annotations.GenericGenerator(name = "foreignGenerator", strategy = "foreign", parameters = @Parameter(name = "property", value = "atData"))
@Column(name = "ACT_AT_ID")
private long actvAtId;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "ACT_AT_ID",insertable=false,updatable=false)
private AtData atData;
// getters, setters
AtData.java
@OneToOne(fetch = FetchType.LAZY, mappedBy="atData")
private AtLinkData atLinkData;
感谢@CycDemo的帮助。