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!