java hibernate使用注解來定義聯(lián)合主鍵
java hibernate使用注解來定義聯(lián)合主鍵
下面使用hibernate的API中說明的三種方式來定義主鍵,主要使用Annotation來定義hibernate中的聯(lián)合主鍵
下面取至hibernate的API文檔:
定義組合主鍵的幾種語法:
1、將組件類注解為@Embeddable,并將組件的屬性注解為@Id
2、將組件的屬性注解為@EmbeddedId
3、將類注解為@IdClass,并將該實(shí)體中所有屬于主鍵的屬性都注解為@Id
下面就分別使用這三種方式來定義聯(lián)合主鍵。
建表的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、主鍵類必須要實(shí)現(xiàn)java.io.Serializable接口 * 2、主鍵類必須要重寫equals和hashCode方法 * @author ibm */ @Embeddable 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.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; } @Id public 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; } @EmbeddedId public 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,并將該實(shí)體中所有屬于主鍵的屬性都注解為@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() { } @Id public String getId() { return id; } public void setId(String id) { this.id = id; } @Id public 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; } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- JAVA中通過Hibernate-Validation進(jìn)行參數(shù)驗(yàn)證
- 詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
- Java框架篇:Spring+SpringMVC+hibernate整合開發(fā)
- Java的Hibernate框架中復(fù)合主鍵映射的創(chuàng)建和使用教程
- Java的Hibernate框架結(jié)合MySQL的入門學(xué)習(xí)教程
- 從最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate
- 在Java的Hibernate框架中使用SQL語句的簡單介紹
- Java web Hibernate如何與數(shù)據(jù)庫鏈接
相關(guān)文章
SpringBoot自動配置實(shí)現(xiàn)流程詳細(xì)分析
這篇文章主要介紹了SpringBoot自動配置原理分析,SpringBoot是我們經(jīng)常使用的框架,那么你能不能針對SpringBoot實(shí)現(xiàn)自動配置做一個詳細(xì)的介紹。如果可以的話,能不能畫一下實(shí)現(xiàn)自動配置的流程圖。牽扯到哪些關(guān)鍵類,以及哪些關(guān)鍵點(diǎn)2022-12-12Java Speech API實(shí)現(xiàn)語音識別
Java語音識別是一項(xiàng)非常有用的功能,它可以將語音轉(zhuǎn)換為文本,從而實(shí)現(xiàn)語音輸入和語音控制功能,在當(dāng)今數(shù)字化時代,語音識別技術(shù)逐漸成為人機(jī)交互的重要方式之一,語音識別技術(shù)可以幫助我們將語音數(shù)據(jù)轉(zhuǎn)化為文字,進(jìn)而進(jìn)行后續(xù)的處理和分析2023-10-10