Java 休眠多对多选择错误列

Java 休眠多对多选择错误列,java,spring,hibernate,Java,Spring,Hibernate,解决方法:我在打电话给getAllergies的地方出错了!此设置没有问题 我目前正在从事一个涉及Spring和Hibernate的项目,并且遇到了一个涉及多个关系的问题 以下是java类: Patient.java package medicapp.models.impl; import javax.persistence.*; import java.sql.Date; import java.util.List; @Entity @Table(name="patient") publ

解决方法:我在打电话给getAllergies的地方出错了!此设置没有问题

我目前正在从事一个涉及Spring和Hibernate的项目,并且遇到了一个涉及多个关系的问题

以下是java类:

Patient.java

package medicapp.models.impl;

import javax.persistence.*;
import java.sql.Date;
import java.util.List;


@Entity
@Table(name="patient")
public class Patient {

    @Id
    @Column(name="Id")
    private int id;
    @Column(name="NatNumber")
    private String nationalNumber;
    @Column(name="FirstName")
    private String firstName;
    @Column(name="LastName")
    private String lastName;
    @Column(name="Address")
    private String address;
    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name="birthplace")
    private Municipality birthPlace;
    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name="zipcode")
    private Municipality municipality;
    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name="healthinsuranceid")
    private HealthInsurance healthInsurance;

    @Column(name="Nationality")
    private String nationality;
    @Column(name="ContactName")
    private String contactName;
    @Column(name="ContactNumber")
    private String contactNumber;
    @Column(name="Phone")
    private String phone;
    @Column(name="Email")
    private String email;
    @Column(name="Gender")
    private char gender;
    @Column(name="DateOfBirth")
    private Date birthDate;
    @Column(name="Smoker")
    private boolean smoker;
    // Geen Idee hoe we dat gaan doen, effe opgelost door transient te gebruiken, moet nog worden gecheckt

    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name="bloodgroupid")
    private BloodType bloodType;

    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name="juridicalid")
    private Juridical juridical;

    @ManyToMany
    @JoinTable(name = "allergypatient",
            joinColumns = @JoinColumn(name="PatientId"),
            inverseJoinColumns = @JoinColumn(name = "AllergyId")
    )
    private List<Allergy> allergies;

    @ManyToMany
    @JoinTable(name = "medicationpatient",
            joinColumns = @JoinColumn(name="PatientId"),
            inverseJoinColumns = @JoinColumn(name = "MedicationId")
    )
    //@Transient
    private List<Medication> medications;

    @ManyToMany
    @JoinTable(name = "chronicdiseasepatient",
            joinColumns = @JoinColumn(name="PatientId"),
            inverseJoinColumns = @JoinColumn(name = "ChronicDiseaseId")
    )
    //@Transient
    private List<Disease> diseases;

    @ManyToMany
    @JoinTable(name = "vaccinationpatient",
            joinColumns = @JoinColumn(name="PatientId"),
            inverseJoinColumns = @JoinColumn(name = "VaccinationId")
    )
    //@Transient
    private List<Vaccination> vaccinations;

    @Column(name="lastwill")
    private boolean lastWill;

    /**
     *
     * @param id
     * @param nationalNumber
     * @param firstName
     * @param lastName
     * @param address
     * @param birthPlace
     * @param municipality
     * @param healthInsurance
     * @param nationality
     * @param gender
     * @param birthDate
     */
    public Patient(int id, String nationalNumber, String firstName, String lastName, String address, Municipality birthPlace, Municipality municipality, HealthInsurance healthInsurance, String nationality, char gender, Date birthDate) {
        this.id = id;
        this.nationalNumber = nationalNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.birthPlace = birthPlace;
        this.municipality = municipality;
        this.healthInsurance = healthInsurance;
        this.nationality = nationality;
        this.gender = gender;
        this.birthDate = birthDate;
    }

    /**
     * Default Constructor without parameters, this is required for using Hibernate
     */
    public Patient() {
    }

    /**
     * @param firstName
     * @param lastName
     * @param address
     * @param municipality
     * @param birthDate Patient constructor with the minum required values
     */
    public Patient(String firstName, String lastName, String address, Municipality municipality, Date birthDate) {
        this.firstName = firstName;
        this.address = address;
        this.municipality = municipality;
        this.lastName = lastName;
        this.birthDate = birthDate;
    }
   public Patient(String firstName, String lastName, String address,Date birthDate){
        this.firstName = firstName;
        this.address = address;

        this.lastName = lastName;
        this.birthDate = birthDate;
    }

    public Patient(String firstName, String lastName, String address, Municipality municipality, Municipality birthPlace, String nationalNumber, String nationality, char gender, Date birthDate, String phone, String email, Boolean smoker, Juridical juridical, BloodType bloodType, HealthInsurance healthInsurance, String contactName, String contactNumber, boolean lastWill) {
        this.address = address;
        this.birthDate = birthDate;
        this.birthPlace = birthPlace;
        this.firstName = firstName;
        this.gender = gender;
        this.lastName = lastName;
        this.nationality = nationality;
        this.nationalNumber = nationalNumber;
        this.municipality = municipality;
        this.juridical = juridical;
        this.lastWill = lastWill;
        this.healthInsurance = healthInsurance;
        this.bloodType = bloodType;
        this.phone = phone;
        this.email = email;
        this.smoker = smoker;
        this.contactName = contactName;
        this.contactNumber = contactNumber;

    }


    /**
     * @param firstName
     * @param lastName
     * @param address
     * @param municipality
     * @param birthPlace
     * @param nationalNumber
     * @param nationality
     * @param gender
     * @param birthDate
     * Patient constructor that uses all the values retrieved from the e-ID
     * @author Geert Peters
     */
    public Patient(String firstName, String lastName, String address, Municipality municipality, Municipality birthPlace, String nationalNumber, String nationality, char gender, Date birthDate) {
        this.address = address;
        this.birthDate = birthDate;
        this.birthPlace = birthPlace;
        this.firstName = firstName;
        this.gender = gender;
        this.lastName = lastName;
        this.nationality = nationality;
        this.nationalNumber = nationalNumber;
        this.municipality = municipality;

    }

     /**
     * @param allergy adds an allergy to a patient's file
     */
    public void addAllergy(Allergy allergy) {
        this.allergies.add(allergy);
    }

    /**
     * @param disease adds a disease to a patient's file
     */
    public void addDisease(Disease disease) {
        this.diseases.add(disease);
    }

    /**
     * @param medication adds a medication to a patient's file
     */
    public void addMedication(Medication medication) {
        this.medications.add(medication);
    }

    /**
     * @param vaccination adds a vaccination to a patient's file
     */
    public void addVaccination(Vaccination vaccination) {
        this.vaccinations.add(vaccination);
    }

    /**
     * @param medication removes a medication  from the list
     */
    public void removeMedication(Medication medication) {
        this.medications.remove(medication);
    }

    /**
     * Gets smoker.
     *
     * @return Value of smoker.
     */
    public boolean isSmoker() {
        return smoker;
    }

    /**
     * Gets birthPlace.
     *
     * @return Value of birthPlace.
     */
    public Municipality getBirthPlace() {
        return birthPlace;
    }

    /**
     * Sets new id.
     *
     * @param id New value of id.
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * Sets new smoker.
     *
     * @param smoker New value of smoker.
     */
    public void setSmoker(boolean smoker) {
        this.smoker = smoker;
    }

    /**
     * Gets id.
     *
     * @return Value of id.
     */
    public int getId() {
        return id;
    }

    /**
     * Gets address.
     *
     * @return Value of address.
     */
    public String getAddress() {
        return address;
    }

    /**
     * Sets new lastName.
     *
     * @param lastName New value of lastName.
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Gets gender.
     *
     * @return Value of gender.
     */
    public char getGender() {
        return gender;
    }

    /**
     * Gets nationalNumber.
     *
     * @return Value of nationalNumber.
     */
    public String getNationalNumber() {
        return nationalNumber;
    }



    /**
     * Gets firstName.
     *
     * @return Value of firstName.
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Gets municipality.
     *
     * @return Value of municipality.
     */
    public Municipality getMunicipality() {
        return municipality;
    }

    /**
     * Sets new nationalNumber.
     *
     * @param nationalNumber New value of nationalNumber.
     */
    public void setNationalNumber(String nationalNumber) {
        this.nationalNumber = nationalNumber;
    }

    /**
     * Sets new nationality.
     *
     * @param nationality New value of nationality.
     */
    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    /**
     * Sets new municipality.
     *
     * @param municipality New value of municipality.
     */
    public void setMunicipality(Municipality municipality) {
        this.municipality = municipality;
    }

    /**
     * Gets phone.
     *
     * @return Value of phone.
     */
    public String getPhone() {
        return phone;
    }

    /**
     * Sets new gender.
     *
     * @param gender New value of gender.
     */
    public void setGender(char gender) {
        this.gender = gender;
    }

    /**
     * Gets contactNumber.
     *
     * @return Value of contactNumber.
     */
    public String getContactNumber() {
        return contactNumber;
    }

    /**
     * Gets lastName.
     *
     * @return Value of lastName.
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets new address.
     *
     * @param address New value of address.
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * Sets new firstName.
     *
     * @param firstName New value of firstName.
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Sets new birthPlace.
     *
     * @param birthPlace New value of birthPlace.
     */
    public void setBirthPlace(Municipality birthPlace) {
        this.birthPlace = birthPlace;
    }

    /**
     * Gets nationality.
     *
     * @return Value of nationality.
     */
    public String getNationality() {
        return nationality;
    }

    /**
     * Gets contactName.
     *
     * @return Value of contactName.
     */
    public String getContactName() {
        return contactName;
    }

    /**
     * Gets birthDate.
     *
     * @return Value of birthDate.
     */
    public Date getBirthDate() {
        return birthDate;
    }

    /**
     * Sets new contactName.
     *
     * @param contactName New value of contactName.
     */
    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    /**
     * Sets new phone.
     *
     * @param phone New value of phone.
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * Sets new contactNumber.
     *
     * @param contactNumber New value of contactNumber.
     */
    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }



    /**
     * Sets new birthDate.
     *
     * @param birthDate New value of birthDate.
     */
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    /**
     * Gets vaccinations.
     *
     * @return Value of vaccinations.
     */
    public List<Vaccination> getVaccinations() {
        return vaccinations;
    }

    /**
     * Gets allergies.
     *
     * @return Value(Set) of allergies.
     */
    public List<Allergy> getAllergies() {
        return allergies;
    }

    /**
     * Gets diseases.
     *
     * @return Value(List) of diseases.
     */
    public List<Disease> getDiseases() {
        return diseases;
    }

    /**
     * Gets medications.
     *
     * @return Value(List) of medications.
     */
    public List<Medication> getMedications() {
        return medications;
    }

    /**
     * Gets bloodgroup.
     *
     * @return Value of bloodgroup.
     */
    public BloodType getBloodType() {
        return bloodType;
    }

    /**
     * Sets new bloodgroup.
     *
     * @param bloodType New value of bloodgroup.
     */
    public void setBloodType(BloodType bloodType) {
        this.bloodType = bloodType;
    }

    /**
     * Gets HealthInsurance.
     *
     * @return Value of HealthInsureance.
     */
    public HealthInsurance getHealthInsurance() {
        return healthInsurance;
    }

    /**
     * Sets new HealthInsurance.
     *
     * @param healthInsurance New value of HealthInsurance.
     */
    public void setHealthInsurance(HealthInsurance healthInsurance) {
        this.healthInsurance = healthInsurance;
    }


    /**
     * Gets email.
     *
     * @return Value of email.
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets new lastWill.
     *
     * @param lastWill New value of lastWill.
     */
    public void setLastWill(boolean lastWill) {
        this.lastWill = lastWill;
    }

    /**
     * Sets new juridical.
     *
     * @param juridical New value of juridical.
     */
    public void setJuridical(Juridical juridical) {
        this.juridical = juridical;
    }

    /**
     * Sets new allergies.
     *
     * @param allergies New value of allergies.
     */
    public void setAllergies(List<Allergy> allergies) {
        this.allergies = allergies;
    }

    /**
     * Sets new diseases.
     *
     * @param diseases New value of diseases.
     */
    public void setDiseases(List<Disease> diseases) {
        this.diseases = diseases;
    }

    /**
     * Gets lastWill.
     *
     * @return Value of lastWill.
     */
    public boolean isLastWill() {
        return lastWill;
    }

    /**
     * Gets juridical.
     *
     * @return Value of juridical.
     */
    public Juridical getJuridical() {
        return juridical;
    }

    /**
     * Sets new medications.
     *
     * @param medications New value of medications.
     */
    public void setMedications(List<Medication> medications) {
        this.medications = medications;
    }

    /**
     * Sets new email.
     *
     * @param email New value of email.
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Sets new vaccinations.
     *
     * @param vaccinations New value of vaccinations.
     */
    public void setVaccinations(List<Vaccination> vaccinations) {
        this.vaccinations = vaccinations;
    }
}
这意味着我会得到以下结果:

2 3乳糖

2 5 5只动物

2 5 5只动物

2 19 19 Pennicilline

现在如果我打印出过敏实例的描述,它总是会给我干草,因为这是id为2的过敏

我需要有第二个专栏,作为用来获取过敏的专栏。我有办法改变这个吗


您可以指定hibernate实现的使用jpa/的join COLLMNS

@许多 @可接合( name=“EMP_PROJ”, joinColumns={@JoinColumn(name=“EMP_ID”,referencedColumnName=“ID”)}, inverseJoinColumns={@JoinColumn(name=“PROJ_ID”,referencedColumnName=“ID”)} 私人清单项目


我不明白,根据你发布的sql,第二列是allergyId?我很抱歉,似乎我有一个错误,我调用了getAllergy方法,但非常感谢你的帮助。这似乎没有什么区别,我仍然得到了完全相同的结果
package medicapp.models.impl;

import javax.persistence.*;
import java.util.List;


@Entity
@Table(name="allergy")
public class Allergy {

    @Id
    @Column(name="Id")
    @GeneratedValue
    private int id;

    @Column(name = "Description")
    private String description;

    @ManyToMany(mappedBy = "allergies")
    private List<Patient> patients;

    /**
     * Default Constructor without parameters, this is required for using Hibernate
     */
    public Allergy(){

    }

    /**
     *
     * @param description
     */
    public Allergy(String description){
        this.description = description;
    }


    /**
     * Sets new id.
     *
     * @param id New value of id.
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * Sets new description.
     *
     * @param description New value of description.
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Gets description.
     *
     * @return Value of description.
     */
    public String getDescription() {
        return description;
    }

    /**
     * Gets id.
     *
     * @return Value of id.
     */
    public int getId() {
        return id;
    }
}
select
        allergies0_.PatientId as PatientI1_11_0_,
        allergies0_.AllergyId as AllergyI2_1_0_,
        allergy1_.Id as Id1_0_1_,
        allergy1_.Description as Descript2_0_1_ 
    from
        allergypatient allergies0_ 
    inner join
        allergy allergy1_ 
            on allergies0_.AllergyId=allergy1_.Id 
    where
        allergies0_.PatientId=?