-->
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.  [ 8 posts ] 
Author Message
 Post subject: one-to-many mapping again
PostPosted: Mon Nov 17, 2003 3:52 am 
Newbie

Joined: Mon Nov 17, 2003 3:31 am
Posts: 6
Dear Sir,

I have encountered one-to-many mapping problem using hibernate. I have two table one called OneCXNUser and one called UserSMsisdn. These two table also have a field called Imsi and many records in table UserSMsisdn can be mapped to the table OneCXNUser using this fields.

When I can successfully add one record into the table OneCXNUser but nothing is added to the table UserSMsisdn. I have already read the document and add the cascade="all" to the onecxn.hbm.xml but still fail. The xml is as follows:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="com.primecreation.cxn.system.bean.OneCXNUser" table="ONECXN_USER">

<id name="imsi" type="string" unsaved-value="null" >
<column name="IMSI" sql-type="varchar(16)" not-null="true"/>
<generator class="assigned"/>
</id>


<property name="active">
<column name="ACTIVE" sql-type="number(1)" />
</property>

<set name="sMsisdn" table="USER_SMSISDN" cascade="all" >
<key column="IMSI"/>
<one-to-many class="com.primecreation.cxn.system.bean.UserSMsisdn" />
</set>
</class>

<class name="com.primecreation.cxn.system.bean.UserSMsisdn" table="USER_SMSISDN">

<composite-id>
<key-property name="secNetworkId" type="int">
<column name="SEC_NETWORKID" sql-type="number(2)" not-null="true"/>
</key-property>
<key-property name="sMsisdn" type="string">
<column name="SMSISDN" sql-type="varchar(16)" not-null="true"/>
</key-property>
</composite-id>


<property name="cliSetting">
<column name="CLI_SETTING" sql-type="number(1)" not-null="true"/>
</property>

<many-to-one name="oneCXNUser" column="IMSI" not-null="true"
class="com.primecreation.cxn.system.bean.OneCXNUser" />
</class>

</hibernate-mapping>


The Java code is as follows:

session = factory.openSession( conn );

UserSMsisdn sMsisdn = new UserSMsisdn();
sMsisdn.setSecNetworkId( 13 );
sMsisdn.setImsi( "123456789012345" );
sMsisdn.setsMsisdn( "8613006037474" );
sMsisdn.setCliSetting( 1 );


OneCXNUser user = new OneCXNUser();
user.setImsi( "123456789012345" );
user.setpMsisdn( "85212345678" );
user.setActive( 3 );
user.setLastVisitNetwork( 4 );
sMsisdn.setOneCXNUser( user );
Set set = new HashSet(10);
set.add( sMsisdn );
user.setsMsisdn( set );


tran = session.beginTransaction();
session.save( user );
tran.commit();

I notice that there is only one insert and two update instead of two insert.

Regards,
Au Yeung


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 4:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Try using inverse="true" and Read the relationships section of the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 5:12 am 
Newbie

Joined: Mon Nov 17, 2003 3:31 am
Posts: 6
david wrote:
Try using inverse="true" and Read the relationships section of the documentation.



I have added this but it only remove the update SQL but it still only add one record to the table OneCXNUser but not UserSMsisdn

Regards,
William


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 6:31 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
<set name="sMsisdn" table="USER_SMSISDN" cascade="all" >

should be
Code:
<set name="sMsisdn" cascade="all" >

since Hibernate already knows the table by the one-to-many class.

Since Msis... is a composite id, you need to help Hibernate wether to save or update the row.

set unsaved-value="any|none" and manually update|save if in the non-default case.

There are some comments on that issue in the reference doc (for assigned id, but it's the same pb).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 7:40 am 
Newbie

Joined: Mon Nov 17, 2003 3:31 am
Posts: 6
Since Msis... is a composite id, you need to help Hibernate wether to save or update the row.

set unsaved-value="any|none" and manually update|save if in the non-default case.

There are some comments on that issue in the reference doc (for assigned id, but it's the same pb).[/quote]


Thanks, it works.

But actually, I don't know what is the meaning of the attribute unsave-value in the composite-id and the tag id and the meaning of any, none, null, default, etc. May you explain it more??

Regards,
William


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 8:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Quote:
Hibernate users have requested a general purpose method that either saves a transient instance by generating a new identifier or update the persistent state associated with its current identifier. The saveOrUpdate() method now implements this functionality. Hibernate distinguishes "new" (unsaved) instances from "existing" (saved or loaded in a previous session) instances by the value of their identifier property. The unsaved-value attribute of the <id> mapping specifies which identifier values should be interpreted as representing a "new" instance.
The allowed values of unsaved-value are:
* any - always save
* none - always update
* null - save when identifier is null (this is the default)
*valid identifier value - save when identifier is null or the given value (eg "0", "1", etc...


For composite id, the list is restricted to any or none.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 9:25 pm 
Newbie

Joined: Mon Nov 17, 2003 3:31 am
Posts: 6
I still have the following questions

1. If I don't use the function, then the attribute unsaved-value doesn't have any effect because the document states that "The saveOrUpdate() method now implements this functionality."

2. And what is the meaning of "identity" in your explaination. You mean the Java Object attribute, the attribute in xxx.hbm.xml or even in the RDBMS field or other things.


3. In the document hibernate_reference section 7.4 page 50, titled updating object saved or loaded in the current session, it states that updating request both SQL select and update and therefore inefficient and so hibernate offers an alternate approach. But I want to know where is the alternate approach??

4. And I notice that when I delete the object, it also execute a SQL select statement first in the function session.load and then execute SQL delete, similar to update. Then it there also another approach to avoid this??

Regards,
William


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
williamay53 wrote:
1. If I don't use the function, then the attribute unsaved-value doesn't have any effect because the document states that "The saveOrUpdate() method now implements this functionality."

It is use when using saveOrUpdate().
Beware cascading strategy. Cascaded objects will always be saveOrUpdated wether the parent is saved or updated, unless you manually save or update the child before the parent.

williamay53 wrote:
2. And what is the meaning of "identity" in your explaination. You mean the Java Object attribute, the attribute in xxx.hbm.xml or even in the RDBMS field or other things.

All of them, they are all related.

williamay53 wrote:
3. In the document hibernate_reference section 7.4 page 50, titled updating object saved or loaded in the current session, it states that updating request both SQL select and update and therefore inefficient and so hibernate offers an alternate approach. But I want to know where is the alternate approach??

Answer is chapter 7.5

williamay53 wrote:
4. And I notice that when I delete the object, it also execute a SQL select statement first in the function session.load and then execute SQL delete, similar to update. Then it there also another approach to avoid this??

Chapter 7.5

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.