-->
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: Refresh Not Loading Collections
PostPosted: Tue Nov 02, 2010 3:41 am 
Newbie

Joined: Fri Oct 15, 2010 12:39 am
Posts: 6
Can somebody please explain to me why a refresh might not load a child collection.

My code is like:

session.save(student)
transaction.commit()
session.refresh(account)

and the student is not in the refreshed account

From account.java
Code:
   @OneToMany(mappedBy="account", fetch=FetchType.EAGER)
   public Set<Student> getStudentSet() {
      return studentSet;
   }

   public void setStudentSet(Set<Student> studentSet) {
      this.studentSet = studentSet;
   }


From student.java
Code:
   @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumn (name="accountId")
   public Account getAccount() {
      return account;
   }
   public void setAccount(Account account) {
      this.account = account;
   }


SQL:
Code:
Hibernate: insert into person (dob, email, firstName, isMale, lastName, note) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into student (accountId, grade, school, personId) values (?, ?, ?, ?)
Hibernate: update account set balance=? where accountId=?
Hibernate: select account0_.accountId as accountId0_0_, account0_.balance as balance0_0_ from account account0_ where account0_.accountId=?


Top
 Profile  
 
 Post subject: Re: Refresh Not Loading Collections
PostPosted: Tue Nov 02, 2010 2:20 pm 
Newbie

Joined: Fri Oct 15, 2010 12:39 am
Posts: 6
Anybody? Do I need to specify more info. Is this a dumb question? Would really appreciate a response.


Top
 Profile  
 
 Post subject: Re: Refresh Not Loading Collections
PostPosted: Tue Nov 02, 2010 3:58 pm 
Newbie

Joined: Fri Oct 15, 2010 12:39 am
Posts: 6
Figured it out. Had to load the account first before refreshing. Makes sense, but is certainly not intuitive to a new user. I tried using session.load(account.class, accountId) before I discovered refresh, only to find out that load() does not load associated collections. Why? Then I tried using an example criteria only to find out that example criteria ignore id properties, so I was getting back a list of all accounts. Why? Finally, since do not have the name of the id property at runtime without the use of the Reflection API, (overkill), my only option was refresh.

My code above should have been:

session.save(student);
session.flush();
account = session.load(account.class, accountId);
session.refresh(account);
transaction.commit()

Man, this is one hell of a learning curve!


Top
 Profile  
 
 Post subject: Re: Refresh Not Loading Collections
PostPosted: Tue Nov 02, 2010 10:40 pm 
Newbie

Joined: Fri Oct 15, 2010 12:39 am
Posts: 6
Well, I guess I'm not out of the woods on this one. Getting ready to give up. After many many wasted hours on this issue, (with no help from anyone on this forum...), I've now verified that I am correctly specifying a class object and id to a call to session.load(), and for some reason I receive back an object of the correct type, but with a null id and no properties set. What the heck...

Code is like:

account = session.load(account.class, accountId);

After which, my account object has a null id, with no members in its collections and none of its properties set. How is this possible? I thought at the very least, it would throw me an error... Anyone? Or am I still talking to myself? This is by far, the most frustrating coding experience I've ever encountered.


Top
 Profile  
 
 Post subject: Re: Refresh Not Loading Collections
PostPosted: Wed Nov 03, 2010 1:40 am 
Newbie

Joined: Fri Oct 15, 2010 12:39 am
Posts: 6
Just wanted to say thanks for all the help from the Hibernate Team and all the very informative replies to this post!! Everyone has been very helpful. There's no way that I ever would have guessed by the documentation in the API Reference that session.load() just returns an uninitialized proxy and does not actually hit the database until you invoke a method of the proxy. That just isn't the type of behavior a new user would expect. Thank goodness for page 293 of the 400 page manual. Now I understand how Hibernate is such a time saver. It only took a couple hours to figure out why the following fails:

session.load(account.class, accountId)
session.refresh(account)

It needs to be:

session.get(account.class, accountId)
session.refresh(account)

Everywhere I looked, the only difference stated between the two methods was that load() will throw an error if a row with the given id is not matched. Everywhere except page 293...


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.