hibernate子表继承父表,实体之间如何关联
附件表cgform_uploadfiles(数据库中的字段有id,name两个字段)对应hibernate实体类CgUploadEntity
附件父表(和其他附件表公共字段而抽出来的字段)t_s_attachment(数据库中的字段有id,userId两个字段)对应hibernate实体类TSAttachment
例子:让cgform_uploadfiles表继承t_s_attachment表,意思是说在保存cgform_uploadfiles子表的数据时,同时在t_s_attachment父表中也插入一条记录,两表之间通过id关联(两表的id相同)。
1.TSAttachment实体
@Entity
@Table(name =
"t_s_attachment"
)
@Inheritance(strategy = InheritanceType.JOINED)
public
class
TSAttachment
implements
java.io.Serializable {
private
String
id;
private
Integer userId;
@Id
@GeneratedValue(generator =
"paymentableGenerator"
)
@GenericGenerator(name =
"paymentableGenerator"
, strategy =
"uuid"
)
@Column(name =
"ID"
,nullable=
false
,length=
32
)
public
String
getId() {
return
id;
}
public
void
setId(
String
id) {
this
.id = id;
}
@Column(name =
"ID"
)
public
Integer getUserId() {
return
userId;
}
public
void
setUserId(Integer userId) {
this
.userId = userId;
}
}
2.cgform_uploadfiles实体
@Entity
@Table
(name =
"cgform_uploadfiles"
, schema =
""
)
@PrimaryKeyJoinColumn
(name =
"id"
)
@SuppressWarnings
(
"serial"
)
public
class
CgUploadEntity
extends
TSAttachment
implements
java.io.Serializable{
private
String name;
@Column
(name =
"name"
)
public
String getName() {
return
name;
}
public
void
setCgformId(String name) {
this
.name = name;
}
}
总结:在保存TSAttachment实体对象的时候,上面两个实体的id值一样,都是都uuid自动生成。这很像一对一关联,但不同的是在CgUploadEntity实体对象中可以直接对TSAttachment实体对象的属性进行赋值和获取,很方便!
本文地址:http://yayihouse.com/yayishuwu/chapter/1036