-->
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: NullPointerException in Hibernate Set
PostPosted: Thu Oct 21, 2004 1:19 pm 
Newbie

Joined: Wed Oct 15, 2003 10:24 pm
Posts: 10
After I call a commit to my transaction, I'm experiencing a problem where a NullPointerException is getting thrown from methods that accesses a lazily loaded Set. The exception is happening when the wrapped java.util.Set in net.sf.hibernate.collection.Set is accessed but appears to still be null.

Basically when I call a method like size or iterator on my set, the underlying code in Hibernate attempts to read the lazy loaded set, but when it comes back from the read the Set is still null and the exeption is thrown. If I remove the commit, all is well.

Thanks,
Paul


Hibernate version:
version 2.1.6

Mapping documents:

<class
name="eg.model.Task"
table="tbl_TASKX"
discriminator-value="-1"
>

<id
name="key"
column="TASK_ID"
type="java.lang.Long"
>
<generator class="native">
</generator>
</id>

<discriminator
column="CLASS"
type="int"
/>

<version
name="version"
type="int"
column="VERSION"
access="property"
unsaved-value="negative"
/>

<property
name="name"
type="java.lang.String"
column="NAME"
/>

<property
name="number"
type="java.lang.String"
column="NUMBER"
unique="true"
/>

<many-to-one
name="type"
class="eg.model.TaskType"
cascade="none"
outer-join="true"
column="TYPE_ID"
not-null="true"
/>

<many-to-one
name="parentSchedule"
class="eg.model.Schedule"
cascade="none"
outer-join="auto"
column="PARENT_SCHED_ID"
not-null="false"
/>

<property
name="status"
type="int"
column="STATUS"
/>

<property
name="sequence"
type="int"
column="SEQ"
not-null="true"
/>

<many-to-one
name="role"
class="eg.model.Role"
cascade="none"
outer-join="true"
column="ROLE_ID"
/>

<many-to-one
name="owner"
class="eg.model.CompositeUser"
cascade="none"
outer-join="true"
column="USR_ID"
/>

<property
name="actualStartDate"
type="java.util.Date"
column="ACTUAL_START"
/>

<property
name="actualEndDate"
type="java.util.Date"
column="ACTUAL_END"
/>

<property
name="actualDuration"
type="java.lang.Integer"
column="DRVD_ACTUAL_DURATION"
/>

<set
name="comments"
table="tbl_TASK_COMMENT"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>

<key
column="TASK_ID"
>
</key>

<many-to-many
class="eg.model.Comment"
column="COMMENT_ID"
outer-join="auto"
/>

</set>

<set
name="predecessors"
table="tbl_TASK_DEP"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>

<key
column="DEP_TASK_ID"
>
</key>

<many-to-many
class="eg.model.Task"
column="TASK_ID"
outer-join="auto"
/>

</set>

<property
name="scheduledDuration"
type="int"
column="SCHED_DUR"
/>

<property
name="scheduledStartDate"
type="java.util.Date"
column="DRVD_SCHED_START"
/>

<property
name="scheduledEndDate"
type="java.util.Date"
column="DRVD_SCHED_END"
/>

<property
name="isPartOfTemplate"
type="boolean"
column="IN_TEMPLATE"
/>
<subclass
name="eg.model.UploadReferenceTask"
discriminator-value="9"
>

</subclass>
<subclass
name="eg.model.UploadApprovalTask"
discriminator-value="3"
>


</subclass>
<subclass
name="eg.model.StandardTask"
discriminator-value="2"
>


</subclass>
<subclass
name="eg.model.ReviewTask"
discriminator-value="8"
>

</subclass>
<subclass
name="eg.model.ApprovalTask"
discriminator-value="4"
>

</subclass>

</subclass>
<subclass
name="eg.model.Process"
discriminator-value="-1"
>
<property
name="targetDuration"
type="int"
column="TARGET_DURATION"
/>

<set
name="schedules"
lazy="true"
inverse="false"
cascade="all-delete-orphan"
sort="eg.model.ScheduleSequenceComparator"
>

<key
column="PROCESS_ID"
>
</key>

<one-to-many
class="eg.model.Schedule"
/>
</set>

<set
name="referenceElements"
table="tbl_TASK_REF_ELEMENT"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>

<key
column="TASK_ID"
>
</key>

<element
column="ELEMENT_ID"
type="java.lang.Long"
not-null="false"
unique="false"
/>

</set>

<subclass
name="eg.model.CyclicProcess"
discriminator-value="-1"
>

<subclass
name="eg.model.ApprovalProcess"
discriminator-value="5"
>

<subclass
name="eg.model.RootApprovalProcess"
discriminator-value="7"
>
<property
name="elementId"
type="java.lang.Long"
column="ELEMENT_ID"
/>

<property
name="isTemplate"
type="boolean"
column="IS_TEMPLATE"
/>

<set
name="templateTaskTypes"
table="tbl_TEMPLATE_TYPE"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>

<key
column="TASK_ID"
>
</key>

<many-to-many
class="eg.model.TaskType"
column="TASK_TYPE_ID"
outer-join="auto"
/>

</set>

<set
name="templateRoles"
table="tbl_TEMPLATE_ROLE"
lazy="true"
inverse="false"
cascade="none"
sort="unsorted"
>

<key
column="TASK_ID"
>
</key>

<many-to-many
class="eg.model.Role"
column="ROLE_ID"
outer-join="auto"
/>

</set>

</subclass>

</subclass>

</subclass>
<subclass
name="eg.model.ProjectProcess"
discriminator-value="1"
>

<subclass
name="eg.model.RootProjectProcess"
discriminator-value="6"
>
<property
name="elementId"
type="java.lang.Long"
column="ELEMENT_ID"
/>

<property
name="isTemplate"
type="boolean"
column="IS_TEMPLATE"
/>

<set
name="templateTaskTypes"
table="tbl_TEMPLATE_TYPE"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>

<key
column="TASK_ID"
>
</key>

<many-to-many
class="eg.model.TaskType"
column="TASK_TYPE_ID"
outer-join="auto"
/>

</set>

<set
name="templateRoles"
table="tbl_TEMPLATE_ROLE"
lazy="true"
inverse="false"
cascade="none"
sort="unsorted"
>

<key
column="TASK_ID"
>
</key>

<many-to-many
class="eg.model.Role"
column="ROLE_ID"
outer-join="auto"
/>

</set>

</subclass>

</subclass>

</subclass>

</class>
<class
name="eg.model.Schedule"
table="tbl_SCHED"
discriminator-value="1"
>

<id
name="key"
column="SCHED_ID"
type="java.lang.Long"
>
<generator class="native">
</generator>
</id>

<discriminator
column="CLASS"
type="int"
/>

<version
name="version"
type="int"
column="VERSION"
access="property"
unsaved-value="negative"
/>

<property
name="sequence"
type="int"
column="SEQ"
not-null="true"
/>

<property
name="approvalFileId"
type="java.lang.Long"
column="APPROVAL_FILE_ID"
not-null="false"
/>

<property
name="status"
type="int"
column="STATUS"
/>

<many-to-one
name="role"
class="eg.model.Role"
cascade="none"
outer-join="true"
column="ROLE_ID"
/>

<many-to-one
name="owner"
class="eg.model.CompositeUser"
cascade="none"
outer-join="true"
column="USR_ID"
/>

<many-to-one
name="parentProcess"
class="eg.model.Process"
cascade="none"
outer-join="auto"
column="PROCESS_ID"
not-null="false"
/>

<set
name="comments"
table="tbl_SCHED_COMMENT"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>

<key
column="SCHED_ID"
>
</key>

<many-to-many
class="eg.model.Comment"
column="COMMENT_ID"
outer-join="auto"
/>

</set>

<set
name="tasks"
lazy="true"
inverse="false"
cascade="all-delete-orphan"
sort="eg.model.TaskSequenceComparator"
>

<key
column="PARENT_SCHED_ID"
>
</key>

<one-to-many
class="eg.model.Task"
/>
</set>

<property
name="scheduledStartDate"
type="java.util.Date"
column="SCHED_START"
not-null="false"
/>

<property
name="scheduledEndDate"
type="java.util.Date"
column="DRVD_SCHED_END"
/>

<property
name="projectedEndDate"
type="java.util.Date"
column="DRVD_PROJECTED_END"
/>

<property
name="scheduledDuration"
type="int"
column="DRVD_SCHED_DUR"
/>

<property
name="projectedDuration"
type="int"
column="DRVD_PROJ_DUR"
/>

<property
name="actualStartDate"
type="java.util.Date"
column="ACTUAL_START"
/>

<property
name="actualEndDate"
type="java.util.Date"
column="ACTUAL_END"
/>

<property
name="childStartedLate"
type="boolean"
column="CHILD_STARTED_LATE"
not-null="false"
/>

<property
name="nextChildDue"
type="java.util.Date"
column="NEXT_CHILD_DUE"
not-null="false"
/>

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
session.setFlushMode(FlushMode.AUTO);
Transaction transaction = session.beginTransaction();

Long processId = new Long(31118);
Process processTask = (Process)session.load( Task.class, processId);
String newName = "PJ Process X";
processTask.setName(newName);
transaction.commit();

Schedule schedule = null;

if (processTask.getSchedules().iterator().hasNext())
{

schedule = (Schedule) processTask.getSchedules().iterator().next();
}

if (schedule != null && schedule.getTasks() != null)
{
schedule.getTasks().size(); //This throws a null pointer
}


Top
 Profile  
 
 Post subject: NullPointerException in Hibernate Set - from query/load/get?
PostPosted: Thu Oct 21, 2004 9:47 pm 
Newbie

Joined: Thu Jul 29, 2004 10:54 am
Posts: 2
Location: Toronto, CANADA
We had a similar problem with the Hibernate Set was NOT initialized when it tried to use it and the size() method threw a NPE.

In our case (v2.1.3 but this is still an issue with v2.1.6) we were getting objects from a collection whose owner was loaded by query rather than by get or load. Once we added code to load the original item (to which the collection is attached) by key via get/load the exception went away. We did traced the problem to these lines of code in Loader.java where it can't figure out the owner of the collection in certain circumstances.

[code]
/**
* Read one collection element from the current row of the JDBC result set
*/
private void readCollectionElement(
final Object optionalOwner,
final Serializable optionalKey,
final ResultSet rs,
final SessionImplementor session)
throws HibernateException, SQLException {
final CollectionPersister collectionPersister = getCollectionPersister();
final Serializable collectionRowKey = (Serializable) collectionPersister.readKey(rs, session);
if (collectionRowKey!=null) {
if ( log.isDebugEnabled() ) log.debug( "found row of collection: " + MessageHelper.infoString(collectionPersister, collectionRowKey) );
Object owner = optionalOwner;
if (owner==null) {
owner = session.getCollectionOwner(collectionRowKey, collectionPersister);
if (owner==null) {
//TODO: This is assertion is disabled because there is a bug that means the
// original owner of a transient, uninitialized collection is not known
// if the collection is re-referenced by a different object associated
// with the current Session
//throw new AssertionFailure("bug loading unowned collection");
}
}
PersistentCollection rowCollection = session.getLoadingCollection(collectionPersister, collectionRowKey, rs);
if (rowCollection!=null) rowCollection.readFrom(rs, collectionPersister, owner);
}
else if (optionalKey!=null) {
if ( log.isDebugEnabled() ) log.debug( "result set contains (possibly empty) collection: " + MessageHelper.infoString(collectionPersister, optionalKey) );
session.getLoadingCollection(collectionPersister, optionalKey, rs); //handle empty collection
}
}
[/code]

_________________
\|/- Keith Mashinter, WCNA & AKME Solutions


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 27, 2004 4:33 pm 
Newbie

Joined: Sun Apr 18, 2004 11:12 pm
Posts: 3
Location: Waltham, MA
I am having a similar problem except that in my case I noticed that this is happening when using a many-to-one element in a composite id

unfortunately I can't do a get or load, I need the query to work.
are there other solutions?


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.