-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problems after loading and saving object with association.
PostPosted: Wed May 10, 2006 11:06 am 
Hallo developers, I got this strange error that i'm going to explain.

Hibernate Version: --> 3.1.3
Mapping documents: --> they are in the hibernate.zip linked
Code between sessionFactory.openSession() and session.close(): -->
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();   session.beginTransaction();            session.save(platformInfo);
session.getTransaction().commit();

Name and version of the database you are using: --> Oracle 10g


I got these objects:

PlatformInfo
ContainerProfileInfo

PlatformInfo has a many-to-many association with ContainerProfileInfo

PlatformInfo N----->M ContainerProfileInfo

I implemented these associations in standard way so in code:

Code:
public class PlatformInfo extends PlatformElement {
      String name; //key assigned   
      Collection<ContainerProfileInfo> containerProfiles = new HashSet<ContainerProfileInfo>();
      ...
   }


Code:
public class ContainerProfileInfo extends PlatformElement{   
      private String name; //key assigned      
      ...
   }   


When I create this object hierarchy "by hand" and i save the master object "PlatformInfo" i got what i expect on DB.

Code:
PlatformManager platformManager = new PlatformManager();
      
// Clean the DB
platformManager.deleteTables();
      
// Create the main bean
PlatformInfo platformInfo = new PlatformInfo(PLATFORM_KEY);
      
// Create the associated bean and assign it to the "father"
ContainerProfileInfo profileInfo = new ContainerProfileInfo("profile1");   
platformInfo.addContainerProfile(profileInfo);      
      
// Save the Bean
platformManager.store(platformInfo);         
System.out.println(platformInfo.toString());      


Then I load the just saved object from DB, delete records from DB and then i save it again.

Code:
// Load the "just saved" bean
PlatformInfo platformInfoLoaded = (PlatformInfo) platformManager.load(PlatformInfo.class, PLATFORM_KEY);
      
// Clean the DB and then resave it.... Here on DB i lose the association!!!
platformManager.deleteTables();
platformManager.store(platformInfoLoaded);      
System.out.println(platformInfoLoaded.toString());
      
// Reload the "just saved" bean and check with toString that is no more consistent
PlatformInfo info = (PlatformInfo) platformManager.load(PlatformInfo.class, PLATFORM_KEY);
System.out.println(info.toString());


Now even if the objects are the same (they hold same data) if i check on DB the association between PlatformInfo and the ContainerProfile
is no more available.


Maybe i'm making some errors, but i couldn't understand where.

Anyone could help me please?

I attach a zip file with 2 ant target:
build: build the jar
run: run the test

If you want to use it you have to configure hibernate-cfg.xml with your own DB settings.

For those who use Eclipse there's also the .project file, just open it and modify build path to point at lib folder.

http://www.lafiocavenmola.it/modules/uploader/index.php?action=downloadfile&filename=hibernate.zip

Thanks for your time

Kind Regards

Massimo



Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:[/code][url][/url]
Code:


Top
  
 
 Post subject:
PostPosted: Mon May 15, 2006 4:20 am 
Ok I found the problem.

It was hidden here : platformManager.deleteTables();

Code:
private void deleteTables() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();      
session.beginTransaction();   
session.createQuery("DELETE FROM Platform").executeUpdate();      
session.createQuery("DELETE FROM ContainerProfile").executeUpdate();
      session.getTransaction().commit();
      
}


In fact saving the objects and cleaning the DB in this way lead us to a inconsistent way.
Hibernate doesn't save again the objects associated since they are not marked as dirty.

So we loose the referenced objects (ContainerProfileInfo).

Maybe we didn't usa createQuery("...") in canonical way.

I hope this can be helpful for someone.


Top
  
 
 Post subject:
PostPosted: Mon May 15, 2006 5:05 am 
Ok I found the problem.

It was hidden here : platformManager.deleteTables();

Code:
private void deleteTables() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();      
session.beginTransaction();   
session.createQuery("DELETE FROM Platform").executeUpdate();      
session.createQuery("DELETE FROM ContainerProfile").executeUpdate();
      session.getTransaction().commit();
      
}


In fact saving the objects and cleaning the DB in this way lead us to a inconsistent state.
Hibernate doesn't save again the objects associated since they are not marked as dirty.

So we loose the referenced objects (ContainerProfileInfo).

Maybe we didn't use createQuery("...") in canonical way.

I hope this can be helpful for someone.


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