I try to return all the patients related to a particular user everything works but when I try to return the date i receive the error that I have no appropriate constructor. this is the patient class:
package com.objects; import java.io.Serializable; import java.sql.Date; import java.sql.Timestamp; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.CollectionOfElements; import org.hibernate.cfg.Configuration; import org.hibernate.envers.reader.FirstLevelCache; import javax.persistence.UniqueConstraint; @Entity @Table(name = "patient", uniqueConstraints = { @UniqueConstraint(columnNames = "paitentFirstName"), @UniqueConstraint(columnNames = "paitentLastName") }) public class Patient implements Serializable { @Id @Column(name="id") private int id; private String paitentFirstName; private String paitentLastName; private Timestamp dateOfbirth; private String sex; @ManyToMany(cascade = {CascadeType.ALL},fetch=FetchType.EAGER) @JoinTable(name="User_Patient", joinColumns={@JoinColumn(name="id")}, inverseJoinColumns={@JoinColumn(name="userName")}) private Set<User> users = new HashSet<User>(); @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.patient", cascade=CascadeType.ALL) private Set<JoinDrugPatient> JoinDrugPatient = new HashSet<JoinDrugPatient>(0); public Patient(int id, String paitentFirstName, String paitentLastName, Timestamp dateOfbirth,String sex) { this.id = id; this.paitentFirstName = paitentFirstName; this.paitentLastName = paitentLastName; this.dateOfbirth = dateOfbirth; this.sex = sex; } public Patient(int id, String paitentFirstName,String lastName,String sex){ this.id = id; this.paitentFirstName = paitentFirstName; this.paitentLastName=lastName; this.sex=sex; } public Patient(String date){ paitentFirstName=date; } public Patient(){} public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "paitentFirstName", nullable = false, length = 10) public String getPaitentFirstName() { return paitentFirstName; } public void setPaitentFirstName(String paitentFirstName) { this.paitentFirstName = paitentFirstName; } @Column(name = "paitentLastName", nullable = false, length = 10) public String getPaitentLastName() { return paitentLastName; } public void setPaitentLastName(String paitentLastName) { this.paitentLastName = paitentLastName; } public Timestamp getDateOfbirth() { return dateOfbirth; } public void setDateOfbirth(Timestamp dateOfbirth) { this.dateOfbirth = dateOfbirth; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.patient", cascade=CascadeType.ALL) public Set<JoinDrugPatient> getJoinDrugPatient() { return JoinDrugPatient; } public void setJoinDrugPatient(Set<JoinDrugPatient> joinDrugPatient) { JoinDrugPatient = joinDrugPatient; } public Set<JoinDrugPatient> getStockCategories() { return JoinDrugPatient; } public void setStockCategories(Set<JoinDrugPatient> stockCategories) { this.JoinDrugPatient = stockCategories; } @Override public String toString() { return "Patient [id=" + id + ", paitentFirstName=" + paitentFirstName + ", paitentLastName=" + paitentLastName + ", dateOfbirth=" + dateOfbirth + ", sex=" + sex + "]"; } } this is the query(hibernate)
List<Patient> l=session.createQuery("Select new Patient(p.id,p.paitentFirstName,p.paitentLastName,p.dateOfbirth,p.sex)" +" from Patient p join p.users a where a.UserName=?") .setParameter(0, userName) .list();
from Patient p...? new() operator is useful when you are building an entity constructed by other entities... (check select clause)