使用注解来定义联合主键

下面使用hibernate的API中说明的三种方式来定义主键,主要使用Annotation来定义hibernate中的联合主键

下面取至hibernate的API文档:

定义组合主键的几种语法:

1、将组件类注解为@Embeddable,并将组件的属性注解为@Id

2、将组件的属性注解为@EmbeddedId

3、将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id

下面就分别使用这三种方式来定义联合主键。

建表的SQL语句:

 

[sql]  view plain copy
 
  1. CREATE TABLE `syslogs` (  
  2.   `id` varchar(50) NOT NULL,  
  3.   `yhid` varchar(50) NOT NULL,  
  4.   `modelname` varchar(100) DEFAULT NULL,  
  5.   `content` varchar(500) DEFAULT NULL,  
  6.   `inserttime` varchar(20) DEFAULT NULL,  
  7.   `remark` varchar(50) DEFAULT NULL,  
  8.   PRIMARY KEY (`id`,`yhid`)  
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf-8;  


一、将组件类注解为@Embeddable

 

 

[java]  view plain copy
 
  1. /** 
  2.  * SysLogsDtoId代表主键类 
  3.  */  
  4. package com.hibernate.dto;  
  5.   
  6. import javax.persistence.Embeddable;  
  7. /** 
  8.  * 1、主键类必须要实现java.io.Serializable接口 
  9.  * 2、主键类必须要重写equals和hashCode方法 
  10.  * @author ibm 
  11.  */  
  12. @Embeddable  
  13. public class SysLogsDtoId implements java.io.Serializable {  
  14.   
  15.     private static final long serialVersionUID = 1L;  
  16.     private String id;  
  17.     private String yhid;  
  18.   
  19.     public SysLogsDtoId() {  
  20.     }  
  21.   
  22.     public SysLogsDtoId(String id, String yhid) {  
  23.         this.id = id;  
  24.         this.yhid = yhid;  
  25.     }  
  26.   
  27.     public String getId() {  
  28.         return this.id;  
  29.     }  
  30.   
  31.     public void setId(String id) {  
  32.         this.id = id;  
  33.     }  
  34.   
  35.     public String getYhid() {  
  36.         return this.yhid;  
  37.     }  
  38.   
  39.     public void setYhid(String yhid) {  
  40.         this.yhid = yhid;  
  41.     }  
  42.   
  43.     public boolean equals(Object other) {  
  44.         if ((this == other))  
  45.             return true;  
  46.         if ((other == null))  
  47.             return false;  
  48.         if (!(other instanceof SysLogsDtoId))  
  49.             return false;  
  50.         SysLogsDtoId castOther = (SysLogsDtoId) other;  
  51.   
  52.         return ((this.getId() == castOther.getId()) || (this.getId() != null && castOther.getId() != null && this.getId().equals(castOther.getId())))  
  53.                 && ((this.getYhid() == castOther.getYhid()) || (this.getYhid() != null && castOther.getYhid() != null && this.getYhid().equals(  
  54.                         castOther.getYhid())));  
  55.     }  
  56.   
  57.     public int hashCode() {  
  58.         int result = 17;  
  59.   
  60.         result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());  
  61.         result = 37 * result + (getYhid() == null ? 0 : this.getYhid().hashCode());  
  62.         return result;  
  63.     }  
  64.   
  65. }  

 

[java]  view plain copy
 
  1. /** 
  2.  * SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId 
  3.  */  
  4. package com.hibernate.dto;  
  5.   
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.Id;  
  9. import javax.persistence.Table;  
  10.   
  11. @Entity  
  12. @Table(name = "syslogs")  
  13. public class SysLogsDto implements java.io.Serializable {  
  14.     private static final long serialVersionUID = 1L;  
  15.     private SysLogsDtoId id;  
  16.     private String modelname;  
  17.     private String content;  
  18.     private String inserttime;  
  19.     private String remark;  
  20.   
  21.     public SysLogsDto() {  
  22.     }  
  23.   
  24.     public SysLogsDto(SysLogsDtoId id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public SysLogsDto(SysLogsDtoId id, String modelname, String content, String inserttime, String remark) {  
  29.         this.id = id;  
  30.         this.modelname = modelname;  
  31.         this.content = content;  
  32.         this.inserttime = inserttime;  
  33.         this.remark = remark;  
  34.     }  
  35.   
  36.     @Id  
  37.     public SysLogsDtoId getId() {  
  38.         return this.id;  
  39.     }  
  40.   
  41.     public void setId(SysLogsDtoId id) {  
  42.         this.id = id;  
  43.     }  
  44.   
  45.     @Column(name = "modelname", length = 100)  
  46.     public String getModelname() {  
  47.         return this.modelname;  
  48.     }  
  49.   
  50.     public void setModelname(String modelname) {  
  51.         this.modelname = modelname;  
  52.     }  
  53.   
  54.     @Column(name = "content", length = 500)  
  55.     public String getContent() {  
  56.         return this.content;  
  57.     }  
  58.   
  59.     public void setContent(String content) {  
  60.         this.content = content;  
  61.     }  
  62.   
  63.     @Column(name = "inserttime", length = 20)  
  64.     public String getInserttime() {  
  65.         return this.inserttime;  
  66.     }  
  67.   
  68.     public void setInserttime(String inserttime) {  
  69.         this.inserttime = inserttime;  
  70.     }  
  71.   
  72.     @Column(name = "remark", length = 50)  
  73.     public String getRemark() {  
  74.         return this.remark;  
  75.     }  
  76.   
  77.     public void setRemark(String remark) {  
  78.         this.remark = remark;  
  79.     }  
  80.   
  81. }  


二、将组件的属性注解为@EmbeddedId

 

这种情况最简单,主键类只用定义主键字段,不需要写任何注解。然后在对象类中在主键类的get方法上加上@EmbeddedId注解。

 

[java]  view plain copy
 
  1. /** 
  2.  * SysLogsDtoId代表主键类 
  3.  */  
  4. package com.hibernate.dto;  
  5.   
  6. public class SysLogsDtoId implements java.io.Serializable {  
  7.   
  8.     private static final long serialVersionUID = 1L;  
  9.     private String id;  
  10.     private String yhid;  
  11.   
  12.     public SysLogsDtoId() {  
  13.     }  
  14.   
  15.     public SysLogsDtoId(String id, String yhid) {  
  16.         this.id = id;  
  17.         this.yhid = yhid;  
  18.     }  
  19.   
  20.     public String getId() {  
  21.         return this.id;  
  22.     }  
  23.   
  24.     public void setId(String id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public String getYhid() {  
  29.         return this.yhid;  
  30.     }  
  31.   
  32.     public void setYhid(String yhid) {  
  33.         this.yhid = yhid;  
  34.     }  
  35.   
  36.     public boolean equals(Object other) {  
  37.         if ((this == other))  
  38.             return true;  
  39.         if ((other == null))  
  40.             return false;  
  41.         if (!(other instanceof SysLogsDtoId))  
  42.             return false;  
  43.         SysLogsDtoId castOther = (SysLogsDtoId) other;  
  44.   
  45.         return ((this.getId() == castOther.getId()) || (this.getId() != null && castOther.getId() != null && this.getId().equals(castOther.getId())))  
  46.                 && ((this.getYhid() == castOther.getYhid()) || (this.getYhid() != null && castOther.getYhid() != null && this.getYhid().equals(  
  47.                         castOther.getYhid())));  
  48.     }  
  49.   
  50.     public int hashCode() {  
  51.         int result = 17;  
  52.   
  53.         result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());  
  54.         result = 37 * result + (getYhid() == null ? 0 : this.getYhid().hashCode());  
  55.         return result;  
  56.     }  
  57.   
  58. }  



 

 

[java]  view plain copy
 
  1. /** 
  2.  * SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId 
  3.  */  
  4. package com.hibernate.dto;  
  5.   
  6. import javax.persistence.Column;  
  7. import javax.persistence.EmbeddedId;  
  8. import javax.persistence.Entity;  
  9. import javax.persistence.Table;  
  10.   
  11. @Entity  
  12. @Table(name = "syslogs")  
  13. public class SysLogsDto implements java.io.Serializable {  
  14.     private static final long serialVersionUID = 1L;  
  15.     private SysLogsDtoId id;  
  16.     private String modelname;  
  17.     private String content;  
  18.     private String inserttime;  
  19.     private String remark;  
  20.   
  21.     public SysLogsDto() {  
  22.     }  
  23.   
  24.     public SysLogsDto(SysLogsDtoId id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public SysLogsDto(SysLogsDtoId id, String modelname, String content, String inserttime, String remark) {  
  29.         this.id = id;  
  30.         this.modelname = modelname;  
  31.         this.content = content;  
  32.         this.inserttime = inserttime;  
  33.         this.remark = remark;  
  34.     }  
  35.   
  36.     @EmbeddedId  
  37.     public SysLogsDtoId getId() {  
  38.         return this.id;  
  39.     }  
  40.   
  41.     public void setId(SysLogsDtoId id) {  
  42.         this.id = id;  
  43.     }  
  44.   
  45.     @Column(name = "modelname", length = 100)  
  46.     public String getModelname() {  
  47.         return this.modelname;  
  48.     }  
  49.   
  50.     public void setModelname(String modelname) {  
  51.         this.modelname = modelname;  
  52.     }  
  53.   
  54.     @Column(name = "content", length = 500)  
  55.     public String getContent() {  
  56.         return this.content;  
  57.     }  
  58.   
  59.     public void setContent(String content) {  
  60.         this.content = content;  
  61.     }  
  62.   
  63.     @Column(name = "inserttime", length = 20)  
  64.     public String getInserttime() {  
  65.         return this.inserttime;  
  66.     }  
  67.   
  68.     public void setInserttime(String inserttime) {  
  69.         this.inserttime = inserttime;  
  70.     }  
  71.   
  72.     @Column(name = "remark", length = 50)  
  73.     public String getRemark() {  
  74.         return this.remark;  
  75.     }  
  76.   
  77.     public void setRemark(String remark) {  
  78.         this.remark = remark;  
  79.     }  
  80.   
  81. }  


三、将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id

 

 

[html]  view plain copy
 
  1. /**  
  2.  * SysLogsDtoId代表主键类  
  3.  */  
  4. package com.hibernate.dto;  
  5.   
  6. public class SysLogsDtoId implements java.io.Serializable {  
  7.   
  8.     private static final long serialVersionUID = 1L;  
  9.     private String id;  
  10.     private String yhid;  
  11.   
  12.     public SysLogsDtoId() {  
  13.     }  
  14.   
  15.     public SysLogsDtoId(String id, String yhid) {  
  16.         this.id = id;  
  17.         this.yhid = yhid;  
  18.     }  
  19.   
  20.     public String getId() {  
  21.         return this.id;  
  22.     }  
  23.   
  24.     public void setId(String id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public String getYhid() {  
  29.         return this.yhid;  
  30.     }  
  31.   
  32.     public void setYhid(String yhid) {  
  33.         this.yhid = yhid;  
  34.     }  
  35.   
  36.     public boolean equals(Object other) {  
  37.         if ((this == other))  
  38.             return true;  
  39.         if ((other == null))  
  40.             return false;  
  41.         if (!(other instanceof SysLogsDtoId))  
  42.             return false;  
  43.         SysLogsDtoId castOther = (SysLogsDtoId) other;  
  44.   
  45.         return ((this.getId() == castOther.getId()) || (this.getId() != null && castOther.getId() != null && this.getId().equals(castOther.getId())))  
  46.                 && ((this.getYhid() == castOther.getYhid()) || (this.getYhid() != null && castOther.getYhid() != null && this.getYhid().equals(  
  47.                         castOther.getYhid())));  
  48.     }  
  49.   
  50.     public int hashCode() {  
  51.         int result = 17;  
  52.   
  53.         result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());  
  54.         result = 37 * result + (getYhid() == null ? 0 : this.getYhid().hashCode());  
  55.         return result;  
  56.     }  
  57.   
  58. }  

 

[java]  view plain copy
 
  1. /** 
  2.  * SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId 
  3.  */  
  4. package com.hibernate.dto;  
  5.   
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.Id;  
  9. import javax.persistence.IdClass;  
  10. import javax.persistence.Table;  
  11.   
  12. @Entity  
  13. @Table(name = "syslogs")  
  14. @IdClass(value=SysLogsDtoId.class)  
  15. public class SysLogsDto implements java.io.Serializable {  
  16.     private static final long serialVersionUID = 1L;  
  17.     private String id;  
  18.     private String yhid;  
  19.     private String modelname;  
  20.     private String content;  
  21.     private String inserttime;  
  22.     private String remark;  
  23.   
  24.     public SysLogsDto() {  
  25.     }  
  26.   
  27.     @Id  
  28.     public String getId() {  
  29.         return id;  
  30.     }  
  31.   
  32.   
  33.     public void setId(String id) {  
  34.         this.id = id;  
  35.     }  
  36.   
  37.     @Id  
  38.     public String getYhid() {  
  39.         return yhid;  
  40.     }  
  41.   
  42.   
  43.     public void setYhid(String yhid) {  
  44.         this.yhid = yhid;  
  45.     }  
  46.   
  47.   
  48.     @Column(name = "modelname", length = 100)  
  49.     public String getModelname() {  
  50.         return this.modelname;  
  51.     }  
  52.   
  53.     public void setModelname(String modelname) {  
  54.         this.modelname = modelname;  
  55.     }  
  56.   
  57.     @Column(name = "content", length = 500)  
  58.     public String getContent() {  
  59.         return this.content;  
  60.     }  
  61.   
  62.     public void setContent(String content) {  
  63.         this.content = content;  
  64.     }  
  65.   
  66.     @Column(name = "inserttime", length = 20)  
  67.     public String getInserttime() {  
  68.         return this.inserttime;  
  69.     }  
  70.   
  71.     public void setInserttime(String inserttime) {  
  72.         this.inserttime = inserttime;  
  73.     }  
  74.   
  75.     @Column(name = "remark", length = 50)  
  76.     public String getRemark() {  
  77.         return this.remark;  
  78.     }  
  79.   
  80.     public void setRemark(String remark) {  
  81.         this.remark = remark;  
  82.     }  
  83.   
  84. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值