国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java hibernate使用注解來定義聯合主鍵

java hibernate使用注解來定義聯合主鍵

2020-07-28 14:40CSDN Java教程

這篇文章主要介紹了java hibernate使用注解來定義聯合主鍵的相關資料,需要的朋友可以參考下

java  hibernate使用注解來定義聯合主鍵

下面使用hibernate的API中說明的三種方式來定義主鍵,主要使用Annotation來定義hibernate中的聯合主鍵

下面取至hibernate的API文檔:

定義組合主鍵的幾種語法:

1、將組件類注解為@Embeddable,并將組件的屬性注解為@Id
2、將組件的屬性注解為@EmbeddedId
3、將類注解為@IdClass,并將該實體中所有屬于主鍵的屬性都注解為@Id

下面就分別使用這三種方式來定義聯合主鍵。

建表的SQL語句:

?
1
2
3
4
5
6
7
8
9
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * SysLogsDtoId代表主鍵類
 */
package com.hibernate.dto;
 
import javax.persistence.Embeddable;
/**
 * 1、主鍵類必須要實現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;
  }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * 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注解。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * 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;
  }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * 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,并將該實體中所有屬于主鍵的屬性都注解為@Id

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * 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;
  }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 * 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;
  }
 
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/wyc_cs/article/details/9031991

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜寂寞少妇aaa片毛片 | 伊人激情 | 亚洲第一成av人网站懂色 | 欧美福利视频 | 成人乱码一区二区三区av | 日韩欧美在线观看 | 日韩av片无码一区二区不卡电影 | 国产视频久久 | 国产在线观看一区 | 91久久精品国产91久久性色tv | 欧美视频二区 | 神马久久久久久久 | 欧美成人精品一区二区男人看 | 高清视频一区二区三区 | 亚洲一区二区国产 | 日本中文在线 | 亚洲欧美日韩国产综合 | 午夜一级片 | 欧美日韩三区 | 久久精品国语 | 亚洲欧美视频在线 | 日韩美一级片 | 玖玖国产精品视频 | 日韩免费观看视频 | 成人久久久久久久久 | 亚洲免费色| 欧美午夜一区二区三区免费大片 | 国产一区二区三区在线观看网站 | 免费精品人在线二线三线区别 | 亚洲成人一二三 | 成人看片毛片免费播放器 | 日韩毛片免费在线观看 | 日韩福利视频 | 国产91色 | 国产免费一区二区三区最新6 | 成人高清视频在线观看 | 成人在线网站 | 激情五月婷婷基地 | 久久亚洲一区 | 国产一级一级国产 | 精品久久av |