Hibernate's annotation method of joint primary key

Hibernate union primary key annotation method, that is, a table in Hibernate has multiple fields united as a unique primary key. At this time, you can use the federated primary key of hibernate to operate. Take the user table as an example, userId+userName is the unique primary key, because sometimes the names of multiple users are the same. If you add userId, you can control the uniqueness and judge whether they are the same user.

Note:

  • The primary key class (UserPK class) refers to the class with only the union fields of userId and userName, that is, the class with multiple fields as the primary key = = Union primary key class.
  • The User class refers to the entity class of the User table.

One way

  • Primary key class (UserPK class): this class implements java.io.Serializable Interface, override equals and hascode, and annotate the class as @ Embeddable.
  • Entity class (User class): this class does not contain the fields in the primary key class, but it needs to save the reference of the joint primary key class, generate the set and get methods, and annotate the reference as @Id . Entity class annotation is @ entity and @ Table.

Entity class:

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

/**
 * @author: ZHJ
 * @date:Created in 11:15 2020/3/16
 * @modify By:
 * @description :User Entity class
 */
@Entity
@Table(name = "user")
public class User implements Serializable {
    private Integer age;
    private String email;
    private Date birthday;
    @Id
    private UserPK userPK;//composite key class 

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public UserPK getUserPK() {
        return userPK;
    }

    public void setUserPK(UserPK userPK) {
        this.userPK = userPK;
    }
}

Primary key class:

import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;

/**
 * @author: ZHJ
 * @date:Created in 11:21 2020/3/16
 * @modify By:
 * @description :User Union primary key class of
 */
@Embeddable
public class UserPK implements Serializable{
    private Integer userId;
    private String userName;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    //Rewrite hashCode
    @Override
    public int hashCode() {
        return Objects.hash(this.userId,this.userName);
    }
    //Rewrite equal
    @Override
    public boolean equals(Object obj) {
        if(obj instanceof UserPK){
            UserPK key = (UserPK)obj;
            if(this.userId.equals(key.getUserId()) && this.userName.equals(key.getUserName())){
                return true;
            }
        }
        return false;
    }
}

Mode two

  • Primary key class (UserPK class): this class implements java.io.Serializable Interface and override equals and hascode without @ Embeddable annotation.
  • Entity class (User class): this class does not contain the fields in the primary key class, but it needs to save the reference of the joint primary key class, generate the set and get methods, and annotate the reference as @ EmbeddedId. Entity class annotation is @ entity and @ Table.

Entity class:

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @author: ZHJ
 * @date:Created in 11:15 2020/3/16
 * @modify By:
 * @description :User Entity class
 */
@Entity
@Table(name = "user")
public class User implements Serializable {
    private Integer age;
    private String email;
    private Date birthday;
    @EmbeddedId
    private UserPK userPK;

    public UserPK getUserPK() {
        return userPK;
    }

    public void setUserPK(UserPK userPK) {
        this.userPK = userPK;
    }

    @Column(name = "age", nullable = true, length = 11)
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
    @Column(name = "email", nullable = true, length = 240)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    @Column(name = "birthday", nullable = true, length = 0)
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

Primary key class:

import java.io.Serializable;
import java.util.Objects;

/**
 * @author: ZHJ
 * @date:Created in 11:21 2020/3/16
 * @modify By:
 * @description :User Union primary key class of
 */
public class UserPK implements Serializable{
    private Integer userId;
    private String userName;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    //Rewrite hashCode
    @Override
    public int hashCode() {
        return Objects.hash(this.userId,this.userName);
    }
    //Rewrite equal
    @Override
    public boolean equals(Object obj) {
        if(obj instanceof UserPK){
            UserPK key = (UserPK)obj;
            if(this.userId.equals(key.getUserId()) && this.userName.equals(key.getUserName())){
                return true;
            }
        }
        return false;
    }
}

Mode three

  • Primary key class (UserPK class): this class implements java.io.Serializable Interface and override equals and hascode without @ Embeddable annotation.
  • Entity class (User class): this class contains the fields in the union primary key class. Annotate all the union primary key fields as @ Id, and add the annotation above this class: @ IdClass (Union primary key class. Class). Entity class annotation is @ entity and @ Table.

Entity class:

‚Äčimport javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @author: ZHJ
 * @date:Created in 11:15 2020/3/16
 * @modify By:
 * @description :User Entity class
 */
@Entity
@Table(name = "user")
@IdClass(UserPK.class)
public class User implements Serializable {
    @Id
    @Column(name = "user_id", nullable = false, length = 11)
    private Integer userId;//Union primary key field
    @Id
    @Column(name = "user_name", nullable = false, length = 11)
    private String userName;//Union primary key field
    @Column(name = "age", nullable = true, length = 11)
    private Integer age;
    @Column(name = "email", nullable = true, length = 240)
    private String email;
    @Column(name = "birthday", nullable = true, length = 0)
    private Date birthday;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

Primary key class:

import java.io.Serializable;
import java.util.Objects;

/**
 * @author: ZHJ
 * @date:Created in 11:21 2020/3/16
 * @modify By:
 * @description :User Union primary key class of
 */
public class UserPK implements Serializable{
    private Integer userId;
    private String userName;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    //Rewrite hashCode
    @Override
    public int hashCode() {
        return Objects.hash(this.userId,this.userName);
    }
    //Rewrite equal
    @Override
    public boolean equals(Object obj) {
        if(obj instanceof UserPK){
            UserPK key = (UserPK)obj;
            if(this.userId.equals(key.getUserId()) && this.userName.equals(key.getUserName())){
                return true;
            }
        }
        return false;
    }
}

Tags: Programming Java Hibernate

Posted on Mon, 16 Mar 2020 00:28:05 -0400 by bkbragg