java hibernate使用注解来定义联合主键
java hibernate使用注解来定义联合主键
下面使用hibernate的API中说明的三种方式来定义主键,主要使用Annotation来定义hibernate中的联合主键
下面取至hibernate的API文档:
定义组合主键的几种语法:
1、将组件类注解为@Embeddable,并将组件的属性注解为@Id
2、将组件的属性注解为@EmbeddedId
3、将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id
下面就分别使用这三种方式来定义联合主键。
建表的SQL语句:
CREATETABLE`syslogs`( `id`varchar(50)NOTNULL, `yhid`varchar(50)NOTNULL, `modelname`varchar(100)DEFAULTNULL, `content`varchar(500)DEFAULTNULL, `inserttime`varchar(20)DEFAULTNULL, `remark`varchar(50)DEFAULTNULL, PRIMARYKEY(`id`,`yhid`) )ENGINE=InnoDBDEFAULTCHARSET=utf-8;
一、将组件类注解为@Embeddable
/**
*SysLogsDtoId代表主键类
*/
packagecom.hibernate.dto;
importjavax.persistence.Embeddable;
/**
*1、主键类必须要实现java.io.Serializable接口
*2、主键类必须要重写equals和hashCode方法
*@authoribm
*/
@Embeddable
publicclassSysLogsDtoIdimplementsjava.io.Serializable{
privatestaticfinallongserialVersionUID=1L;
privateStringid;
privateStringyhid;
publicSysLogsDtoId(){
}
publicSysLogsDtoId(Stringid,Stringyhid){
this.id=id;
this.yhid=yhid;
}
publicStringgetId(){
returnthis.id;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetYhid(){
returnthis.yhid;
}
publicvoidsetYhid(Stringyhid){
this.yhid=yhid;
}
publicbooleanequals(Objectother){
if((this==other))
returntrue;
if((other==null))
returnfalse;
if(!(otherinstanceofSysLogsDtoId))
returnfalse;
SysLogsDtoIdcastOther=(SysLogsDtoId)other;
return((this.getId()==castOther.getId())||(this.getId()!=null&&castOther.getId()!=null&&this.getId().equals(castOther.getId())))
&&((this.getYhid()==castOther.getYhid())||(this.getYhid()!=null&&castOther.getYhid()!=null&&this.getYhid().equals(
castOther.getYhid())));
}
publicinthashCode(){
intresult=17;
result=37*result+(getId()==null?0:this.getId().hashCode());
result=37*result+(getYhid()==null?0:this.getYhid().hashCode());
returnresult;
}
}
/**
*SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId
*/
packagecom.hibernate.dto;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.Id;
importjavax.persistence.Table;
@Entity
@Table(name="syslogs")
publicclassSysLogsDtoimplementsjava.io.Serializable{
privatestaticfinallongserialVersionUID=1L;
privateSysLogsDtoIdid;
privateStringmodelname;
privateStringcontent;
privateStringinserttime;
privateStringremark;
publicSysLogsDto(){
}
publicSysLogsDto(SysLogsDtoIdid){
this.id=id;
}
publicSysLogsDto(SysLogsDtoIdid,Stringmodelname,Stringcontent,Stringinserttime,Stringremark){
this.id=id;
this.modelname=modelname;
this.content=content;
this.inserttime=inserttime;
this.remark=remark;
}
@Id
publicSysLogsDtoIdgetId(){
returnthis.id;
}
publicvoidsetId(SysLogsDtoIdid){
this.id=id;
}
@Column(name="modelname",length=100)
publicStringgetModelname(){
returnthis.modelname;
}
publicvoidsetModelname(Stringmodelname){
this.modelname=modelname;
}
@Column(name="content",length=500)
publicStringgetContent(){
returnthis.content;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
@Column(name="inserttime",length=20)
publicStringgetInserttime(){
returnthis.inserttime;
}
publicvoidsetInserttime(Stringinserttime){
this.inserttime=inserttime;
}
@Column(name="remark",length=50)
publicStringgetRemark(){
returnthis.remark;
}
publicvoidsetRemark(Stringremark){
this.remark=remark;
}
}
二、将组件的属性注解为@EmbeddedId
这种情况最简单,主键类只用定义主键字段,不需要写任何注解。然后在对象类中在主键类的get方法上加上@EmbeddedId注解。
/**
*SysLogsDtoId代表主键类
*/
packagecom.hibernate.dto;
publicclassSysLogsDtoIdimplementsjava.io.Serializable{
privatestaticfinallongserialVersionUID=1L;
privateStringid;
privateStringyhid;
publicSysLogsDtoId(){
}
publicSysLogsDtoId(Stringid,Stringyhid){
this.id=id;
this.yhid=yhid;
}
publicStringgetId(){
returnthis.id;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetYhid(){
returnthis.yhid;
}
publicvoidsetYhid(Stringyhid){
this.yhid=yhid;
}
publicbooleanequals(Objectother){
if((this==other))
returntrue;
if((other==null))
returnfalse;
if(!(otherinstanceofSysLogsDtoId))
returnfalse;
SysLogsDtoIdcastOther=(SysLogsDtoId)other;
return((this.getId()==castOther.getId())||(this.getId()!=null&&castOther.getId()!=null&&this.getId().equals(castOther.getId())))
&&((this.getYhid()==castOther.getYhid())||(this.getYhid()!=null&&castOther.getYhid()!=null&&this.getYhid().equals(
castOther.getYhid())));
}
publicinthashCode(){
intresult=17;
result=37*result+(getId()==null?0:this.getId().hashCode());
result=37*result+(getYhid()==null?0:this.getYhid().hashCode());
returnresult;
}
}
/**
*SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId
*/
packagecom.hibernate.dto;
importjavax.persistence.Column;
importjavax.persistence.EmbeddedId;
importjavax.persistence.Entity;
importjavax.persistence.Table;
@Entity
@Table(name="syslogs")
publicclassSysLogsDtoimplementsjava.io.Serializable{
privatestaticfinallongserialVersionUID=1L;
privateSysLogsDtoIdid;
privateStringmodelname;
privateStringcontent;
privateStringinserttime;
privateStringremark;
publicSysLogsDto(){
}
publicSysLogsDto(SysLogsDtoIdid){
this.id=id;
}
publicSysLogsDto(SysLogsDtoIdid,Stringmodelname,Stringcontent,Stringinserttime,Stringremark){
this.id=id;
this.modelname=modelname;
this.content=content;
this.inserttime=inserttime;
this.remark=remark;
}
@EmbeddedId
publicSysLogsDtoIdgetId(){
returnthis.id;
}
publicvoidsetId(SysLogsDtoIdid){
this.id=id;
}
@Column(name="modelname",length=100)
publicStringgetModelname(){
returnthis.modelname;
}
publicvoidsetModelname(Stringmodelname){
this.modelname=modelname;
}
@Column(name="content",length=500)
publicStringgetContent(){
returnthis.content;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
@Column(name="inserttime",length=20)
publicStringgetInserttime(){
returnthis.inserttime;
}
publicvoidsetInserttime(Stringinserttime){
this.inserttime=inserttime;
}
@Column(name="remark",length=50)
publicStringgetRemark(){
returnthis.remark;
}
publicvoidsetRemark(Stringremark){
this.remark=remark;
}
}
三、将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id
/**
*SysLogsDtoId代表主键类
*/
packagecom.hibernate.dto;
publicclassSysLogsDtoIdimplementsjava.io.Serializable{
privatestaticfinallongserialVersionUID=1L;
privateStringid;
privateStringyhid;
publicSysLogsDtoId(){
}
publicSysLogsDtoId(Stringid,Stringyhid){
this.id=id;
this.yhid=yhid;
}
publicStringgetId(){
returnthis.id;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetYhid(){
returnthis.yhid;
}
publicvoidsetYhid(Stringyhid){
this.yhid=yhid;
}
publicbooleanequals(Objectother){
if((this==other))
returntrue;
if((other==null))
returnfalse;
if(!(otherinstanceofSysLogsDtoId))
returnfalse;
SysLogsDtoIdcastOther=(SysLogsDtoId)other;
return((this.getId()==castOther.getId())||(this.getId()!=null&&castOther.getId()!=null&&this.getId().equals(castOther.getId())))
&&((this.getYhid()==castOther.getYhid())||(this.getYhid()!=null&&castOther.getYhid()!=null&&this.getYhid().equals(
castOther.getYhid())));
}
publicinthashCode(){
intresult=17;
result=37*result+(getId()==null?0:this.getId().hashCode());
result=37*result+(getYhid()==null?0:this.getYhid().hashCode());
returnresult;
}
}
/**
*SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId
*/
packagecom.hibernate.dto;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.Id;
importjavax.persistence.IdClass;
importjavax.persistence.Table;
@Entity
@Table(name="syslogs")
@IdClass(value=SysLogsDtoId.class)
publicclassSysLogsDtoimplementsjava.io.Serializable{
privatestaticfinallongserialVersionUID=1L;
privateStringid;
privateStringyhid;
privateStringmodelname;
privateStringcontent;
privateStringinserttime;
privateStringremark;
publicSysLogsDto(){
}
@Id
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
@Id
publicStringgetYhid(){
returnyhid;
}
publicvoidsetYhid(Stringyhid){
this.yhid=yhid;
}
@Column(name="modelname",length=100)
publicStringgetModelname(){
returnthis.modelname;
}
publicvoidsetModelname(Stringmodelname){
this.modelname=modelname;
}
@Column(name="content",length=500)
publicStringgetContent(){
returnthis.content;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
@Column(name="inserttime",length=20)
publicStringgetInserttime(){
returnthis.inserttime;
}
publicvoidsetInserttime(Stringinserttime){
this.inserttime=inserttime;
}
@Column(name="remark",length=50)
publicStringgetRemark(){
returnthis.remark;
}
publicvoidsetRemark(Stringremark){
this.remark=remark;
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!