欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JPA学习之联合主键

程序员文章站 2022-03-02 15:32:43
...

使用JPA需要写实体类

package com.wff.web.exam.jobinfo.entities;

import javax.persistence.*;
import java.math.BigInteger;
import java.sql.Date;

@Entity
@Table(name = "user")
public class User {
    @Id
   //自增型
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long user_ID;
    private  String username;
    private  String paW;
    private  int status;//1为导师 2为在职者,3为求职者
    private  String sex;
    private  Date age;
    private Date graduationdate;
    private  String keywords;
    private  String major;
   private  String contactline;//判断手机号码位数
    private  Date registerdate;

    public Long getUser_ID() {
        return user_ID;
    }

    public void setUser_ID(Long user_ID) {
        this.user_ID = user_ID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPaW() {
        return paW;
    }

    public void setPaW(String paW) {
        this.paW = paW;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getSex() {
        return sex;
    }

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

    public Date getAge() {
        return age;
    }

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

    public Date getGraduationdate() {
        return graduationdate;
    }

    public void setGraduationdate(Date graduationdate) {
        this.graduationdate = graduationdate;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getContactLine() {
        return contactline;
    }

    public void setContactLine(String contactLine) {
        this.contactline = contactLine;
    }

    public Date getRegisterDate() {
        return registerdate;
    }

    public void setRegisterDate(Date registerDate) {
        this.registerdate = registerDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "user_ID=" + user_ID +
                ", username='" + username + '\'' +
                ", paW='" + paW + '\'' +
                ", status='" + status + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                ", graduationdate=" + graduationdate +
                ", keywords='" + keywords + '\'' +
                ", major='" + major + '\'' +
                ", contactLine='" + contactline + '\'' +
                ", registerDate=" + registerdate +
                '}';
    }//contactLine
}

其中需要用

 @Id
   //自增型
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long user_ID;

@Id 注解表明主键

但是对于符合主键的情况比如
表User_J

Table: user_j
Columns:
user_ID bigint(20) AI PK
job_ID bigint(20) PK
receivedate date

其中userid与jobld都是主键,则需要添加一个联合主键类PrimaryKey.class,需要注意
1、添加注解@Embeddable
2、重写equals()和hashcode()方法
3、写构造函数

package com.wff.web.exam.jobinfo.entities;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;

@Embeddable  // @Embeddable只使用这个实体内的属性
public class PrimaryKey implements Serializable {


    private  Long user_id;
    private  Long job_id;

    public PrimaryKey() {
    }

    public PrimaryKey(Long user_ID, Long info_ID) {
        this.user_id = user_ID;
        this.job_id= info_ID;
    }

    public Long getUser_id() {
        return user_id;
    }

    public void setUser_id(Long user_ID) {
        this.user_id = user_ID;
    }

    public Long getJob_id() {
        return job_id;
    }

    public void setJob_id(Long info_ID) {
        this.job_id = info_ID;
    }

    public boolean equals(Object object){
        if (this == object) {
            return true;
        }
        if (!(object instanceof PrimaryKey)) {
            return false;
        }
        PrimaryKey primaryKey=(PrimaryKey)object;
        return this.user_id.equals(primaryKey.user_id)
                && this.job_id.equals(primaryKey.job_id);
    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.job_id.hashCode();
        hash = hash * prime + this.user_id.hashCode();
        return hash;
    }


}


再添加实体类,注意
1、此时的主键为PrimaryKey
2、注解不再是@Id,而是@EmbeddedId
3、成员变量名称与数据库字段名称必须统一,否则会报错

package com.wff.web.exam.jobinfo.entities;

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

@Entity
@Table(name = "user_j")
public class UserJ implements  Serializable{

    @EmbeddedId
    private  PrimaryKey id;

    public UserJ(){}


    public PrimaryKey getId() {
        return id;
    }

    public void setId(PrimaryKey id) {
        this.id = id;
    }

    public Date getReceivedate() {
        return receivedate;
    }

    public void setReceivedate(Date receivedate) {
        this.receivedate = receivedate;
    }

    @Column(name = "receivedate")
    private Date receivedate;



    @Override
    public String toString() {
        return "UserJ{" +
                "user_ID=" + id.getUser_id()+
                ", job_ID=" + id.getJob_id() +
                ", date=" + receivedate +
                '}';
    }
}

增删改查操作与平时类似

package com.wff.web.exam.jobinfo.Dao;

import com.wff.web.exam.jobinfo.entities.PrimaryKey;
import com.wff.web.exam.jobinfo.entities.UserJ;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
public interface UserJobDao extends JpaRepository<UserJ,PrimaryKey> ,JpaSpecificationExecutor<UserJ> {

    @Override
    List<UserJ> findAll();

    //    根据userID找到收藏的job
    @Query(value =  "SELECT * FROM user_j u where u.user_id=?1",nativeQuery = true)
    List<UserJ> findById(long id);


}