So I am having a little trouble trying to get my create user function to work. I am trying to create a new user for every new user entry from the Scanner console, but my Hibernate is updating the same person ID, instead of assigning a new person ID to the new user.

my DAO class:

    public void createDoctor(Doctor doctor) {
    sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(doctor);
    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}

my main class:

    int adminNum = sc.nextInt();

        if (adminNum == 1) {
            System.out.print("Please enter doctor's first name: ");
            sc.nextLine();
            String firstName = sc.nextLine();
            System.out.print("Please enter doctor's last name: ");
            String lastName = sc.nextLine();
            Doctor d = new Doctor();
            d.setFirstName(firstName);
            d.setLastName(lastName);

            doctorList.add(d);
            int index = doctorList.indexOf(d);
            dao.createDoctor(doctorList.get(index));

        }

Doctor Class:

    import java.util.*;

    import javax.persistence.*;

    @Entity
    public class Doctor extends Person {

@OneToOne
@JoinColumn(name = "SPECIALTY_ID")
private Specialty specialty;

@OneToMany(mappedBy = "doctor", targetEntity = Patient.class,fetch=FetchType.EAGER, cascade= CascadeType.ALL )
private List<Patient> patients;

private double salary;


public Doctor(){
    patients = new ArrayList<Patient>();
}

public void setSalary(double salary) {
    this.salary = salary;
}

public double getSalary() {
    return salary;
}

public void setSpecialty(Specialty specialty) {
    this.specialty = specialty;
}

public Specialty getspecialty() {
    return specialty;
}

public void setPatient(Patient patient){
    patients.add(patient);
}
public List<Patient> getPatients(){
    return patients;
}
    }

Person Class:

    package edu.cs157b.medicalSystem;

    import javax.persistence.*;

    @Entity
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    public class Person {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "PERSON_ID")
private int personId;
private String first_name;
private String last_name;
private char sex;

public Person() {

}

public int getPersonId() {
    return personId;
}

public void setPersonId(int personId) {
    this.personId = personId;
}

public void setFirstName(String first_name) {
    this.first_name = first_name;
}

public String getFirstName() {
    return first_name;
}

public void setLastName(String last_name) {
    this.last_name = last_name;
}

public String getLastName() {
    return last_name;
}

public void setSex(char sex){
    this.sex = sex;
}

public char getSex(){
    return sex;
}

    }

没有正确的解决方案

其他提示

In your Person class declare the id field as Integer so that newly created instances will have null value as personId. The primitive int type will always have 0 as initial value.

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "PERSON_ID")
private Integer personId;

I don't like this code much.

Personally, I'd prefer having a ctor that did something rather than a do-nothing default ctor and having to call setters.

I'd persist the Doctor, then add it to the List.

Is this synchronized? Doesn't look thread safe if that List is shared, mutable data.

I see no equals or hashCode implemented here. That might explain it. Best to use the primary key fields to determine equality.

I think you're desperately in need of a tutorial. Forget your assignment for a moment and do something simple:

http://docs.jboss.org/hibernate/core/3.3/reference/en-US/html/tutorial.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top