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

my main class:

    int adminNum = sc.nextInt();

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

            int index = doctorList.indexOf(d);


Doctor Class:

    import java.util.*;

    import javax.persistence.*;

    public class Doctor extends Person {

@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){
public List<Patient> getPatients(){
    return patients;

Person Class:

    package edu.cs157b.medicalSystem;

    import javax.persistence.*;

    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    public class Person {
@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){ = sex;

public char getSex(){
    return sex;


No correct solution


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.

@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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow