2

I am doing a program on Many-To-One in Hibernate and when I write the following code in DAO:

sessionFactory = HibernateUtility.createSessionFactory();
            session = sessionFactory.openSession();
           PatientEntity patientEntity=new PatientEntity();
           DoctorEntity doctorEntity=(DoctorEntity) session.get(DoctorEntity.class, doctorId);
         System.out.println(doctorEntity);
           if (doctorEntity!= null) 
           {
               System.out.println("hello");
                patientEntity.setId(patient.getId());
                patientEntity.setName(patient.getName());
                patientEntity.setAge(patient.getAge());
                patientEntity.setPhoneNumber(patient.getPhoneNumber());
                patientEntity.setDoctor(doctorEntity);
            }

            session.getTransaction().begin();
            session.persist(patientEntity);
            session.getTransaction().commit();t the 

I get the following exception:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): entity.PatientEntity

My Entity classes:

@Entity
@Table(name="Doctor")
public class DoctorEntity {
    @Id
    @Column(name="id")
    private String id;
    private String name;
    private String phoneNumber;
    private String address;


    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

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

    @Id
    private Integer id;
    private String name;
    private String phoneNumber;
    private Integer age;
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="id",unique=true,insertable=false,updatable=false)
    private DoctorEntity doctor;
    public Integer getId() 
    {
        return id;
    }
    public void setId(Integer id) 
    {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public DoctorEntity getDoctor() {
        return doctor;
    }
    public void setDoctor(DoctorEntity doctor) {
        this.doctor = doctor;
    }
}

I have been through different links but not able to correlate my problem with it. Hibernate Many-To-One Relationship without Primary Key or Join Table

Repeated column in mapping for entity (should be mapped with insert="false" update="false")

Another Repeated column in mapping for entity error

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity

Any help is appreciated!

Community
  • 1
  • 1
A_J
  • 977
  • 4
  • 16
  • 44

2 Answers2

3

You might have to specify @GeneratedValue(strategy=GenerationType.AUTO) or use your own custom generators.

rockcode
  • 333
  • 1
  • 8
  • I am sorry to say that I didn't check the doctorEntity fetched from database was null corresponding to the given doctorId. So it was not going inside the if condition and not setting the primary key of patient table. Now when I have taken a valid doctorId, the exception has changed to the following: Repeated column in mapping for entity: entity.PatientEntity column: id (should be mapped with insert="false" update="false") So my question is: Is it compulsory to write unique=true,insertable=false,updatable=false over the reference of DoctorEntity in PatientEntity class ? – A_J Aug 25 '15 at 00:19
0

You need to specify the @GeneratedValue Annotation with Generation strategy of your choice in the Patient class.