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.