-->
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.  [ 7 posts ] 
Author Message
 Post subject: Removing object from Set does not remove from DB
PostPosted: Wed May 19, 2004 9:26 am 
Newbie

Joined: Wed Sep 17, 2003 1:31 am
Posts: 7
Location: tampa, FL
I have a persistent object with a many-to-one collection. Upon retrieving this object from the db I'm wanting to remove objects from the Set and add new ones as appropriate. I was reading in 14.1.4 of the hib documentation about being able to dereference the collection (Set) on the persistent object in order to allow all recs to be deleted, then create a new Set and put the new objects to be persisted in there and then go about a saveOrUpdate() on the parent persistent object. In my test I have one object in the Set - I then go about dereferencing the Set (setting it to null in the parent object), instantiating a new HashSet, and then adding two new objects to the Set, then doing a saveOrUpdate() on the parent. What happens is the two additional rows are written to the db and I see a constraint violation because it's attempting to re-add the first row. Doesn't look like the deletes are happening. How should this be achieved?

Hibernate version is: 2.1.2
Database: Oracle 8i 8.1.7


Stack trace:

19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 1, SQLState: 23000
19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 1, SQLState: 23000
19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] ERROR net.sf.hibernate.util.JDBCExceptionReporter - [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (INDPFP.XAK1PORTFOLIO_PARTY) violated

19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] ERROR net.sf.hibernate.util.JDBCExceptionReporter - [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (INDPFP.XAK1PORTFOLIO_PARTY) violated

19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 1, SQLState: 23000
19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 1, SQLState: 23000
19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] ERROR net.sf.hibernate.util.JDBCExceptionReporter - [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (INDPFP.XAK1PORTFOLIO_PARTY) violated

19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] ERROR net.sf.hibernate.util.JDBCExceptionReporter - [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (INDPFP.XAK1PORTFOLIO_PARTY) violated

19-May-2004 09:11:20 [org.junitee.runner.TestRunner@1399f26] ERROR net.sf.hibernate.util.JDBCExceptionReporter - Could not execute JDBC batch update
weblogic.jdbc.base.BaseBatchUpdateException: [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (INDPFP.XAK1PORTFOLIO_PARTY) violated

at weblogic.jdbc.base.BasePreparedStatement.executeOneStatementAtATime(Unknown Source)
at weblogic.jdbc.base.BaseStatement.executeBatchEmulation(Unknown Source)
at weblogic.jdbc.base.BasePreparedStatement.executeBatch(Unknown Source)
at weblogic.jdbc.wrapper.PreparedStatement_weblogic_jdbc_base_BasePreparedStatement.executeBatch(Unknown Source)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2385)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2335)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at com.pwc.us.pfp.firmportfolio.datagateway.hibernate.HibFirmPortfolioDataGateway.updateFirmPortfolio(HibFirmPortfolioDataGateway.java:153)
at com.pwc.us.pfp.firmportfolio.services.FirmPortfolioServicesBean.updateFirmPortfolio(FirmPortfolioServicesBean.java:90)
at com.pwc.us.pfp.firmportfolio.services.FirmPortfolioServices_kxm0gg_ELOImpl.updateFirmPortfolio(FirmPortfolioServices_kxm0gg_ELOImpl.java:157)
at com.pwc.us.pfp.firmportfolio.services.test.FirmPortfolioSvcsTest.testUpdateFirmPortfolio(FirmPortfolioSvcsTest.java:216)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.junitee.runner.TestRunner$1.run(TestRunner.java:72)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 9:30 am 
Senior
Senior

Joined: Fri Nov 21, 2003 5:55 am
Posts: 155
Show your mapping files and java source code.
If I understand your problem you can only do an update, not necessary a delete before.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 9:43 am 
Newbie

Joined: Wed Sep 17, 2003 1:31 am
Posts: 7
Location: tampa, FL
Source code:

//retrieve set of parent persistent objects
Collection firmPortfolios = lcl.findFirmPortfoliosByCountry( user.getCountryCode() );
FirmPortfolio p = null;
//retrieve one to do test
Iterator iter = firmPortfolios.iterator();
if( iter.hasNext() )
{
p = (FirmPortfolio)iter.next();
}
//update it
p.setName( "xxx" );
p.setFirmName( "xxx" );
p.setAddress( "xx" );
p.setPhoneNumber( "xxx" );

//kill the Set - has one child object in it
p.setPortfolioParties( null );
//create new Set
p.setPortfolioParties( new HashSet() );

PortfolioRole pmRole = new PortfolioRole();
pmRole.setRoleKey( AppEnums.PM );

PortfolioParty pp1 = new PortfolioParty();
pp1.setInternalUser( user2 );
pp1.setFirmPortfolio( p );
pp1.setPortfolioRole( pmRole );

PortfolioParty pp2 = new PortfolioParty();
pp2.setInternalUser( user3 );
pp2.setFirmPortfolio( p );
pp2.setPortfolioRole( pmRole );

//add two child PortfolioParty objects to new Set
p.addPortfolioParty( pp1 );
p.addPortfolioParty( pp2 );

//invokes method that does saveOrUpdate()
FirmPortfolio up = lcl.updateFirmPortfolio( p );


//code snippet where doing saveOrUpdate()

//get the HibernateSession object
session = HibernateGatewayFactory.createConnection();
session.saveOrUpdate( portfolio );
session.flush();



//parent persistent object mapping
<hibernate-mapping>
<class name="firmportfolio.FirmPortfolio" table="FIRM_PORTFOLIO" dynamic-update="false" dynamic-insert="false">
<id name="portfolioKey" column="PORTFOLIO_KEY" type="int" unsaved-value="0">
<generator class="sequence">
<param name="sequence">FIRM_PORTFOLIO_SEQ</param>
</generator>
</id>
<property name="name" type="java.lang.String" update="true" insert="true" column="PORTFOLIO_NAME"/>
<property name="firmName" type="java.lang.String" update="true" insert="true" column="FIRM_NAME"/>
<property name="address" type="java.lang.String" update="true" insert="true" column="ADDRESS"/>
<property name="country" type="java.lang.String" update="true" insert="true" column="COUNTRY_CODE"/>
<property name="phoneNumber" type="java.lang.String" update="true" insert="true" column="PHONE"/>
<set name="portfolioParties" lazy="false" inverse="true" cascade="all" sort="unsorted">
<key column="PORTFOLIO_KEY"/>
<one-to-many class="firmportfolio.PortfolioParty"/>
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-FirmPortfolio.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>



//object in Set mapping
<hibernate-mapping>
<class name="firmportfolio.PortfolioParty" table="PORTFOLIO_PARTY" dynamic-update="false" dynamic-insert="false">
<id name="portfolioPartyKey" column="PORTFOLIO_PARTY_KEY" type="int" unsaved-value="0">
<generator class="sequence">
<param name="sequence">PORTFOLIO_PARTY_SEQ</param>
</generator>
</id>
<many-to-one name="externalUser" class="ExternalUser" cascade="none" outer-join="false" update="true" insert="true" column="PERSONNEL_KEY" not-null="false"/>
<many-to-one name="internalUser" class="PWCUser" cascade="none" outer-join="false" update="true" insert="true" column="PGU_INDP_PERSONNEL_ID" not-null="false"/>
<many-to-one name="portfolioRole" class="firmportfolio.PortfolioRole" cascade="none" outer-join="auto" update="true" insert="true" column="ROLE_KEY" not-null="true"/>
<many-to-one name="firmPortfolio" class="firmportfolio.FirmPortfolio" cascade="none" outer-join="auto" update="true" insert="true" column="PORTFOLIO_KEY" not-null="true"/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-PortfolioParty.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 9:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Yes show the mapping files and the java code you use to drive this interaction with Hibernate. This most definitely does work as I use it in a number of situations.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 12:10 pm 
Newbie

Joined: Wed Sep 17, 2003 1:31 am
Posts: 7
Location: tampa, FL
Steve,
Did you have a chance to look at the code and mappings I provided? Also, can you elaborate on that section from the hibernate documentation that I mentioned?

Thanks,

jeff


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 1:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
So is the session used to load the intial FirmPortfolio collection open and available throughout this process? Or does lcl.findFirmPortfoliosByCountry( user.getCountryCode() ); close the session?

If not, I don't think this "one shot delete" semantic is available for detached objects. I've never tried it though, so I cannot say for sure but I don't think it would work.

So one option would be to try executing all this within the bounds of a single session. This is how I do it, and is the method I can assure works. The other option would be to map the set using "all-delete-orphan" instead of just "all" as the cascade semantic. That one *should* work.

Also, post the Hibernate logs leading up to that stack trace you posted earlier.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 20, 2004 9:48 am 
Newbie

Joined: Wed Sep 17, 2003 1:31 am
Posts: 7
Location: tampa, FL
Steve,
I used the cascade="all-delete-orphan" on the Set in the parent persisten object. Then did a removeAll() on the Set, then added the new objects to the Set, pushed to our persistence layer and did the saveOrUpdate(). And the good news is that it worked. Thanks for your help.

Jeff


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