`

使用注解来定义联合主键

 
阅读更多

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

下面取至hibernate的API文档:

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

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

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

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

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

建表的SQL语句:

 

[sql] view plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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. }  
分享到:
评论

相关推荐

    java hibernate使用注解来定义联合主键

    主要介绍了java hibernate使用注解来定义联合主键的相关资料,需要的朋友可以参考下

    Hibernate注释大全收藏

    @Id 注解可将实体Bean中某个属性定义为主键,使用@GenerateValue注解可以定义该标识符的生成策略。 • AUTO - 可以是 identity column, sequence 或者 table 类型,取决于不同底层的数据库 • TABLE - 使用table...

    Hibernate注解

    * generator 指定生成主键使用的生成器(可能是orcale中的序列)。 * @SequenceGenerator —— 注解声明了一个数据库序列。该注解有如下属性 * name 表示该表主键生成策略名称,它被引用在@GeneratedValue中设置的...

    Spring.3.x企业应用开发实战(完整版).part2

    4.10.1 使用注解定义Bean 4.10.2 使用注解配置信息启动Spring容器 4.10.3 自动装配Bean 4.10.4 Bean作用范围及生命过程方法 4.11 基于Java类的配置 4.11.1 使用Java类提供Bean定义信息 4.11.2 使用基于Java类的配置...

    Spring3.x企业应用开发实战(完整版) part1

    4.10.1 使用注解定义Bean 4.10.2 使用注解配置信息启动Spring容器 4.10.3 自动装配Bean 4.10.4 Bean作用范围及生命过程方法 4.11 基于Java类的配置 4.11.1 使用Java类提供Bean定义信息 4.11.2 使用基于Java类的配置...

    最全Hibernate 参考文档

    5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...

    Hibernate实战(第2版 中文高清版)

     1.1.3 在Java中使用SQL   1.1.4 面向对象应用程序中的持久化   1.2 范式不匹配   1.2.1 粒度问题   1.2.2 子类型问题   1.2.3 同一性问题   1.2.4 与关联相关的问题   1.2.5 数据导航的问题   ...

    数据库设计规范.pdf

    UPDATE/DELETE是通过所有字段来定位操作的⾏,相当于每⾏就是⼀次全表扫描 少数情况可以使⽤联合唯⼀主键,需与DBA协商 对于主键字段值是从其它地⽅插⼊(⾮⾃⼰使⽤AUTO_INCREMENT⽣产),去掉AUTO_INCREMENT定义。...

    hibernate 框架详解

    使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合...

    hibernate 体系结构与配置 参考文档(html)

    使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    Hibernate3+中文参考文档

    5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...

    hibernate3.04中文文档.chm

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    Hibernate参考文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    Hibernate教程

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    Hibernate 中文 html 帮助文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    C#编程经验技巧宝典

    14 <br>0028 “///”符号的使用技巧 14 <br>0029 使用注释取消程序语句的执行 15 <br>2.2 语句 15 <br>0030 跳转语句GOTO的使用 15 <br>0031 Continue语句的使用 16 <br>0032 Break...

    Hibernate3的帮助文档

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    JAVA上百实例源码以及开源项目源代码

     Java非对称加密源程序代码实例,本例中使用RSA加密技术,定义加密算法可用 DES,DESede,Blowfish等。  设定字符串为“张三,你好,我是李四”  产生张三的密钥对(keyPairZhang)  张三生成公钥(publicKeyZhang...

Global site tag (gtag.js) - Google Analytics