-->
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: My Cache Does Not Reflect What Is In The Database.
PostPosted: Thu Dec 01, 2005 2:15 pm 
Beginner
Beginner

Joined: Tue Sep 20, 2005 11:01 am
Posts: 25
Hibernate version: 2.1

I have two database tables and they have a parent-child relationship. These tables are called PERSON and ADDR. There can be 0 - many ADDR records for each PERSON record.

Code:
PERSON
   PERSON_ID      NUMBER
   NAME         VARCHAR(50)

ADDR
   ADDRESS_ID      NUMBER
   PERSON_ID      NUMBER
   ADDRESS_LINE    VARCHAR(50)


In the Person.hbm.xml file I have a set called addrs and I specified "inverse=true".

I have code that looks like the following:

Code:
Session hSession = HibernateUtil.getSession();
Person person = new Person();
person.setName("John Doe");
hSession.saveOrUpdate (person);
hSession.flush ();

Addr address = new Addr();
address.setPerson(person);
address.setAddressLine("Washington DC"):
hSession.saveOrUpdate(address);
hSession.flush();

address = new Addr();
address.setPerson(person);
address.setAddressLine("New York City"):
hSession.saveOrUpdate(address);
hSession.flush();


The problem I have is after the above code is run the value of
person.getAddrs() is null. In other words, even though I added the two Addr objects to the database that is not reflected in the addrs Set of the Person object.

Can someone tell me what the problem is here? Can it be rectified by a change in the hbm.xml file? Will I need to restructure my code to support this?

Thanks in advance,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 2:50 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Try committing the transaction. ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 3:04 pm 
Beginner
Beginner

Joined: Tue Sep 20, 2005 11:01 am
Posts: 25
So you're saying I need to use a Transaction object and perform a commit before the parent object will reflect the fact that I have added child records, right? Currently I am not using a Transaction object but I could change my code to utilize one.

I thought a Session.saveOrUpdate() and then a Session.flush() would take care of that. If performance is not an issue is there any reason not to do a Transaction.commit() after every Session.flush()?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 3:15 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://hibernate.org/42.html
http://hibernate.org/43.html

And the reference documentation...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 4:09 pm 
Beginner
Beginner

Joined: Tue Sep 20, 2005 11:01 am
Posts: 25
OK, what I take from the referenced documentation is, assuming I do not use auto-commit, I should always use beginTransaction() and commit().

To be honest I still don't quite understand why this will take care of my issue. When I look at the API documentation for the Transaction.commit() I see that commit "flushs the associated Session and ends the unit of work." Is the key here "ends the unit of work"?

My situation is a little more complicated than I original stated. The code sample I provided is actually a small part of a larger Transaction. A higher level view of my code is:

Code:
try {
    Session hSession = HibernateUtil.getSession();

    hSession.beginTransaction();

    <do a bunch of things ....>

    <perform code sample below >

    < do a buch more things ... >

    hSession.getTransaction().commit();
}
catch (Exception e) {
    hSession.getTransaction().rollback();
}


The point I want to make is that if I run into a probem and need to rollback I need to rollback a lot more than the code sample provided below. Can I do multiple commits for each beginTransaction()? If so, and I have to do a rollback, will I rollback to the beginTransaction() or to the last commit()?

I appreciate your help. :)


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.