-->
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: Hibernate relationship collections ever null?
PostPosted: Tue May 26, 2009 6:28 pm 
Newbie

Joined: Tue Jun 14, 2005 3:21 pm
Posts: 9
We are seeing null collections in Hibernate relationships, and were wondering about the expected behavior for the collections.

So, for example, our members have a relationship to the groups they are in:

Member:
@OneToMany
@JoinTable(
name="GroupMember",
joinColumns=@JoinColumn(name="memberId"),
inverseJoinColumns=@JoinColumn(name="groupTypeId"))
public Set<GroupType> getGroupTypes() {
return groupTypes;
}

We would expect any Member object loaded from Hibernate to have a non null Set<GroupType>, but we are seeing that intermittently, the return Set<GroupType> is null, rather than an empty set.
When we persist the object, we don't bother to initialize those collections if they are empty.
From http://opensource.atlassian.com/project ... e/HHH-2369, I get the impression that Hibernate should always return an empty collection, per the ejb3 specification. Is this true? Is this true even if second level cache is enabled and Hibernate is reviving a saved object with a null collection? Are there any conditions under which an object loaded from Hibernate will return a null collection?


Top
 Profile  
 
 Post subject: Re: Hibernate relationship collections ever null?
PostPosted: Wed May 27, 2009 7:33 am 
Newbie

Joined: Wed May 27, 2009 7:01 am
Posts: 2
I'm not sure, but I think that when entity is loaded properties which are null in database are left with their default values (eg. values that were set by constructor). Do you initialize groupTypes to an empty set or do you let it stay null?


Top
 Profile  
 
 Post subject: Re: Hibernate relationship collections ever null?
PostPosted: Wed May 27, 2009 6:15 pm 
Newbie

Joined: Tue Jun 14, 2005 3:21 pm
Posts: 9
In case anyone else ever runs across this issue, here is what was happening:
What we were seeing was our own, created objects, reloaded from an existing Hibernate session that had stayed around between requests.
By default, if you fail to close a session for some reason, and it is still attached to a thread, Spring's OpenSessionInViewFilter will join the existing session, rather than creating a new one. We had a bug in our code that was causing us to leave sessions open, and the errors we were seeing were when a user would access a thread bound to an existing session and hit the stale objects.
To add even more key words, the reason we were failing to close sessions had to do with filter usage during Error handling inside our application. By default, filters don't apply to ERROR dispatched requests, you have to specify like this in your web.xml to get the filters to kick in:

<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>


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.