These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Appropriate Mapping for lookup data
PostPosted: Mon Oct 09, 2006 1:55 pm 
Newbie

Joined: Wed Jul 12, 2006 11:30 am
Posts: 6
Hi,

In my application, I have a bunch of reference data tables. With reference data I mean the read only data for the application. I have defined an object model for this reference data. The non reference data maps to these objects with Many-to-Many relationships and every thing works fine.
But the low level analysis of the code (through SQL Debug) shows that while inserting non reference data one insert statement and multiple update statement (one for each mapped reference data) is executed.

Let us take an example:
I have two reference data tables:
Table - RefA
REF_A_ID PK
REF_A_NAME
REF_A_VAL1
REF_A_VAL2

Table - RefB
REF_B_ID PK
REF_B_NAME
REF_B_VAL1
REF_B_VAL2

and one application table
Table Tab1
TAB_1_ID
TAB_1_NAME
TAB_1_REF_A
TAB_1_REF_B

The mapping file for this example is like
<class name="RefA" table="REFA">
<id name="refAId" column="REF_A_ID"/>
<property name="name" column="REF_A_NAME"/>
<property name="value1" column="REF_A_VAL1"/>
<property name="value2" column="REF_A_VAL2"/>
</class>
<class name="RefB" table="REFB">
<id name="refBId" column="REF_B_ID"/>
<property name="name" column="REF_B_NAME"/>
<property name="value1" column="REF_B_VAL1" />
<property name="value2" column="REF_B_VAL2"/>
</class>
<class name="Tab1" table="TAB1">
<id name="Tab1Id" column="TAB_1_ID"/>
<property name="name" column="TAB_1_NAME"/>
<many-to-one name="refA" column="TAB_1_REF_A"/>
<many-to-one name="refB" column="TAB_1_REF_B"/>
</class>

Now when I try to save a new Tab1 object, I see three statements being executed -
1. Insert Statement inserting the a new row in Tab1. The values inserted in columns TAB_1_REF_A and TAB_1_REF_B are null.
2. Update statement modifying the just inserted row by setting the value for column TAB_1_REF_A
3. Update statement modifying the same row by setting the value for column TAB_1_REF_B

This kind of makes sense as property refA and refB are mappings. However, these are just read only mapping. Is there a way for me to control this behavior and ensuring that the complete operation is executed as one insert statement?

One approach that comes to my mind is to break the relationships and use property instead -
<class name="Tab1" table="TAB1">
<id name="Tab1Id" column="TAB_1_ID"/>
<property name="name" column="TAB_1_NAME"/>
<property name="refA" column="TAB_1_REF_A"/>
<property name="refB" column="TAB_1_REF_B"/>
</class>
The business layer in this case will be responsible for fetching the corresponding RefA and RefB objects. The RefA and RefB are read-only data and can be cached.
Though this approach gives performance benefits, it creates inconsistencies in Object Model.

Do we have any other approach for handling this situation?

Thanks.
Amit


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 3:51 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
I believe the quick fix for this is to set mutable to false on your read only class mappings:
Code:
<class .... mutable="false" ....>

Relevant Documentation:
http://www.hibernate.org/hib_docs/v3/re ... tion-class


However this will just be masking the problem more than anything else, hibernate will only do the update if it thinks the Object in the referenced table has changed. Usually this is the result of getters that don't return exactly what was set on the corresponding setter.

Without knowing the details of your read only classes I can't really be more specific than that, but watch out for things like null strings changed to empty string (or vica-versa).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 5:51 pm 
Newbie

Joined: Wed Jul 12, 2006 11:30 am
Posts: 6
Thanks for the response EdC.

Important thing here is that code is not trying to update the read only classes.

What I am referring to is that when I insert a new row in Tab1, three operations are executed in Tab1 table only.
First inserts a new row in Tab1 with ID and name.
Second updates this row by adding TAB_1_REF_A and
Third updates the same same row by adding TAB_1_REF_B.

Even if read-only classes are immutable, this behavior does not change.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 5:52 pm 
Newbie

Joined: Wed Jul 12, 2006 11:30 am
Posts: 6
Thanks for the response EdC.

Important thing here is that code is not trying to update the read only classes.

What I am referring to is that when I insert a new row in Tab1, three operations are executed in Tab1 table only.
First inserts a new row in Tab1 with ID and name.
Second updates this row by adding TAB_1_REF_A and
Third updates the same same row by adding TAB_1_REF_B.

Even if read-only classes are immutable, this behavior does not change.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.