Reputation: 31
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();
Upvotes: 0
Views: 703
Reputation:
This issue is not a Hibernate issue it is truly a Java issue as is stated in Effective Java (2nd Edition).
There are some classes in the Java platform libraries that do extend an instantiable class and add a value component. For example, java.sql.Timestamp extends java.util.Date and adds a nanoseconds field. The equals implementation for Timestamp does violate symmetry and can cause erratic behavior if Timestamp and Date objects are used in the same collection or are otherwise intermixed. The Timestamp class has a disclaimer cautioning programmers against mixing dates and timestamps. While you won’t get into trouble as long as you keep them separate, there’s nothing to prevent you from mixing them, and the resulting errors can be hard to debug. This behavior of the Timestamp class was a mistake and should not be emulated. (Bloch, Effective Java, 2nd Ed.)
What do you have in the class
Users
? You won't have any problem if you are usingjava.sql.Timestamp
everywhere in your code.
Upvotes: 1