Hibernate operates table (composite primary key) query without primary key

This records the annotation method of the combined primary key in hibernate .

The hibernate-annotations documentation says that the combined primary key is implemented using annotations, as follows

Here are several syntaxes for defining a composite primary key:

1. Annotate the component class as @Embeddable, and annotate the component's properties as @Id

2. Annotate the properties of the component as @EmbeddedId

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


I used the second one and it worked perfectly!


The second way is simpler

1. Primary key class, this class does not need any annotations

package com.seed.lee.model;

import javax.persistence.Column;

/**
* This class is used as the (combined primary key) primary key class of the Person class
*
* @author Administrator
*
*/

public class PeopleUionPKID implements java.io.Serializable {

    private String firstName;

    private String secondName;

    @Column(length=20)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    @Column(length=20)
    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof PeopleUionPKID){
            PeopleUionPKID pk=(PeopleUionPKID)obj;
            if(this.firstName.equals(pk.firstName)&&this.secondName.equals(pk.secondName)){
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

}

2. The class mapped by the data table (just add @EmbeddedId to the reference of the primary key class)

package com.seed.lee.model;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Peopel {

    PeopleUionPKID uionPKID = new PeopleUionPKID();

    private int age;

    private String sex;

    private String job;

    @EmbeddedId
    public PeopleUionPKID getUionPKID() {
        return uionPKID;
    }

    public void setUionPKID(PeopleUionPKID uionPKID) {
        this.uionPKID = uionPKID;
    }

    public int getAge() {
        return age;
    }

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

    @Column(length=4)
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Column(length=20)
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

}
https://blog.csdn.net/renzhe_liubin/article/details/69388999 Reprinted from

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769420&siteId=291194637