问题描述
我有一个Catagory表,带有复合主键和外键。我的表结构就像
CREATE TABLE CATAGORY(
CATAGORY_ID int(11)NOT NULL,
CATAGORY_NAME varchar(50)DEFAULT NULL,
VERSION int(11)NOT NULL,
PARENT_ID int(11)DEFAULT NULL,
PRIMARY KEY(CATAGORY_ID,VERSION)
);
ALTER TABLE CATAGORY ADD CONSTRAINT FOREIGN KEY(PARENT_ID,VERSION)REFERENCES CATAGORY(CATAGORY_ID,VERSION);
我想要一个像这样的Catagory类的ORM映射
public class Catagory {
@EmbeddedId
private CatagoryPk pk;
@Column(name =CATAGORY_NAME)
private String catagoryName;
私人类别parentId;
私人清单< Catagory>孩子;
@Embeddable
public class CatagoryPk {
@Column(name =VERSION)
private Integer version;
@Column(name =CATEGORY_ID)
private Integer catagoryId;
}
现在如何注释
私人类别parentId;
私人列表< Catagory>孩子;
@JoinColumns ({
@JoinColumn(name =VERSION,referencedColumnName =VERSION),
@JoinColumn(name =PARENT_ID,referencedColumnName =CATAGORY_ID)
})
@ManyToOne
私人类别parentId;
@OneToMany(mappedBy =parentId)
私人列表< Catagory>孩子的;
或带有 @IdClass
的版本,而不是 @EmbeddableId
(用Eclipselink 2.2.1测试):
pre $ public class CatagoryPk实现Serializable {
私有Integer版本;
private Integer catagoryId;
$ b $ public CatagoryPk(){
}
public CatagoryPk(Integer version,Integer catagoryId){
this.version = version;
this.catagoryId = catagoryId;
@Entity
@IdClass(CatagoryPk.class)
public class Catagory {
@Column(name =VERSION )
@Id私人整数版本;
@Column(name =CATAGORY_ID)
@Id private Integer catagoryId;
$ b @ManyToOne
@JoinColumns({
@JoinColumn(name =VERSION,referencedColumnName =VERSION,insertable = false,updatable = false),
@JoinColumn(name =PARENT_ID,referencedColumnName =CATAGORY_ID)
})
private类别parentId;
}
I have a Catagory table, with composite primary key and foreign key. My table structure is like
CREATE TABLE CATAGORY (
CATAGORY_ID int(11) NOT NULL,
CATAGORY_NAME varchar(50) DEFAULT NULL,
VERSION int(11) NOT NULL,
PARENT_ID int(11) DEFAULT NULL,
PRIMARY KEY (CATAGORY_ID,VERSION)
);
ALTER TABLE CATAGORY ADD CONSTRAINT FOREIGN KEY (PARENT_ID, VERSION) REFERENCES CATAGORY (CATAGORY_ID, VERSION);
and I want a ORM mapping with Catagory class like this
public class Catagory {
@EmbeddedId
private CatagoryPk pk ;
@Column(name="CATAGORY_NAME")
private String catagoryName;
private Catagory parentId;
private List<Catagory> childs ;
}
@Embeddable
public class CatagoryPk {
@Column(name="VERSION")
private Integer version;
@Column(name="CATEGORY_ID")
private Integer catagoryId;
}
Now how to annotate
private Catagory parentId;
private List<Catagory> childs ;
@JoinColumns({
@JoinColumn(name="VERSION", referencedColumnName="VERSION"),
@JoinColumn(name="PARENT_ID", referencedColumnName="CATAGORY_ID")
})
@ManyToOne
private Catagory parentId;
@OneToMany(mappedBy="parentId")
private List<Catagory> childs;
or version with @IdClass
instead of @EmbeddableId
(tested with Eclipselink 2.2.1):
public class CatagoryPk implements Serializable {
private Integer version;
private Integer catagoryId;
public CatagoryPk() {
}
public CatagoryPk(Integer version, Integer catagoryId) {
this.version = version;
this.catagoryId = catagoryId;
}
}
@Entity
@IdClass(CatagoryPk.class)
public class Catagory {
@Column(name="VERSION")
@Id private Integer version;
@Column(name="CATAGORY_ID")
@Id private Integer catagoryId;
@ManyToOne
@JoinColumns({
@JoinColumn(name="VERSION", referencedColumnName="VERSION", insertable=false, updatable=false),
@JoinColumn(name="PARENT_ID", referencedColumnName="CATAGORY_ID")
})
private Catagory parentId;
}
这篇关于休眠复合外键和单个表中的主键的一对一映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!