-->
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: Quesiton on first level cache.
PostPosted: Mon Oct 31, 2005 2:58 pm 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Hibernate version: 3.0

Mapping documents: NA

Code between sessionFactory.openSession() and session.close():
NA
Full stack trace of any exception that occurs:
NA
Name and version of the database you are using:
NA
The generated SQL (show_sql=true):
NA
Debug level Hibernate log excerpt:
NA

I have 2 entity objects A and B; A contains a foriegn key back to B so...

class B {
private int id
private String name
private String ss#
private Set As
}

class A {
private int id
private String streetName
private String postalCodes
private B b
}

<many-to-one name="B" class="com.B" fetch="select">
<column name="b_id" not-null="true" />
</many-to-one>

When I do the following the first level cache does not store the row from the database it stores what I set on the object, for example...

Session s = Session.current();
A a = (A)s.createQuery(from A where id = 12345).uniqueResult();

B b = new B();
b.setId(2);
a.setB(b);

a = (A)s.createQuery(from A where id = 12345).uniqueResult();
b = a.getB();

b.getName(); // this returns null
b.getSS#(); // this returns null


why is b returning null for name and ss#? I created a new B instance and set the id, shouldn't hibernate load the entity associated with id 2 into the cache. It seems hibernate is caching the newly created instance as is, meaning I set the id = 2, but did not set name and ss# which are returning as nulls when I call get.

Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 4:31 pm 
Newbie

Joined: Sun Oct 30, 2005 2:38 pm
Posts: 9
Location: Goodyear, AZ
Just a guess, but B is not a persistent instance. Instead of:
Code:
b.setId(2);

try:
Code:
s.load(b, new Integer(2));

If that doesn't do the trick, you might need to call s.flush() after a.setB().

HTH
Eric


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 4:36 pm 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
yep, that did the trick. I should have caught that B was a dettached object and either call get or load to atach it to the session. I thought hibernate given the that B's id was set would retrieve the row from the database and cache it.

Thanks!


Top
 Profile  
 
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.