javahibernate USES annotations to define the federated primary key

  • 2020-05-30 20:16:53
  • OfStack

java hibernate USES annotations to define the federated primary key

Primary keys are defined using the three ways described in hibernate's API below, primarily Annotation for the union primary keys in hibernate

The following is the API document taken to hibernate:

Several syntaxes for defining composite primary keys:

1. Annotate the component class as @Embeddable and the properties of the component as @Id
2. Annotate the properties of the component as @EmbeddedId
3. Annotate the class as @IdClass and all the properties in the entity that belong to the primary key as @Id

Here are three ways to define a federated primary key.

SQL statement for building a table:


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; 

1. Annotate the component class as @Embeddable


/** 
 * SysLogsDtoId Stands for primary key class  
 */ 
package com.hibernate.dto; 
 
import javax.persistence.Embeddable; 
/** 
 * 1 , the primary key class must be implemented java.io.Serializable interface  
 * 2 , the primary key class must be overridden equals and hashCode methods  
 * @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 Maps a class for a table object, where the primary key is the primary key class 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; 
  } 
 
} 

2. Annotate the properties of the component as @EmbeddedId

This is the simplest case, where the primary key class defines only the primary key fields and does not need to write any annotations. Then add the @EmbeddedId annotation to the get method of the primary key class in the object class.


/** 
 * SysLogsDtoId Stands for primary key class  
 */ 
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 Maps a class for a table object, where the primary key is the primary key class 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; 
  } 
 
} 

3. Annotate the class as @IdClass and all properties in the entity that belong to the primary key as @Id


/** 
 * SysLogsDtoId Stands for primary key class  
 */ 
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 Maps a class for a table object, where the primary key is the primary key class 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; 
  } 
 
} 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: