0

我尝试返回与特定用户相关的所有患者,一切正常,但是当我尝试返回日期时,我收到错误,即我没有合适的构造函数。这是病人类:

    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 + "]";
    }





}

这是查询(休眠)

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

1 回答 1

1

这个问题不是Hibernate问题,它是真正的Java问题,如Effective Java (2nd Edition)中所述。

Java 平台库中有一些类确实扩展了可实例化的类并添加了值组件。例如,java.sql.Timestamp 扩展了 java.util.Date 并添加了一个纳秒字段。Timestamp 的 equals 实现确实违反了对称性,如果 Timestamp 和 Date 对象在同一个集合中使用或以其他方式混合使用,则可能导致不稳定的行为。Timestamp 类有一个免责声明,警告程序员不要混合日期和时间戳。虽然只要将它们分开就不会遇到麻烦,但没有什么可以阻止您混合它们,并且由此产生的错误可能难以调试。Timestamp 类的这种行为是错误的,不应被模仿。(布洛赫,有效的 Java,第 2 版。)

你在课堂上有Users什么?java.sql.Timestamp如果您在代码中的任何地方都使用,则不会有任何问题。

于 2013-08-03T21:02:42.727 回答