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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate calls update (rather than save) on children.
PostPosted: Fri Jul 07, 2006 1:30 pm 
Newbie

Joined: Fri Jul 07, 2006 10:06 am
Posts: 5
I am using Hibernate 1.3.1 in Spring. My database is Oracle 10g express.

I have the following object tree:

Portfolio {id, name, List<Positions> positions}

Position {id, symbol, nShares}

Here is the mapping file:

<hibernate-mapping auto-import="true" default-lazy="false">
<class name="Portfolio">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<list name="positions" inverse="false" cascade="all">
<key column="pos_id" not-null="true"/>
<list-index column="indx"/>
<one-to-many class="Position"/>
</list>
</class>
<class name=”Position”>
<id name="id">
<generator class="native"/>
</id>
<property name="symbol"/>
<property name=”nShare”/>
</class>
</hibernate-mapping>

The test code:
Test() {
Position pos = new Position();
pos.setId(1000L);
pos.setSymbol("bar");
pos.setNSahres(100);

Protfolio port = new Portfolio();
port.setId(1000L);
port.setName("port");
List<Positions> positions = new ArrayList<Position>(1);
positions.add(pos);
port.setIPositions(positions);
System.out.println("Test before save: " + port);
getHibernateTemplate().save(prod);
System.out.println("Test after save: " + port);
}

The output:
Test before save: Portfolio (port, 1000, [Position(bar, 1000, 100)]).

Test before save: Portfolio (port, 3, [Position(bar, 1000, 100)]).

As you can see that portfolio id has been changed after save while position id has not.

The table position is empty.

I found from trace that save() was called on portfolio but update() was called on positions.

If it called save() on both parent and child, I should not have seen the problem.

How should I define map files in order to achieve this behavior?

Thanks in advance,

Paul


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 1:49 pm 
Newbie

Joined: Tue Apr 19, 2005 3:16 pm
Posts: 18
Hi, Paul. Are those mappings correct? You've got your position list referencing a column "pos_id", but both of the classes declare their id columns as simply "id". If Portfolio's ID column is "id", and Position's ID column is "pos_id" with a foreign key "portfolio_id", then your key column reference in Portfolio should be "portfolio_id".

Did I misunderstand your example?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 2:17 pm 
Newbie

Joined: Fri Jul 07, 2006 10:06 am
Posts: 5
Hi Kintar,

I modified the mapping as follows:

<class name="Portfolio">
<id name="id" column="port_id"> <!-- id to port_id -->
<generator class="native"/>
</id>
<property name="name"/>
<list name="positions" inverse="false" cascade="all">
<key column="pord_id" not-null="true"/> <!-- pos_id to port_id -->
<list-index column="indx"/>
<one-to-many class="Position"/>
</list>
</class>
<class name=”Position”>
<id name="id" column="pos_id"> <!-- id to pos_id -->
<generator class="native"/>
</id>
<property name="symbol"/>
<property name=”nShare”/>
</class>

Now the table definition seems better:

CREATE TABLE POSITION (
POS_ID NUMBER(19,0) PRIMARY KEY,
SYMBOL VARCHAR2(255),
NSHARES NUMBER(10, 0),
PROD_ID NUMBER(19,0) NOT NULL,
INDX NUMBER(10,0)
)

But I still have the same problem. POSITION table is not populated.

Any idea?

Thanks, Paul


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 3:04 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 2:43 pm
Posts: 29
Location: Silicon Valley
In your mapping you declare both ids <generator class="native"/>, but in your tests you are assigning the ids. This fools Hibernate into thinking your objects are already persistent. For new instances, set id to 0, then let Hibernate assign the id once the object is saved. (If you are using the value 1000L as code for an unsaved object, declare unsaved-value="1000" in your mapping.)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 3:11 pm 
Newbie

Joined: Fri Jul 07, 2006 10:06 am
Posts: 5
Hello Frusso,


Specifying unsaved-value in mapping file resolved my problem.

Thank you very much,

Paul


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