一对一的关系 如(CrmCustomer 和CrmReasearch,CrmIntendInfo是一对一的关系
分别是客户,客户调查和客户意向调查
public
class
CrmCustomer
implements
java.io.Serializable
{
private
CrmCustResearch crmCustResearch;
private
CrmIntendInfo crmIntendInfo;
@OneToOne(cascade
=
{}
, fetch
=
FetchType.LAZY)
@PrimaryKeyJoinColumn
public
CrmCustResearch getCrmCustResearch()
{
return
this
.crmCustResearch;
}
public
void
setCrmCustResearch(CrmCustResearch crmCustResearch)
{
this
.crmCustResearch
=
crmCustResearch;
}
@OneToOne(cascade
=
{}
, fetch
=
FetchType.LAZY)
@PrimaryKeyJoinColumn
public
CrmIntendInfo getCrmIntendInfo()
{
return
this
.crmIntendInfo;
}
public
void
setCrmIntendInfo(CrmIntendInfo crmIntendInfo)
{
this
.crmIntendInfo
=
crmIntendInfo;
}
}
在CrmResearch 和CrmIntendInfo 进行相应的设置
public class CrmCustResearch implements java.io.Serializable {
private long custId;
public CrmCustResearch(long custId, String epId) {
this.custId = custId;
this.epId = epId;
}
}
在数据库里面custId 设置为外键 指向CrmCustomer的custId
另外一种一对一关系的类型,不是通过相同的键来连接 ,而是通过直接设置一对一关系来连接
public class CrmCommInfoType implements java.io.Serializable {
// 一对一关系
private long commTypeId;
private CrmCommonInfo crmCommonInfo;
@OneToOne(cascade = {}, fetch = FetchType.LAZY, mappedBy = " crmCommInfoType " ,optional = true )
// mappedBy=crmCommInfoType,是和这个表一对一的CrmCommInfo 里面的关联字段
public CrmCommonInfo getCrmCommonInfo() {
return this .crmCommonInfo;
}
}
public class CrmCommonInfo implements java.io.Serializable {
private long crmCommonInfoId;
private CrmCommInfoType crmCommInfoType;//mappedby 的crmCommInfoType
@OneToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "COMM_TYPE_ID",referencedColumnName = "COMM_TYPE_ID",unique = true)//要连接的栏的名字,数据库里面的字段
public CrmCommInfoType getCrmCommInfoType() {
return this.crmCommInfoType;
}
public void setCrmCommInfoType(CrmCommInfoType crmCommInfoType) {
this.crmCommInfoType = crmCommInfoType;
}
}
还要在数据库里面设置外间约束(如在MySql 下面要设置的Sql如下)
ALTER TABLE CRM_COMMON_INFO CONSTRAINT FKF8882C3D003D0B1 FOREIGH KEY(COMM_TYPE_ID) REFERENCES CRM_COMM_INFO_TYPE(COMM_TYPE_ID)
posted on 2006-09-23 11:11
doodoosun 阅读(202)
评论(0) 编辑 收藏