使用注解来定义联合主键
来源:互联网 发布:电锯惊魂1解析 知乎 编辑:程序博客网 时间:2024/06/05 00:11
下面使用hibernate的API中说明的三种方式来定义主键,主要使用Annotation来定义hibernate中的联合主键
下面取至hibernate的API文档:
定义组合主键的几种语法:
1、将组件类注解为@Embeddable,并将组件的属性注解为@Id
2、将组件的属性注解为@EmbeddedId
3、将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id
下面就分别使用这三种方式来定义联合主键。
建表的SQL语句:
CREATE TABLE `syslogs` ( `id` varchar(50) NOT NULL, `yhid` varchar(50) NOT NULL, `modelname` varchar(100) DEFAULT NULL, `content` varchar(500) DEFAULT NULL, `inserttime` varchar(20) DEFAULT NULL, `remark` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`,`yhid`)) ENGINE=InnoDB DEFAULT CHARSET=utf-8;
一、将组件类注解为@Embeddable
/** * SysLogsDtoId代表主键类 */package com.hibernate.dto;import javax.persistence.Embeddable;/** * 1、主键类必须要实现java.io.Serializable接口 * 2、主键类必须要重写equals和hashCode方法 * @author ibm */@Embeddablepublic class SysLogsDtoId implements java.io.Serializable {private static final long serialVersionUID = 1L;private String id;private String yhid;public SysLogsDtoId() {}public SysLogsDtoId(String id, String yhid) {this.id = id;this.yhid = yhid;}public String getId() {return this.id;}public void setId(String id) {this.id = id;}public String getYhid() {return this.yhid;}public void setYhid(String yhid) {this.yhid = yhid;}public boolean equals(Object other) {if ((this == other))return true;if ((other == null))return false;if (!(other instanceof SysLogsDtoId))return false;SysLogsDtoId castOther = (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())));}public int hashCode() {int result = 17;result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());result = 37 * result + (getYhid() == null ? 0 : this.getYhid().hashCode());return result;}}
/** * SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId */package com.hibernate.dto;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "syslogs")public class SysLogsDto implements java.io.Serializable {private static final long serialVersionUID = 1L;private SysLogsDtoId id;private String modelname;private String content;private String inserttime;private String remark;public SysLogsDto() {}public SysLogsDto(SysLogsDtoId id) {this.id = id;}public SysLogsDto(SysLogsDtoId id, String modelname, String content, String inserttime, String remark) {this.id = id;this.modelname = modelname;this.content = content;this.inserttime = inserttime;this.remark = remark;}@Idpublic SysLogsDtoId getId() {return this.id;}public void setId(SysLogsDtoId id) {this.id = id;}@Column(name = "modelname", length = 100)public String getModelname() {return this.modelname;}public void setModelname(String modelname) {this.modelname = modelname;}@Column(name = "content", length = 500)public String getContent() {return this.content;}public void setContent(String content) {this.content = content;}@Column(name = "inserttime", length = 20)public String getInserttime() {return this.inserttime;}public void setInserttime(String inserttime) {this.inserttime = inserttime;}@Column(name = "remark", length = 50)public String getRemark() {return this.remark;}public void setRemark(String remark) {this.remark = remark;}}
二、将组件的属性注解为@EmbeddedId
这种情况最简单,主键类只用定义主键字段,不需要写任何注解。然后在对象类中在主键类的get方法上加上@EmbeddedId注解。
/** * SysLogsDtoId代表主键类 */package com.hibernate.dto;public class SysLogsDtoId implements java.io.Serializable {private static final long serialVersionUID = 1L;private String id;private String yhid;public SysLogsDtoId() {}public SysLogsDtoId(String id, String yhid) {this.id = id;this.yhid = yhid;}public String getId() {return this.id;}public void setId(String id) {this.id = id;}public String getYhid() {return this.yhid;}public void setYhid(String yhid) {this.yhid = yhid;}public boolean equals(Object other) {if ((this == other))return true;if ((other == null))return false;if (!(other instanceof SysLogsDtoId))return false;SysLogsDtoId castOther = (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())));}public int hashCode() {int result = 17;result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());result = 37 * result + (getYhid() == null ? 0 : this.getYhid().hashCode());return result;}}
/** * SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId */package com.hibernate.dto;import javax.persistence.Column;import javax.persistence.EmbeddedId;import javax.persistence.Entity;import javax.persistence.Table;@Entity@Table(name = "syslogs")public class SysLogsDto implements java.io.Serializable {private static final long serialVersionUID = 1L;private SysLogsDtoId id;private String modelname;private String content;private String inserttime;private String remark;public SysLogsDto() {}public SysLogsDto(SysLogsDtoId id) {this.id = id;}public SysLogsDto(SysLogsDtoId id, String modelname, String content, String inserttime, String remark) {this.id = id;this.modelname = modelname;this.content = content;this.inserttime = inserttime;this.remark = remark;}@EmbeddedIdpublic SysLogsDtoId getId() {return this.id;}public void setId(SysLogsDtoId id) {this.id = id;}@Column(name = "modelname", length = 100)public String getModelname() {return this.modelname;}public void setModelname(String modelname) {this.modelname = modelname;}@Column(name = "content", length = 500)public String getContent() {return this.content;}public void setContent(String content) {this.content = content;}@Column(name = "inserttime", length = 20)public String getInserttime() {return this.inserttime;}public void setInserttime(String inserttime) {this.inserttime = inserttime;}@Column(name = "remark", length = 50)public String getRemark() {return this.remark;}public void setRemark(String remark) {this.remark = remark;}}
三、将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id
/** * SysLogsDtoId代表主键类 */package com.hibernate.dto;public class SysLogsDtoId implements java.io.Serializable {private static final long serialVersionUID = 1L;private String id;private String yhid;public SysLogsDtoId() {}public SysLogsDtoId(String id, String yhid) {this.id = id;this.yhid = yhid;}public String getId() {return this.id;}public void setId(String id) {this.id = id;}public String getYhid() {return this.yhid;}public void setYhid(String yhid) {this.yhid = yhid;}public boolean equals(Object other) {if ((this == other))return true;if ((other == null))return false;if (!(other instanceof SysLogsDtoId))return false;SysLogsDtoId castOther = (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())));}public int hashCode() {int result = 17;result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());result = 37 * result + (getYhid() == null ? 0 : this.getYhid().hashCode());return result;}}
/** * SysLogsDto为表对象映射类,其中主键为主键类SysLogsDtoId */package com.hibernate.dto;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.IdClass;import javax.persistence.Table;@Entity@Table(name = "syslogs")@IdClass(value=SysLogsDtoId.class)public class SysLogsDto implements java.io.Serializable {private static final long serialVersionUID = 1L;private String id;private String yhid;private String modelname;private String content;private String inserttime;private String remark;public SysLogsDto() {}@Idpublic String getId() {return id;}public void setId(String id) {this.id = id;}@Idpublic String getYhid() {return yhid;}public void setYhid(String yhid) {this.yhid = yhid;}@Column(name = "modelname", length = 100)public String getModelname() {return this.modelname;}public void setModelname(String modelname) {this.modelname = modelname;}@Column(name = "content", length = 500)public String getContent() {return this.content;}public void setContent(String content) {this.content = content;}@Column(name = "inserttime", length = 20)public String getInserttime() {return this.inserttime;}public void setInserttime(String inserttime) {this.inserttime = inserttime;}@Column(name = "remark", length = 50)public String getRemark() {return this.remark;}public void setRemark(String remark) {this.remark = remark;}}
更多文章见:http://www.16boke.com
- 使用注解来定义联合主键
- Hibernate中使用注解生成联合主键
- 注解联合主键
- java注解联合主键
- SQL定义联合主键
- hibernate联合主键 注解方式
- 联合主键的使用
- 联合主键用Hibernate注解映射方式
- Hibernate5联合主键的注解式开发
- hibernate基于注解的联合主键映射
- hibernate笔记-008-联合主键注解配置
- Annotation对Hibernate中联合主键定义
- Annotation对Hibernate中联合主键定义
- hibernate中联合主键的定义方式
- hibernate 注解 ,视图无主键,怎么配置联合主键
- JPA联合主键的使用
- Hibernate联合主键的使用
- spring jpa使用联合主键
- as3实现的拼图游戏
- ASP.NET的内置对象
- 在Jenkins中集成cobertura
- js常用判断方法
- git 学习大闯过
- 使用注解来定义联合主键
- 裸设备配置步骤
- 根据css 样式实现全选
- python的socket
- 简历制作
- 排序算法系列之堆排序
- Java集合框架-Set(TreeSet)
- BusyBox1.21.0编译流程
- 图片上传预览 兼容ie67 8 9 及 FF