-->
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.  [ 10 posts ] 
Author Message
 Post subject: Bidirectional relations are not updated inside transaction
PostPosted: Thu Oct 23, 2003 2:11 pm 
Newbie

Joined: Fri Sep 26, 2003 7:08 am
Posts: 7
Hello all!

I'm using Hibernate with MySql DB server, which maintains tables in InnoDB mode, i.e transaction support is on.
I have two object with parent-child relationship: Model and Entity.
Here are fragments of Model.hbm.xml:
Code:
...
      <id column="MODEL_ID" name="id" type="java.lang.Long">
         <generator class="native"/>
      </id>
...
    <bag name="entities" inverse="false" lazy="false" cascade="all">
         <meta attribute="use-in-tostring">true</meta>
         <key column="MODEL_ID"/>
         <one-to-many class="com.XXXXX.Entity"/>
    </bag>

Here are fragments of Entity.hbm.xml:
Code:
...
      <id column="ENTITY_ID" name="id" type="java.lang.Long">
         <generator class="native"/>
      </id>
...
      <property column="MODEL_ID" length="11" name="modelId" not-null="false" type="java.lang.Long">
         <meta attribute="use-in-tostring">true</meta>
      </property>

So, Model can have a number of Entities-children.
Problem is following. I begin transaction like that:

Code:
    Session session = null;
    Transaction tx = null;
      try {
      session = manager.obtainSession();
      tx = session.beginTransaction();

than pass newly created Model object with collection of Entities to create method. After it I use session.load(classname, ID) to receive my Model object back. I expect all auto-generated ID fields to be set by proper values. But effect is partial - all ID fields are really set to proper values - either in Model(parent) or in Entitiy(children). BUT! Field modelId in Entity(child) remains NULL, but I expect it to be set to Model Id value.
If I commit session - all became proper (after re-reading from DB using another session). But I need these values inside of initial session! Searching for solution of description of such problem has no success.
Is this a bug, or feature, or maybe I do something wrong?
Thanks in advance for help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 3:20 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
Wouldn't you want a

Code:
<many-to-one
         name="model"
         class="Model"
         column="MODEL_ID"
         not-null="true"
         />

instead of

[code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 5:10 pm 
Newbie

Joined: Fri Sep 26, 2003 7:08 am
Posts: 7
cutie wrote:
Wouldn't you want a

Code:
<many-to-one
         name="model"
         class="Model"
         column="MODEL_ID"
         not-null="true"
         />

instead of

[code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 5:29 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
did you try flushing the session after saving the new instance and before reloading it? Hibernate is able to generate ids without hitting the database with its hilo algorithm but anything generated by the database wont get set until the database is hit, which only happens when the session is flushed/closed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 5:32 pm 
Newbie

Joined: Fri Sep 26, 2003 7:08 am
Posts: 7
cutie wrote:
did you try flushing the session after saving the new instance and before reloading it? Hibernate is able to generate ids without hitting the database with its hilo algorithm but anything generated by the database wont get set until the database is hit, which only happens when the session is flushed/closed.

I cannot flush session, because I have some other task to do with this objects - Model etc... This task requires full objects - with all ID set, but if this task fails - I should have ability to rollback changes made to DB...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 6:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
polar wrote:
I cannot flush session, because I have some other task to do with this objects

So what ? you can continue using thoses objects after session.flush().

polar wrote:
but if this task fails - I should have ability to rollback changes made to DB...

That's the purpose of tx.rollback().

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 6:40 pm 
Newbie

Joined: Fri Sep 26, 2003 7:08 am
Posts: 7
epbernard wrote:
polar wrote:
I cannot flush session, because I have some other task to do with this objects

So what ? you can continue using thoses objects after session.flush().

polar wrote:
but if this task fails - I should have ability to rollback changes made to DB...

That's the purpose of tx.rollback().


I'll try this tomorrow.. We have 1.40am now :-) But AFAIR from Hibernate Javadoc - flush method should be used directly before session.close()...
But I'll try .
Tnx


Top
 Profile  
 
 Post subject: Nothing happend :-(
PostPosted: Fri Oct 24, 2003 3:56 am 
Newbie

Joined: Fri Sep 26, 2003 7:08 am
Posts: 7
Nothing happend using session.flush() :-( Effect remain - no parent ID is set in children objects.
Can anyone tell me how to omit such situation?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2003 7:12 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
I won't give the reasons why I have something similar, but suffice to say, I think I know what you need to do in your code.

As you're not using inverse, there is a way to get around this, but it involves not using cascade.

1) Remove cascade="all" from your bag.

2) Then, in your code, do something like this (which is really the non-cascade way the reference material talks about):

Code:
...
session.save(model);

List entities = model.getEntities();
For (int i = 0; i < entities.size(); i ++) {
    Entity entity = (Entity) entities.get(i);
    entity.setModelId(model.getModelId);  //manually setting Foreign Key
    //this bit may not be needed.....
    entity.setModel(model);
    model.getEntities.remove(i);
    model.getEntities.add(entity);
    //.....
    session.save(entity);
}
...

As I said, this is a workaround, where you manually set the foreign key for each child. Try this, and perhaps it works.

-G


Top
 Profile  
 
 Post subject: Almost done
PostPosted: Fri Oct 24, 2003 10:49 am 
Newbie

Joined: Fri Sep 26, 2003 7:08 am
Posts: 7
This is very rude decision :-) I've almost done using session.refresh(). After using session.update() I need just use refresh() to every entity in order to get modelId set (iterate through model.getEntities()). And do so with every entity collection - I have three of them :-(. It is not so beatiful decision, but it is IMHO better than "hands" one.
Iteration through children must be done because refresh does not use cascades (it could be seen by swtching "show sql" property in hibernate.properties to true).
Thanks to everyone for help


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