-->
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.  [ 1 post ] 
Author Message
 Post subject: Using a named query and a loader to map a OneToMany Set
PostPosted: Fri Nov 21, 2008 4:50 pm 
Newbie

Joined: Thu May 03, 2007 4:32 pm
Posts: 3
I am attempting to use a named query in hibernate version 3.2.4.sp1 to load a Set of objects in a POJO. When I do so, I get the following error:

java.lang.NullPointerException at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:333)

I have traced the problem to the fact that for standard OneToMany mappings, hibernate uses a org.hibernate.loader.collection.OneToManyLoader. This class properly creates the "set" object that is wrapped by PersistentSet. However for OneToMany mappings that are loaded via a named query as I have configured below, hibernate is using a org.hibernate.persister.collection.NamedQueryCollectionInitializer. When I run a debugger, I can see that the correct rows are pulled back from the database but then the "set" object is never created. This results in the NullPointerException.

The relevant code is below. I'm hoping that I've got something annotated incorrectly and that someone can just tell me what I need to change in my annotations. If this is not possible, I can always execute the named query via a DAO object, but I'd rather use a mapping so that the call to load the Set<Restriction1> doesn't have to be explicit.

Thanks,
Jay

Code:
/**
* POJO representation of an asset
*/
@Entity
@Table(name="asset")
@NamedNativeQuery
   ( name = "getDescendantRestrictions",
     query = "SELECT restriction_id_seq AS restriction_id_seq, " +
         "       related_asset_id_seq AS asset_id_seq, " +
         "       restriction_type_id_seq AS restriction_type_id_seq " +
         "FROM TABLE(restriction_lower_hier(:assetID))",
     resultClass = Restriction1.class )
public class Asset1 extends BaseObject
{
   Long assetID;
   Set<Restriction1> descendantRestrictions;

   @Id
   @Column(name="ASSET_ID_SEQ",nullable = false,unique=true)
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ASSET_ID_SEQ")
   @SequenceGenerator(name="ASSET_ID_SEQ", allocationSize=1, sequenceName="asset_id_seq")   
   public Long getAssetID() {
      return assetID;
   }
   public void setAssetID( Long assetID ) {
      this.assetID = assetID;
   }

   @OneToMany( mappedBy = "asset", fetch = FetchType.LAZY )
   @Loader( namedQuery = "getDescendantRestrictions" )   
   public Set<Restriction1> getDescendantRestrictions() {
      return descendantRestrictions;
   }
   public void setDescendantRestrictions(Set<Restriction1> descendantRestrictions) {
      this.descendantRestrictions = descendantRestrictions;
   }
}


I can provide the Restriction1 class if necessary.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.