M Sach
M Sach

Reputation: 34424

Behaviour of One to one association in Hibernate?

Below is the my complete code

Here is cfg file

<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property     name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property   name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
    <property name="hibernate.connection.username">MyProject1</property>
    <property name="hibernate.connection.password">tingtong</property>
    <property name="hibernate.default_schema">MyProject1</property>


    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

      <property name="hibernate.hbm2ddl.auto" >update</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Mapping files -->
    <mapping resource="Person.hbm.xml"/>
   </session-factory>
 </hibernate-configuration>

Below is the mapping file

<hibernate-mapping>

 <class name="com.daasl.Person" table="person">
  <id name="id" type="int">
     <generator class="increment"/>
  </id>

  <property name="name" column="cname" type="string"/>
  <one-to-one name="address" class="com.daasl.Address" property-ref="personId"   cascade="all"/>

  </class>


 <class name="com.daasl.Address" table="Address">
  <id name="id" type="int">
     <generator class="increment"/>
  </id>

    <!--for one to one -->
      <property name="personId"  type="int"/> 
  <property name="addressLine1"  type="string"/>

 </hibernate-mapping>

below is the person Value object

public class Person implements Serializable {

private int id;
private String name;
//private int addressId;
private Address address;
// getter seeter for each field
}

below is address value object

 public class Address {

private int id;
private int personId;
private String addressLine1;
// getter seeter for each field

}

Below is the code snippet from main method

tx = session.beginTransaction();
            person = (Person)session.get(Person.class, 4);

        // Create a Person object and save it
        Person p1 = new Person();
        p1.setName("Scott");
        Address add1= new Address();
        add1.setAddressLine1("NY1");
        p1.setAddress(add1);
        session.save(p1);


     //   p1.setName("mohit1");
        // Retrieve the person objects
         person = (Person)session. get(Person.class,1);

        tx.commit();
        tx = null;

As above code when I save Person object I expect address object to be saved also alongwith correponding personid. When I run above program personid is going as 0 in personid column inside address table. as per my understanding personid should be inserted with with person table id value. Right?

Second thing I wanted to know is : Does one to one mapping create foreign-child key relation. As in above example I was expecting personid column inside address table should point to primary key of person table i.e id column. But it did not create any foreign key constraint on personid column of address table.

Am I missing something here

Upvotes: 0

Views: 221

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

Yes, you're missing something. The point of an ORM like hibernate is to be able to implement associations between objects.

Either make the association unidirectional, and remove the personId field from Address, or make it bidirectional, and replace the personId field by a person field, of type Person. In this case, one side of the association (the person.address side) will have to be the inverse of the other one (the adress.person side).

This is described in the Hibernate reference documentation. Read it. See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#assoc-unidirectional-121 for unidirectional one-to-one and http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#assoc-bidirectional-121 for bidirectional one-to-one.

Upvotes: 3

Related Questions