1

Hi I am using hibernate to save data into two tables on a database .Also, I am using mysql .

These are my POJO classes,

Patient.java

public class Patient implements java.io.Serializable {

private Integer idPatient;
private String title;
private String firstName;
private String lastName;
private String middleName;
private Date dob;
private Boolean martitalStatus;
private String gender;
private String nic;
private Date dateCreated;
private Date lastUpdated;
private Set<Contact> contacts = new HashSet<Contact>(0);

public Patient() {
}

public Patient(Date lastUpdated) {
    this.lastUpdated = lastUpdated;
}

public Patient(String title, String firstName, String lastName, String middleName, Date dob, Boolean martitalStatus, String gender, String nic, Date dateCreated, Date lastUpdated, Set<Contact> contacts) {
    this.title = title;
    this.firstName = firstName;
    this.lastName = lastName;
    this.middleName = middleName;
    this.dob = dob;
    this.martitalStatus = martitalStatus;
    this.gender = gender;
    this.nic = nic;
    this.dateCreated = dateCreated;
    this.lastUpdated = lastUpdated;
    this.contacts = contacts;
}

public Integer getIdPatient() {
    return this.idPatient;
}

public void setIdPatient(Integer idPatient) {
    this.idPatient = idPatient;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getFirstName() {
    return this.firstName;
}

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

public String getLastName() {
    return this.lastName;
}

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

public String getMiddleName() {
    return this.middleName;
}

public void setMiddleName(String middleName) {
    this.middleName = middleName;
}

public Date getDob() {
    return this.dob;
}

public void setDob(Date dob) {
    this.dob = dob;
}

public Boolean getMartitalStatus() {
    return this.martitalStatus;
}

public void setMartitalStatus(Boolean martitalStatus) {
    this.martitalStatus = martitalStatus;
}

public String getGender() {
    return this.gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getNic() {
    return this.nic;
}

public void setNic(String nic) {
    this.nic = nic;
}

public Date getDateCreated() {
    return this.dateCreated;
}

public void setDateCreated(Date dateCreated) {
    this.dateCreated = dateCreated;
}

public Date getLastUpdated() {
    return this.lastUpdated;
}

public void setLastUpdated(Date lastUpdated) {
    this.lastUpdated = lastUpdated;
}

public Set<Contact> getContacts() {
    return this.contacts;
}

public void setContacts(Set<Contact> contacts) {
    this.contacts = contacts;
}
}

Contact.java

public class Contact  implements java.io.Serializable {


 private Integer idContact;
 private Patient patient;
 private String telephone;
 private String address;

public Contact() {
}


public Contact(Patient patient) {
    this.patient = patient;
}
public Contact(Patient patient, String telephone, String address) {
   this.patient = patient;
   this.telephone = telephone;
   this.address = address;
}

public Integer getIdContact() {
    return this.idContact;
}

public void setIdContact(Integer idContact) {
    this.idContact = idContact;
}
public Patient getPatient() {
    return this.patient;
}

public void setPatient(Patient patient) {
    this.patient = patient;
}
public String getTelephone() {
    return this.telephone;
}

public void setTelephone(String telephone) {
    this.telephone = telephone;
}
public String getAddress() {
    return this.address;
}

public void setAddress(String address) {
    this.address = address;
}
}

Also I am not using annotations in here.

    Patient patient=new Patient();
    Contact contact=new Contact();

    Set<Contact> contacts=new HashSet<Contact>();
    contact.setTelephone("0358965458");
    contact.setAddress("Horana");        
    contacts.add(contact);

    patient.setFirstName("Wajira");
    patient.setLastName("Dahanushka");
    patient.setDateCreated(Common.getSQLCurrentTimeStamp());
    patient.setLastUpdated(Common.getSQLCurrentTimeStamp());        
    patient.setGender("Male");
    patient.setTitle("Mr");
    patient.setNic("9115580466v");
    patient.setContacts(contacts);

    SessionFactory sessionFactory=new HibernateUtil().getSessionFactory();
    Session session=sessionFactory.openSession();     
    session.beginTransaction();

    session.persist(patient);
    session.getTransaction().commit();
    HibernateUtil.shutdown();

Patient.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2016 9:56:01 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="db.Patient" table="patient" catalog="example_hibernate" optimistic-lock="version">
        <id name="idPatient" type="java.lang.Integer">
            <column name="idPatient" />
            <generator class="identity" />
        </id>
        <property name="title" type="string">
            <column name="title" length="45" />
        </property>
        <property name="firstName" type="string">
            <column name="firstName" length="45" />
        </property>
        <property name="lastName" type="string">
            <column name="lastName" length="45" />
        </property>
        <property name="middleName" type="string">
            <column name="middleName" length="45" />
        </property>
        <property name="dob" type="date">
            <column name="dob" length="10" />
        </property>
        <property name="martitalStatus" type="java.lang.Boolean">
            <column name="martitalStatus" />
        </property>
        <property name="gender" type="string">
            <column name="gender" length="45" />
        </property>
        <property name="nic" type="string">
            <column name="nic" length="45" />
        </property>
        <property name="dateCreated" type="timestamp">
            <column name="dateCreated" length="19" />
        </property>
        <property name="lastUpdated" type="timestamp">
            <column name="lastUpdated" length="19" not-null="true" />
        </property>
        <set name="contacts" table="contact" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="idPatient" not-null="true" />
            </key>
            <one-to-many class="db.Contact" />
        </set>
    </class>
</hibernate-mapping>

Contact.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2016 9:56:01 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="db.Contact" table="contact" catalog="example_hibernate" optimistic-lock="version">
        <id name="idContact" type="java.lang.Integer">
            <column name="idContact" />
            <generator class="identity" />
        </id>
        <many-to-one name="patient" class="db.Patient" fetch="select">
            <column name="idPatient" not-null="true" />
        </many-to-one>
        <property name="telephone" type="string">
            <column name="telephone" length="45" />
        </property>
        <property name="address" type="string">
            <column name="address" length="45" />
        </property>
    </class>
</hibernate-mapping>

Contact table has a foreign key which is the primary key of the patient table.

When I run above code , A new patient record could see in patient table but couldn't see any new contact record in contact table .

Is there anything wrong. Have any ideas .

4
  • 1
    Where you define the hibernate mapping? Can you Show it please Commented Sep 8, 2016 at 5:22
  • 1
    My guess is that, as you have defined it, a Patient is not the owning side of the relationship with contacts. Therefore, persisting a Patient does not persist its contacts. Commented Sep 8, 2016 at 5:24
  • 1
    Whether using annotations or XML to define your entity relationships, you will need to specify a OneToMany relationship between your Patient and Contact classes. Commented Sep 8, 2016 at 5:24
  • @TimBiegeleisen : So what should I do ? Commented Sep 8, 2016 at 5:25

2 Answers 2

3

You need to tell Hibernate that you have a one to many relationship betweens patients and their contacts. After you have done this, persisting a Patient object should also persist the entire graph of that patient. If you were using annotations, then something like this should work:

@Entity
@Table(name="Patient")
public class Patient {
    @OneToMany(mappedBy="patient")
    private Set<Contact>  contacts;
}

@Entity
@Table(name="Contact")
public class Contact {
    @ManyToOne
    private Patient patient;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I want to set one to one relationship . And I am not using annotations in there .
If you want a one-to-one relationship, then why are you using a collection of contacts for each patient?
It has been generated by hibernate reverse engineering wizard in netbeans.
Update your question and show us your config files.
I have added my config files.
2

in Patient.hbm.xml while mentioning the set for contacts, use the following cascade property

<set name="contacts" table="contact" cascade="save-update" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="idPatient" not-null="true" />
            </key>
            <one-to-many class="db.Contact" />
        </set>

By mentioning cascade as save-update, you just have to invoke session.save(patientObj), and contact details will automatically get persisted.

Check this link for complete reference of cascading updates/inserts/delete operation in hibernate associations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.