hibernate 聯合主鍵配置
阿新 • • 發佈:2021-07-27
一.可行demo: 聯合主鍵, id自增
主鍵類可以把欄位單獨放一個類,實現Serializable介面,重寫equals和hascode方法
自增:@GeneratedValue(strategy=GenerationType.AUTO)
表結構
DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` bigint(19) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', `trigger_type` int(2) NOT NULL COMMENT '觸發型別:10駛入山西、20駛出山西、30駛入西安、40駛出西安', `trigger_position` varchar(200) CHARACTER SET utf8 DEFAULT NULL COMMENT '觸發位置', PRIMARY KEY (`id`, `trigger_type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='test' PARTITION BY LIST(trigger_type) ( PARTITION shanxi VALUES IN (10, 20), PARTITION xian VALUES IN (30, 40) );
package com.entity; import lombok.Data; import javax.persistence.*; /** * @ClassName FenceLockCarEntity * @author xxxx * @version 1.0.0 * @Description test */ @Entity @Table(name = "test") @Data public class TestEntity { @Id private TestKey testkey; @Id @Embedded @AttributeOverrides({ @AttributeOverride(name = "id", column = @Column(name = "id")), @AttributeOverride(name = "triggerType", column = @Column(name = "trigger_type")) }) public TestKey getTestkey() { return testkey; } public void setTestkey(TestKey testkey) { this.testkey = testkey; } }
package com.entity; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.io.Serializable; public class TestKey implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(nullable = false, name = "id", columnDefinition = "bigint") private Long id; /** * 觸發型別:10駛入山西、20駛出山西、30駛入西安、40駛出西安 */ @Id @Column(nullable = false, name = "trigger_type") private Integer triggerType; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Integer getTriggerType() { return triggerType; } public void setTriggerType(Integer triggerType) { this.triggerType = triggerType; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof TestKey)) { return false; } TestKey key = (TestKey) obj; if (!this.id.equals(key.getId())) { return false; } if (!this.triggerType.equals(key.getTriggerType())) { return false; } return true; } @Override public int hashCode() { int result = 0; result = this.id == null ? 0 : this.id.hashCode(); result = 29 * (this.triggerType == null ? 0 : this.triggerType.hashCode()) + result; return result; } }
二.參考其他
Hibernate用註解配置複合主鍵有三種方式
①@Embeddable + @Id + @Embedded
②@Embeddable + @EmbeddedId
③@IdClass + @Id
解釋:
@Embeddable 表示這個類可以嵌入到別的類中去,常以表示其他類的某個屬性。
@Embedded 它和 @Embeddable 正好相反,它用來表示某個屬性是被嵌入進來的。
@EmbeddedId = @Embedded + @Id
宣告:
如果採用第三種方式,則必須為主鍵類寫上預設構造方法,因為Hibernate是通過預設構造方法來例項化主鍵類的物件的。
首先,給出主鍵類的原始碼:
package cn.saicent.model;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class CompositeKeyModelKey implements Serializable {
private static final long serialVersionUID = 1717638966560959020L;
private Integer kkey1;
private Integer kkey2;
public CompositeKeyModelKey(Integer kkey1, Integer kkey2) {
this.kkey1 = kkey1;
this.kkey2 = kkey2;
}
public Integer getkkey1() {
return kkey1;
}
public void setkkey1(Integer kkey1) {
this.kkey1 = kkey1;
}
public Integer getkkey2() {
return kkey2;
}
public void setkkey2(Integer kkey2) {
this.kkey2 = kkey2;
}
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(! (obj instanceof CompositeKeyModelKey)) {
return false;
}
CompositeKeyModelKey key = (CompositeKeyModelKey)obj;
if(!kkey1.equals(key.getkkey1())) {
return false;
}
if(!kkey2.equals(key.getkkey2())) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = 0;
result = kkey1 == null ? 0 : kkey1.hashCode();
result = 29 * (kkey2 == null ? 0 : kkey2.hashCode()) + result;
return result;
}
}
第一種方法(@Embeddable + @Id + @Embedded)的Pojo原始碼如下:
package cn.saicent.model;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="COMPOSITE_KEY_TABLE")
public class CompositeKeyModel {
private CompositeKeyModelKey compositeKeyModelKey;
private String field;
@Column(name = "FIELD")
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
@Id
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "kkey1", column = @Column(name="KEY1")),
@AttributeOverride(name = "kkey2", column = @Column(name="KEY2"))
})
public CompositeKeyModelKey getCompositeKeyModelKey() {
return compositeKeyModelKey;
}
public void setCompositeKeyModelKey(CompositeKeyModelKey compositeKeyModelKey) {
this.compositeKeyModelKey = compositeKeyModelKey;
}
}
第二種方法(@Embeddable + @EmbeddedId)的Pojo原始碼如下:
package cn.saicent.model;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="COMPOSITE_KEY_TABLE")
public class CompositeKeyModel {
private CompositeKeyModelKey compositeKeyModelKey;
private String field;
@Column(name = "FIELD")
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "kkey1", column = @Column(name="KEY1")),
@AttributeOverride(name = "kkey2", column = @Column(name="KEY2"))
})
public CompositeKeyModelKey getCompositeKeyModelKey() {
return compositeKeyModelKey;
}
public void setCompositeKeyModelKey(CompositeKeyModelKey compositeKeyModelKey) {
this.compositeKeyModelKey = compositeKeyModelKey;
}
}
第三種方法(@IdClass + @Id)的Pojo類原始碼如下:
package cn.saicent.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name="COMPOSITE_KEY_TABLE")
@IdClass(CompositeKeyModelKey.class)
public class CompositeKeyModel {
private Integer kkey1;
private Integer kkey2;
private String field;
@Column(name = "FIELD")
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
@Id
@Column(name = "KEY1")
public Integer getKkey1() {
return kkey1;
}
public void setKkey1(Integer kkey1) {
this.kkey1 = kkey1;
}
@Id
@Column(name = "KEY2")
public Integer getKkey2() {
return kkey2;
}
public void setKkey2(Integer kkey2) {
this.kkey2 = kkey2;
}
}