简体   繁体   English

spring 数据 jpa 中的连接表

[英]Join tables in spring data jpa

I have an issue in joining two tables column.我在加入两个表格列时遇到问题。 I have two entities Status Report and Employee.我有两个实体状态报告和员工。 and I want the data of employee inside StatusReport.我想要 StatusReport 中的员工数据。

package com.sl.ems.models;

import javax.persistence.*;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

@Entity
@Table(name="statusreport")
public class StatusReport {
    private BigInteger COMPLIANCEID;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger STATUSRPTID;
    private BigInteger EMPID;
    private String COMMENTS;
    private Date CREATEDDATE;
    private BigInteger DEPARTMENT_ID;

    @OneToOne
    @JoinTable(name = "Employees")
    @JoinColumn(name = "EMPID")
    private Employees employee;

    public StatusReport(){
    }
    public StatusReport(BigInteger COMPLIANCEID,BigInteger EMPID,
                        String COMMENTS,Date CREATEDDATE,BigInteger DEPARTMENT_ID){
        this.COMPLIANCEID=COMPLIANCEID;
        this.EMPID=EMPID;
        this.COMMENTS=COMMENTS;
        this.CREATEDDATE=CREATEDDATE;
        this.DEPARTMENT_ID=DEPARTMENT_ID;
    }
    public BigInteger getCOMPLIANCEID() {
        return COMPLIANCEID;
    }

    public void setCOMPLIANCEID(BigInteger COMPLIANCEID) {
        this.COMPLIANCEID = COMPLIANCEID;
    }

    public BigInteger getSTATUSRPTID() {
        return STATUSRPTID;
    }

    public void setSTATUSRPTID(BigInteger STATUSRPTID) {
        this.STATUSRPTID = STATUSRPTID;
    }

    public BigInteger getEMPID() {
        return EMPID;
    }

    public void setEMPID(BigInteger EMPID) {
        this.EMPID = EMPID;
    }

    public String getCOMMENTS() {
        return COMMENTS;
    }

    public void setCOMMENTS(String COMMENTS) {
        this.COMMENTS = COMMENTS;
    }

    public Date getCREATEDDATE() {
        return CREATEDDATE;
    }

    public void setCREATEDDATE(Date CREATEDDATE) {
        this.CREATEDDATE = CREATEDDATE;
    }

    public BigInteger getDEPARTMENT_ID() {
        return DEPARTMENT_ID;
    }

    public void setDEPARTMENT_ID(BigInteger DEPARTMENT_ID) {
        this.DEPARTMENT_ID = DEPARTMENT_ID;
    }

    public Employees getEmployee() {
        return employee;
    }

    public void setEmployee(Employees employee) {
        this.employee = employee;
    }
}

Another class is the employee:另一个 class 是员工:

package com.sl.ems.models;

import com.sl.ems.utils.Utils;

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

@Entity
public class Employees {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger EMPID;
    private String FIRSTNAME;
    private String LASTNAME;
    private Date DOB;
    private String EMAIL;
    private BigInteger DEPARTMENT_ID;

    @OneToOne
    @JoinTable(name = "Department")
    @JoinColumn(name = "DEPARTMENT_ID")
    private Department department;

    public Employees(){
    }
    public Employees(String FIRSTNAME,String LASTNAME,Date DOB,String EMAIL,BigInteger DEPARTMENT_ID){
        this.FIRSTNAME=FIRSTNAME;
        this.LASTNAME=LASTNAME;
        this.DOB=DOB;
        this.EMAIL=EMAIL;
        this.DEPARTMENT_ID=DEPARTMENT_ID;
    }

    public BigInteger getEMPID() {
        return EMPID;
    }

    public void setEMPID(BigInteger EMPID) {
        this.EMPID = EMPID;
    }

    public String getFIRSTNAME() {
        return FIRSTNAME;
    }

    public void setFIRSTNAME(String FIRSTNAME) {
        this.FIRSTNAME = FIRSTNAME;
    }

    public String getLASTNAME() {
        return LASTNAME;
    }

    public void setLASTNAME(String LASTNAME) {
        this.LASTNAME = LASTNAME;
    }

    public Date getDOB() {
        return DOB;
    }

    public void setDOB(Date DOB) {
        this.DOB = DOB;
    }

    public String getEMAIL() {
        return EMAIL;
    }

    public void setEMAIL(String EMAIL) {
        this.EMAIL = EMAIL;
    }

    public BigInteger getDEPARTMENT_ID() {
        return DEPARTMENT_ID;
    }

    public void setDEPARTMENT_ID(BigInteger DEPARTMENT_ID) {
        this.DEPARTMENT_ID = DEPARTMENT_ID;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
}

As you can see employee entity itself have some other joins on other tables.如您所见,员工实体本身在其他表上还有一些其他联接。 Which is a deparment table.这是一个部门表。

package com.sl.ems.models;

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

@Entity
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger DEPARTMENT_ID;
    private String DEPARTMENT_NM;

    public Department(){
    }
    public Department(String DEPARTMENT_NM){
        this.DEPARTMENT_NM=DEPARTMENT_NM;
    }
    public BigInteger getDEPARTMENT_ID() {
        return DEPARTMENT_ID;
    }

    public void setDEPARTMENT_ID(BigInteger DEPARTMENT_ID) {
        this.DEPARTMENT_ID = DEPARTMENT_ID;
    }

    public String getDEPARTMENT_NM() {
        return DEPARTMENT_NM;
    }

    public void setDEPARTMENT_NM(String DEPARTMENT_NM) {
        this.DEPARTMENT_NM = DEPARTMENT_NM;
    }
}

When I join Status Report with Employee I get Sql exception.当我与员工一起加入状态报告时,我得到 Sql 异常。 But strangly when I remove join of Department in Employee entity table then I get the result.但奇怪的是,当我在 Employee 实体表中删除 Department 的连接时,我得到了结果。

Can someone please help if I am missing anything?如果我缺少任何东西,有人可以帮忙吗?

Looks like your mapping is not correct.看起来你的映射不正确。 Also verify you have a EMPID column.还要验证您有一个 EMPID 列。 You don't need to use the @JoinTable annotation in your case.在您的情况下,您不需要使用@JoinTable注释。

StatusReport - removed private BigInteger EMPID; StatusReport - 删除private BigInteger EMPID; as it is ued n joining因为它被使用 n 加入

@Entity
@Table(name="statusreport")
public class StatusReport {
    private BigInteger COMPLIANCEID;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger STATUSRPTID;
    private String COMMENTS;
    private Date CREATEDDATE;
    private BigInteger DEPARTMENT_ID;

    @OneToOne
    @JoinColumn(name = "EMPID")
    private Employees employee;
    
    //others methods
    

Employee - removed private BigInteger DEPARTMENT_ID;员工 - 删除private BigInteger DEPARTMENT_ID; as it is ued n joining因为它被使用 n 加入

@Entity
public class Employees {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger EMPID;
    private String FIRSTNAME;
    private String LASTNAME;
    private Date DOB;
    private String EMAIL;

    @OneToOne
    @JoinColumn(name = "DEPARTMENT_ID")
    private Department department;

Well taking help from above post.从上面的帖子中得到帮助。 I made few other changes in my code.我对我的代码做了一些其他更改。 As I could not manage to remove the field completely from my entity class so I made it Transient and set its property from the join column object method.由于我无法从我的实体 class 中完全删除该字段,因此我将其设为瞬态并从连接列 object 方法设置其属性。 So my class are as follows.所以我的 class 如下。

Employee class is as follows.员工 class 如下。

package com.sl.ems.models;

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

@Entity
public class Employees {
    /**
     Author: Puneet Kumar Bahuguna
     Year: DEC 2020
     Project: SimplyLearn EMS
     Description: This Entity class mapped to the employees table in the database.
     **/
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger EMPID;
    private String FIRSTNAME;
    private String LASTNAME;
    private Date DOB;
    private String EMAIL;
    @Transient
    private BigInteger DEPARTMENT_ID;

    @OneToOne
    @JoinColumn(name = "DEPARTMENT_ID")
    private Department department;

    public Employees(){
    }
    public Employees(BigInteger EMPID){
        this.EMPID=EMPID;
    }
    public Employees(String FIRSTNAME,String LASTNAME,Date DOB,String EMAIL,Department department){
        this.FIRSTNAME=FIRSTNAME;
        this.LASTNAME=LASTNAME;
        this.DOB=DOB;
        this.EMAIL=EMAIL;
        this.department=department;
    }

    public BigInteger getEMPID() {
        return EMPID;
    }

    public void setEMPID(BigInteger EMPID) {
        this.EMPID = EMPID;
    }

    public String getFIRSTNAME() {
        return FIRSTNAME;
    }

    public void setFIRSTNAME(String FIRSTNAME) {
        this.FIRSTNAME = FIRSTNAME;
    }

    public String getLASTNAME() {
        return LASTNAME;
    }

    public void setLASTNAME(String LASTNAME) {
        this.LASTNAME = LASTNAME;
    }

    public Date getDOB() {
        return DOB;
    }

    public void setDOB(Date DOB) {
        this.DOB = DOB;
    }

    public String getEMAIL() {
        return EMAIL;
    }

    public void setEMAIL(String EMAIL) {
        this.EMAIL = EMAIL;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public void setDEPARTMENT_ID(BigInteger DEPARTMENT_ID) {
        this.DEPARTMENT_ID = DEPARTMENT_ID;
    }

    public BigInteger getDEPARTMENT_ID() {
        return DEPARTMENT_ID;
    }
}

StatusReport class is as follows.状态报告 class 如下。

package com.sl.ems.models;

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

@Entity
@Table(name="statusreport")
public class StatusReport {
    /**
     Author: Puneet Kumar Bahuguna
     Year: DEC 2020
     Project: SimplyLearn EMS
     Description: This Entity class mapped to the statusreport table in the database.
     **/
    private BigInteger COMPLIANCEID;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger STATUSRPTID;
    private String COMMENTS;
    private Date CREATEDDATE;
    private BigInteger DEPARTMENT_ID;

    @Transient
    private BigInteger EMPID;

    @OneToOne
    @JoinColumn(name = "EMPID")
    private Employees employee;


    public StatusReport(){
    }
    public StatusReport(String COMMENTS,Date CREATEDDATE){

    }
    public StatusReport(BigInteger COMPLIANCEID,String COMMENTS,Date CREATEDDATE,
                        BigInteger DEPARTMENT_ID,Employees employee){
        this.COMPLIANCEID=COMPLIANCEID;
        this.COMMENTS=COMMENTS;
        this.CREATEDDATE=CREATEDDATE;
        this.DEPARTMENT_ID=DEPARTMENT_ID;
        this.employee=employee;
    }
    public BigInteger getCOMPLIANCEID() {
        return COMPLIANCEID;
    }

    public void setCOMPLIANCEID(BigInteger COMPLIANCEID) {
        this.COMPLIANCEID = COMPLIANCEID;
    }

    public void setEMPID(BigInteger EMPID) {
        this.EMPID = EMPID;
    }

    public BigInteger getEMPID() {
        return EMPID;
    }
    public BigInteger getSTATUSRPTID() {
        return STATUSRPTID;
    }

    public void setSTATUSRPTID(BigInteger STATUSRPTID) {
        this.STATUSRPTID = STATUSRPTID;
    }

    public String getCOMMENTS() {
        return COMMENTS;
    }

    public void setCOMMENTS(String COMMENTS) {
        this.COMMENTS = COMMENTS;
    }

    public Date getCREATEDDATE() {
        return CREATEDDATE;
    }

    public void setCREATEDDATE(Date CREATEDDATE) {
        this.CREATEDDATE = CREATEDDATE;
    }

    public BigInteger getDEPARTMENT_ID() {
        return DEPARTMENT_ID;
    }

    public void setDEPARTMENT_ID(BigInteger DEPARTMENT_ID) {
        this.DEPARTMENT_ID = DEPARTMENT_ID;
    }

    public Employees getEmployee() {
        return employee;
    }

    public void setEmployee(Employees employee) {
        this.employee = employee;
    }
}

Please also note for example while you are saving a StatusReport object by using save method of jpa you will have to set the EMPID through getEmployee().getEMPID()另请注意,例如,当您使用 jpa 的保存方法保存状态报告 object 时,您必须通过 getEmployee().getEMPID() 设置 EMPID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM