Skip to content
Advertisement

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.

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:

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. But strangly when I remove join of Department in Employee entity table then I get the result.

Can someone please help if I am missing anything?

Advertisement

Answer

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. So my class are as follows.

Employee class is as follows.

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.

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()

Advertisement