#StackBounty: #java #spring-boot Criteria API Unable to locate appropriate constructor on class

Bounty: 50

When I am implementing the Criteria API join for my spring boot study, I tried joining 2 classes and fetching the result. But when I am implementing and running I am getting the following error,

Unable to locate appropriate constructor on class [com.spacestudy.model.Investigator]. Expected arguments are: com.spacestudy.model.Employee
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.spacestudy.model.Investigator]

And my Employee.java class like the following,

@Entity
@Table(name="employee")
public class Employee implements Serializable
{
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "employee_seq_generator")
@SequenceGenerator(name = "employee_seq_generator", sequenceName = "employee_seq",allocationSize=1)
@Column(name="nemp_id",columnDefinition="serial")
public Integer nEmpId;

@Column(name="semp_name")
public String sEmpName;

@Column(name="sdesignation")
public String sDesignation;

@Column(name="ninst_id")
public Integer nInstId;

@Column(name="ndept_id")
public Integer nDeptId;

@Column(name="sclient_emp_id")
public String sClientEmpId;

@Column(name="ntemp_emp_id")
public Integer nTempEmpId;

@Column(name="bis_paid")
public boolean bIsPaid=true;

@Column(name="sunpaid_comment")
public String sUnpaidComment;

@ManyToOne(optional=true)
@JoinColumn(name="ndept_id", insertable = false, updatable = false)
public Department department;

@OneToMany(cascade = CascadeType.ALL,mappedBy="nEmpId")
public Set<Investigator> employeeInvestigatorJoinMapping;

public Employee() {
}

public Employee(Integer nEmpId, String sEmpName, String sDesignation, Integer nInstId, Integer nDeptId,
        String sClientEmpId, Integer nTempEmpId, boolean bIsPaid, String sUnpaidComment, Department department,
        Set<Investigator> employeeInvestigatorJoinMapping) {
    super();
    this.nEmpId = nEmpId;
    this.sEmpName = sEmpName;
    this.sDesignation = sDesignation;
    this.nInstId = nInstId;
    this.nDeptId = nDeptId;
    this.sClientEmpId = sClientEmpId;
    this.nTempEmpId = nTempEmpId;
    this.bIsPaid = bIsPaid;
    this.sUnpaidComment = sUnpaidComment;
    this.department = department;
    this.employeeInvestigatorJoinMapping = employeeInvestigatorJoinMapping;
   }    
 }

And my second class Investigator.java,

@Entity
@Table(name = "investigator")
@JsonInclude(JsonInclude.Include.NON_NULL) // avoiding null values
public class Investigator implements Serializable 
{
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "investigator_seq_generator")
@SequenceGenerator(name = "investigator_seq_generator", sequenceName = "investigator_seq")

@Column(name="ninvestigator_id")
public Integer nInvestigatorId;

@Column(name="sinvestigator_name")
public String sInvestigatorName;

@Column(name="ninst_id")
public Integer nInstId;

@Column(name="stitle")
public String sTitle;

@Column(name="ntemp_investigator_id")
public Integer nTempInvestigatorId;

@ManyToOne(optional = false)
@JoinColumn(name="nemp_id",referencedColumnName="nemp_id")
public Employee nEmpId;

// Default Constructor.
public Investigator()
{
}
public Investigator(Integer nInvestigatorId, String sInvestigatorName, Integer nInstId, String sTitle,
        Integer nTempInvestigatorId, Employee nEmpId) {
    super();
    this.nInvestigatorId = nInvestigatorId;
    this.sInvestigatorName = sInvestigatorName;
    this.nInstId = nInstId;
    this.sTitle = sTitle;
    this.nTempInvestigatorId = nTempInvestigatorId;
    this.nEmpId = nEmpId;
   }
}

And Implemented the Criteria API joining like the foolwoing,

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Investigator> cq = cb.createQuery(Investigator.class);
Root<Employee> rootInvestigator = cq.from(Employee.class);
Join<Employee ,Investigator> resultEmployeeMappingObj 
    = rootInvestigator.join("employeeInvestigatorJoinMapping");
cq.multiselect(rootInvestigator);
cq.where(cb.equal(resultEmployeeMappingObj.get("nEmpId"), 21638));
List<Investigator> results = em.createQuery(cq).getResultList();
return results;

Can anyone help me to find where I went in wring direction please?


Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.