使用注解来定义联合主键

下面使用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. }  
(2)id:单个属性(AUTO、Table)联合主键三种实现方式 一、单个主键@Entity //从这个包(javax.persistence.Entity)可以看出,是个标准,不依赖于hibernate //@Table(name="_Teacher") //当model类和数据库的表名,不一致时,应该在这里指定。在这里类名为:teacher,表名是:_Teacher//生成一个表名是GENERATOR_TABLE的表,有两个字段pk_key、pk_value 阅读详情

相关推荐

谈所谓的一张表多个主键,我们称之为联合主键

@Entity @Table(name = "cm_user_organization_role") @IdClass(CmUserOrganizationRoleVo.class) @SuppressWarnings("serial") public class CmUserOrganizationRoleVo implements Serializable{ @Id @Column(n

张小洛的BOLG 8352

spring 使用注解定义联合主键

Hibernate注解规范的文档中提供了三种方法:      1. 将组件类注解为@Embeddable,并将组件的属性注解为@Id;      2. 将组件的属性注解为@Embeddable;      3. 将类注解为@IdClass,并将该实体中所有主键属性注解为@Id。 这里,我采用的是第三种方法——@IdClass,下面就是具体的代码,大家一块讨论一下。

3217

Hibernate联合主键

在实际开发中,数据库表之间关系有很多种情况,其中共有一个是联合主键,就是中由两个以上的属性同时担任主键, 今天我们就来学习一下,联合主键怎么设计实体类,以及怎么写实体类对应的配置文件*.hbm.xml, 新建一个项目名称为:06hibernate_composite_id,结构如下: 项目以分数实体类为例, 导入jar包,参见《Hibernate环境搭建和配置》

红桃峰峰 的博客 1302

【Spring Data JPA】JPA常用注解

@Entity【指定该实体类被JPA管理】 @Table【指定该实体类对应哪张表】 @Id【单个主键】 @IdClass【联合主键】 @Column【表示实体类属性对应数据库表的哪一字段(列)】

杜小舟的博客 2685

Java实体类设置联合主键_javahibernate使用注解定义联合主键

java hibernate使用注解定义联合主键下面使用hibernate的API中说明的三种方式来定义主键,主要使用Annotation来定义hibernate中的联合主键下面取至hibernate的API文档:定义组合主键的几种语法:1、将组件类注解为@Embeddable,并将组件的属性注解为@Id2、将组件的属性注解为@EmbeddedId3、将类注解为@IdClass,并将该实体中所有...

weixin_30246273的博客 1637

java复合主键注解_java hibernate使用注解定义联合主键_java_脚本之家

java hibernate使用注解定义联合主键下面使用hibernate的API中说明的三种方式来定义主键,主要使用Annotation来定义hibernate中的联合主键下面取至hibernate的API文档:定义组合主键的几种语法:1、将组件类注解为@Embeddable,并将组件的属性注解为@Id2、将组件的属性注解为@EmbeddedId3、将类注解为@IdClass,并将该实体中所...

weixin_35107169的博客 662

JPA/Hibernate实体类定义联合主键@IdClass注解使用

学亮编程手记 2697

注解联合主键

<br />package com.lyis.entity; import java.io.Serializable; import javax.persistence.Column; /** * 用户角色关联信息 * * @author Johnson * @version Tuesday March 15th, 2011 */ public class RUserRoleId implements Serializable { private static final

灵魂在奔跑 2297

Hibernate 中的联合主键解决方案

有时候一张表中会定义多个主键,即联合主键,Hibernate 对联合主键也提供了支持,由于 Annotation 现在用的比 XML 更加流行,则本文以 Annotation 讲解。 既然一张表中定义联合主键,则在面向对象的思想中,他们都是主键,在 JAVA 中可以专门定义一个类来存放主键,Hibernate 文档说明此主键类必须实现 Serializable 接口,并且要重写 equals() 方法和 hashCode() 方法。定义如下主键类 ApplePk: 1: package

MZULE CSDN 1084

hibernate mysql 注解主健_hibernate联合主键 注解方式

上一篇博客写了如何用xml配置联合主键。下面我们看看如何用annotation配置联合主键方法一:主键类用@Embeddable,pojo类仍然用@Entity但是引用主键类的对象用@Id主键pojo类:@EmbeddablepublicclasscomposeIdPKimplementsSerializable{privateStringname;privateintid;@C...

weixin_42516967的博客 193

联合主键映射

联合主键 a)         项目名称:hibernate_1000_one2one_uni_fk_composite b)         @JoinColumns 首先写个联合主键类 package com.sl.ys.one2one; public class WifePK{ private int wife_id; private String wife_name;。。。。g

wgs396838895的专栏 360

Hibernate 联合主键的设置

在mysql建表时可以设置联合主键,现在要建一个一个user表,user表中有三个字段username,usergroup,sex,将username与usergroup设置为联合主键,建表法如下:CREATE TABLE `user` ( `usergroup` varchar(20) NOT NULL, `username` varchar(20) NOT NULL, `sex` v

Kevin.yang专栏 2462

Hibernate注解映射联合主键的三种主要方式

  今天在做项目的时候,一个中间表没有主键,所有在创建实体的时候也未加组件,结果报以下错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateSessionFactory' defined in class path resource [sprin...

weixin_34395205的博客 91

Annotation对Hibernate中联合主键定义

Annotation对Hibernate中联合主键定义 public class User { private int id; private String name; private String address; public int getId() { return id; } public String getName() { return name; } pub

飞奔的鸵鸟 1495

JPA 联合主键

/** * 联合主键 * * 1、必须实现Serializable序列化 2、必须提示无参的构造方法 3、必须重写hashCode和equals方法 * * @Embeddable 表示该类中所有属性在应用该联合主键的类中作为它的属性(字段) * @author 张明学 * */ @Embeddable public class AirLinePK i...

我的IT技术杂谈 189

联合主键

XML配置方法  1.新建一个用于存放联合主键的类.  2.配置映射文件  Java代码   //新增存放联合主键的类.   //需要实现序列化接口,覆盖hashCode和equals方法   package com.meiyoudao.domain;      import java.io.Serializable;      /**   *  

secret_killer的专栏 897

Hibernate 联合主键

第一种方法:比如有一个Student类,想用id和name两者联合作为这个类的主键。那么可以定义一个StudentPK类,其私有变量包括name和id。对于这个外部类,有以下三点要求:①加上注解@Embeddable  ②必须要实现序列化,也就是Serializable接口③要重写equals和hashcode函数。要实现序列化是因为对象需要在内存里存储,为了以后内存满了方便从当前内存转移到另一个...

weixin_39892530的博客 322

JPA中间表(关系表)联合主键配置说明

问题场景 平时在开发中经常会出现多对多的关系,这个时候会创建一个关系表。但该关系表中并没有设置唯一主键字段而是联合主键,那么JPA下创建该关系表实体后运行项目会提示No identifier specified或does not define an IdClass的错误。 下面以用户部门关系进行举例说明,用户和部门是多对多的关系。 数据表结构 CREATE TABLE `mb_member_dept` ( `member_id` bigint(20) NOT NULL , `dept_id` big

五仁月饼的专栏 2693

Hibernate框架-联合主键映射

Hibernate允许直接将实体类的多个属性映射成联合主键,如果需要直接将实体类的多列映射成联合主键,则该实体类必须满足如下条件:i. 有无参的构造方法 ii. 实现Java.io.Serializable接口 iii. 建议根据联合主键列所映射的属性来重写equals()和hashCode()方法例:import java.io.Serializable; import java.util.D

msidolphin的博客 581
上一篇: Windows下删除.svn文件夹的最简易方法
下一篇: maven 实现tomcat的远程部署
weixir123
博客等级 码龄16年 1粉丝 207原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值