Five ‘s Weblog

October 18, 2007

uni-directional or bidirectional ?

Filed under: ORM — by powerdream5 @ 10:52 pm
Tags: , , , ,

        When mapping the domain model to the database schema,  perhaps we often ask ourselves one question: Making a one-to-many relationship uni-directional or making it bidirectional, which is better in performance. Today, I am going to show an example to demonstrate the difference between uni-direction and bidirection. I am using Hibernate framework.

        There is a class named Consumer and a class named CreditAccount, both of which mapped to the consumer and credit_account table respectively. It is obvious that each Consumer has more than one credit account, therefore, the relationship between the two classes is one-to-many. Firstly, I make the relationship uni-directional.

10_18_2_2007.jpg 

the mapping files are listed in the following:

//Consumer.hbm.xml
<hibernate-mapping>
          <class name=”Consumer” table=”consumer”>
                 <id name=”id” column=”CONSUMER_ID”>
                       <generator class=”native” />
                </id>
               <property name=”firstName” type=”string” column=”FIRSTNAME”/>
               <property name=”lastName” type=”string” column=”LASTNAME”/>
               <set name=”creditAccount” table=”credit_account” cascade=”all”>
                      <key column=”CONSUMER_ID”/>
                      <one-to-many class=”CreditAccount” />
              </set>
           </class>
</hibernate-mapping>

//CreditAccount.hbm.xml
<hibernate-mapping>
          <class name=”CreditAccount” table=”credit_account”>
                 <id name=”id” column=”ACCOUNT_ID”>
                         <generator class=”native” />
                 </id>
                 <property name=”accountNumber” type=”string” column=”ACCOUNT_NUMBER”/>
          </class>
</hibernate-mapping>

We try to insert an new account into the database, from the comman window or log files, we can see that two sql setences are executed. There are:
insert into credit_account(ACCOUNT_NUMBER) values(?)
update credit_account set CONSUMER_ID=? where ACCOUNT_ID=?

Now, I am going to make a little change to the CreditAccount class to make the relationship between Consumer and it bidirectional.  Therefore, I also need to change the mapping files. After being changed, they should be:

//Consumer.hbm.xml
<hibernate-mapping>
          <class name=”Consumer” table=”consumer”>
                 <id name=”id” column=”CONSUMER_ID”>
                       <generator class=”native” />
                </id>
               <property name=”firstName” type=”string” column=”FIRSTNAME”/>
               <property name=”lastName” type=”string” column=”LASTNAME”/>

               //make sure to set “inverse=true”
               <set name=”creditAccount” table=”credit_account” cascade=”all” inverse=”true”>
                      <key column=”CONSUMER_ID”/>
                      <one-to-many class=”CreditAccount” />
              </set>
           </class>
</hibernate-mapping>

//CreditAccount.hbm.xml
<hibernate-mapping>
          <class name=”CreditAccount” table=”credit_account”>
                 <id name=”id” column=”ACCOUNT_ID”>
                         <generator class=”native” />
                 </id>
                 <property name=”accountNumber” type=”string” column=”ACCOUNT_NUMBER”/>
                 <many-to-one name=”consumer” class=”Consumer” column=”CONSUMER_ID”>
          </class>
</hibernate-mapping>

Now, if we insert a new account into the database, only one sql sentence will be executed:
insert into credit_account(ACCOUNT_NUMBER,CONSUMER_ID) values(?, ?)

       There is no doubt that bidirection is better than uni-direction in the performance. If the relationship is uni-direction, only one end is going to maintain their relationshop. The other end cannot see any change of the relationship.  Therefore, the first time we need to execute two sql sentences, the first sql sentence inserts the data into the credit_account table (at this time, the system doesn’t know this account belongs to whom), and the second sql help the credit_account table build its relationship with the consumer table. However, both the end of the bidirectional relationship are aware of any changes, therefore, only one sql sentence can finish two tasks: inserting data and mataining the relationship.

        You can download the source files.

10_18_1_2007.jpg

Blog at WordPress.com.