-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate deletes collection as soon as entity is loaded
PostPosted: Thu Aug 27, 2009 9:37 pm 
Newbie

Joined: Mon Apr 21, 2008 11:41 pm
Posts: 6
Hi All,

I have an entity with a map inside it. (Mapping to follow).

In a web app I have been using the entity with no problems. I have recently added a new action that loads the entity, but in this action (and not the other action!) when I load the entity it automatically deletes all from the mapped table!

I have recently changed the mapping from a single object to an abstract object with two empty subclasses. (This was to implement an auto-save feature, where you can save an entity in the background, and then do a proper save. If the app is shut down before you do a proper save, the app will see the autosave version and prompt to recover.)

The above works fine, the discriminator is working fine etc. The problem occurs when I try to load the autosaved version and copy its fields into the 'proper' version. Even without loading the autosave version, just loading the proper version will delete one of the maps.

This is the sql executed when the object is loaded:

-----the main entity
Hibernate: select //snip// from Assessment assessment0_ where assessment0_.AssessmentId=?

-- HERE IS THE MAP BEING LOADED
Hibernate: select //snip// from AssessedRisk assessedri0_ where assessedri0_.AssessmentId=?

-- another map, this one is not affected...
Hibernate: select //snip// from Answer answermap0_ where answermap0_.AssessmentID=?

--another map
Hibernate: select //snip// from InterventionDecision interventi0_, InterventionProgram interventi1_, InterventionCategory interventi2_ where interventi0_.AssessmentID=? and interventi0_.InterventionProgramID=interventi1_.InterventionProgramId(+) and interventi1_.InterventionCategoryID=interventi2_.InterventionCategoryId(+) order by interventi0_.DisplaySequenceNum

--yet another map
Hibernate: select //snip// from Interview interviewm0_ where interviewm0_.AssessmentId=? order by interviewm0_.InterviewDate

--- ????? WHY IS THIS HAPPENING?
Hibernate: delete from AssessedRisk where AssessmentId=?



Here is the mapping:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="au.gov.sa.dcs.ornirlocal.assessment.AbstractAssessmentEntity" table="Assessment"
discriminator-value=" " polymorphism="explicit">

<composite-id name="assessmentPK" class="au.gov.sa.dcs.ornir.primarykey.AssessmentPK">
<key-property name="value" column="AssessmentId" type="long" />
</composite-id>

<discriminator column="AssessmentTypeCode" type="string" force="true" />

<version name="versionNumAsLong" column="VersionNum" type="long" />

//SNIP//

<map name="answerMap" table="Answer">
<key>
<column name="AssessmentID" not-null="true" />
</key>
<composite-index class="au.gov.sa.dcs.ornir.primarykey.SurveyPartPK">
<key-property name="value" column="SurveyPartId" type="long" />
</composite-index>
<composite-element class="au.gov.sa.dcs.ornirlocal.assessment.AnswerValueImpl">
<nested-composite-element name="booleanValue" class="au.gov.sa.justice.framework.entity.datatype.BooleanDataType">
<property name="valueAsString" column="AnswerInd" type="string" />
</nested-composite-element>
<nested-composite-element name="answerValueLargeObjectPK" class="au.gov.sa.justice.commonlargeobjectlocal.largeobject.LargeObjectPK">
<property name="value" column="AnswerLargeObjectId" type="long" />
</nested-composite-element>
<nested-composite-element name="textValue" class="au.gov.sa.dcs.ornir.survey.StringAnswerDataType">
<property name="value" column="AnswerText" type="string" />
</nested-composite-element>
</composite-element>
</map>

<map name="assessedRiskMap" table="AssessedRisk">
<key>
<column name="AssessmentId" not-null="true" />
</key>
<composite-index class="au.gov.sa.dcs.ornir.primarykey.SurveyPartPK">
<key-property name="value" column="SurveyPartId" type="long" />
</composite-index>
<composite-element class="au.gov.sa.dcs.ornir.survey.Risk">
<property name="riskScore" column="RiskScore" type="int" />
<nested-composite-element name="riskLevel" class="au.gov.sa.dcs.ornir.survey.RiskLevel">
<property name="value" column="RiskLevelName" type="string" />
</nested-composite-element>
</composite-element>
</map>

<!-- the normal assessment entity -->
<subclass name="au.gov.sa.dcs.ornirlocal.assessment.AssessmentEntity" discriminator-value="NRML"/>

<!-- the auto-save entity -->
<subclass name="au.gov.sa.dcs.ornirlocal.assessment.AssessmentAutoSaveEntity" discriminator-value="AUTO">
<component name="parentAssessmentPK" class="au.gov.sa.dcs.ornir.primarykey.ParentAssessmentPK">
<property name="value" column="ParentAssessmentID" type="long" />
</component>
</subclass>

</class>

</hibernate-mapping>


In the AbstractAssessmentEntity, I am declaring the maps like this:

private Map<PrimaryKey, AnswerValue> answerMap = new HashMap<PrimaryKey, AnswerValue>();
private Map<PrimaryKey, Risk> assessedRiskMap = new HashMap<PrimaryKey, Risk>();

I used to initialise them in the constructor but moved them up to see if that made a difference.

Any clues??? Sorry for the long post, please request more info if required.

Thanks!!!


Top
 Profile  
 
 Post subject: Re: Hibernate deletes collection as soon as entity is loaded
PostPosted: Thu Aug 27, 2009 9:56 pm 
Newbie

Joined: Mon Apr 21, 2008 11:41 pm
Posts: 6
I have made some progress. I was using a subclass of AssessmentPK called ParentAssessmentPK to load the assessment. For some reason, it loaded the assessment fine, but deleted the maps.... I created a new AssessmentPK and used that to load the 'real' entity and it seemed to preserve the map.

However, at various point in the process flow it is still deleting the contents of that table. So the issue still remains.


Top
 Profile  
 
 Post subject: Re: Hibernate deletes collection as soon as entity is loaded
PostPosted: Thu Aug 27, 2009 10:47 pm 
Newbie

Joined: Mon Apr 21, 2008 11:41 pm
Posts: 6
Hibernate: delete from Answer where AssessmentID=?
Hibernate: delete from AssessedRisk where AssessmentId=?
Hibernate: delete from Assessment where AssessmentId=? and VersionNum=?

Ok so it's deleting all of the maps when I load it after a save now. I simply save the entity and then load it back out again (so that hibernate can order the lists) and it deletes all the collections. What's going on?


Top
 Profile  
 
 Post subject: Re: Hibernate deletes collection as soon as entity is loaded
PostPosted: Thu Aug 27, 2009 10:59 pm 
Newbie

Joined: Mon Apr 21, 2008 11:41 pm
Posts: 6
I think I have resolved it... there were other places I was using a different PK class. When I construct a new PK class based on the id from the extending PK class, it seems to all work fine.

Fingers crossed!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.