-->
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.  [ 2 posts ] 
Author Message
 Post subject: Session.find in Hibernate internally calls update and fail
PostPosted: Wed Jul 08, 2009 11:55 pm 
Newbie

Joined: Fri Jun 05, 2009 3:01 am
Posts: 4
Hello All,

Hello All,

I am gettign this viered problem in selecting record from database. It internally tries to do an update to the record at the end of the select in closing the Hibernate Session and fails due to primary key constraint violation.

Why Hibernate tries to update when I just do a select. Below is the related code and error messages.. Please shed some light on this.

* AcctBalance has a collection of AcctTransactions.

Code:
List<AcctBalance> balances = HibernateHelper.find(
            "FROM AcctBalance AS bal WHERE bal.balanceId = ?",
            new Object[] { balanceId },
            new Type[] { Hibernate.LONG },
            sessionContext);


Below is the find method:

Code:
public static List find(
        String queryStr,
        Object[] paramValues,
        Type[] paramTypes,
        SessionContext context)
        throws GeneralException {       

        List result = null;
      Session hbSession = null;
        try {
         hbSession = HibernateHelper.getSession();
            return [color=#FF0000]hbSession.find[/color](
                queryStr,
                paramValues,
                paramTypes);
        } catch (HibernateException ex) {
            if (context != null) {
                try {
                    context.setRollbackOnly();
                } catch (IllegalStateException ignoredEx) {
                    // do nothing!
                }
            }
            throw new GeneralException(
                GeneralExceptionCode.DATABASE_ERROR,
                "Error occured executing find",
                ex);
        } finally {
            HibernateHelper.closeSession(hbSession, context);
        }
    }



Hibernate Mapping
Code:
<hibernate-mapping>
     <class name="com.connectserver.common.AcctBalance" table="ACCT_BALANCE">
       <id name="balanceId" column="BALANCE_ID" type="long" unsaved-value="null">
           <generator class="assigned"/>
       </id>
          <property name="bankMsgId" column="BANK_MSG_ID" type="long"/>
          <property name="statusStr" column="STATUS" not-null="true"/>
         <set name="transactions" lazy="false" cascade="all" table="ACCT_TRANSACTION">
           <key column="BALANCE_ID"/>
           <composite-element class="com.connectserver.common.AcctTransaction">
             <property name="transactionId" column="TRANSACTION_ID"/>
             <property name="tranCode" column="TRAN_CODE"/>
                  <property name="description" column="DESCRIPTION"/>
                  <property name="amount" column="AMOUNT" type="long"/>
                  <property name="statusStr" column="STATUS"/>
           </composite-element>           
       </set>
    </class>
</hibernate-mapping>



Error Message:

Code:
2009-07-09 11:08:40,889 DEBUG [Translator.HibernateHelper] getSession()
2009-07-09 11:08:40,889 DEBUG [net.sf.hibernate.impl.SessionImpl] opened session
2009-07-09 11:08:40,889 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2009-07-09 11:08:40,889 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2009-07-09 11:08:40,889 DEBUG [net.sf.hibernate.hql.QueryTranslator] HQL: SELECT bal FROM com.connectserver.common.AcctBalance AS bal  WHERE bal.bankMsgId=?
2009-07-09 11:08:40,889 DEBUG [net.sf.hibernate.hql.QueryTranslator] SQL: select acctbalanc0_.BALANCE_ID as BALANCE_ID, acctbalanc0_.BANK_MSG_ID as BANK_MSG2_, acctbalanc0_.TRANSLATOR_ID as TRANSLAT3_, acctbalanc0_.AS_OF_DATE as AS_OF_DATE, acctbalanc0_.ACCT_NAME as ACCT_NAME, acctbalanc0_.BANK_CODE as BANK_CODE, acctbalanc0_.CURRENCY_CODE as CURRENCY9_, acctbalanc0_.TOTAL_CREDITS as TOTAL_C12_, acctbalanc0_.NUMBER_OF_CR_TRANS as NUMBER_13_, acctbalanc0_.TOTAL_DEBITS as TOTAL_D14_, acctbalanc0_.NUMBER_OF_DR_TRANS as NUMBER_15_, acctbalanc0_.ACCRUED_CR_INTEREST as ACCRUED16_, acctbalanc0_.ACCRUED_DR_INTEREST as ACCRUED17_, acctbalanc0_.STATUS as STATUS from ACCT_BALANCE acctbalanc0_ where (acctbalanc0_.BANK_MSG_ID=? )
2009-07-09 11:08:40,920 DEBUG [net.sf.hibernate.SQL] select acctbalanc0_.BALANCE_ID as BALANCE_ID, acctbalanc0_.BANK_MSG_ID as BANK_MSG2_, acctbalanc0_.TRANSLATOR_ID as TRANSLAT3_, acctbalanc0_.AS_OF_DATE as AS_OF_DATE, acctbalanc0_.ACCT_NAME as ACCT_NAME, acctbalanc0_.BANK_CODE as BANK_CODE, acctbalanc0_.CURRENCY_CODE as CURRENCY9_, acctbalanc0_.TOTAL_CREDITS as TOTAL_C12_, acctbalanc0_.NUMBER_OF_CR_TRANS as NUMBER_13_, acctbalanc0_.TOTAL_DEBITS as TOTAL_D14_, acctbalanc0_.NUMBER_OF_DR_TRANS as NUMBER_15_, acctbalanc0_.STATUS as STATUS from ACCT_BALANCE acctbalanc0_ where (acctbalanc0_.BANK_MSG_ID=? )
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] result row: 1
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] resolving associations for [com.connectserver.common.AcctBalance#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] done materializing entity [com.connectserver.common.AcctBalance#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] initializing non-lazy collections
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.SQL] select transactio0_.TRANSACTION_ID as TRANSACT2___, transactio0_.TRAN_CODE as TRAN_CODE__, transactio0_.DESCRIPTION as DESCRIPT4___, transactio0_.AMOUNT as AMOUNT__,transactio0_.TRAN_TYPE as TRAN_TYPE__, transactio0_.VALUE_DATE as VALUE_DATE__, transactio0_.STATUS as STATUS__, transactio0_.BALANCE_ID as BALANCE_ID__ from ACCT_TRANSACTION transactio0_ where transactio0_.BALANCE_ID=?
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] result set contains (possibly empty) collection: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] result row:
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] found row of collection: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] result row:
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] found row of collection: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] result row:
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] found row of collection: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] result row:
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.loader.Loader] found row of collection: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] 1 collections were found in result set
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] collection fully initialized: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] 1 collections initialized
2009-07-09 11:08:40,936 DEBUG [Translator.HibernateHelper] closeSession()
2009-07-09 11:08:40,936 DEBUG [Translator.HibernateHelper] closeSession() - flushing and closing hibernate session
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] Collection dirty: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] Collection found: [com.connectserver.common.AcctBalance.transactions#1], was: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 (re)creations, [color=#FF0000]1 updates[/color], 0 removals to 1 collections
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.Printer] listing entities:
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.impl.Printer] com.connectserver.common.AcctBalance{accruedCrInterest=200, transactions=[AcctTransaction{amount=2468, valueDate=null, transactionId=1, fitId=fit1, description=test1, tranCode=A, statusStr=null, refNo=ABC1, tranType=t}, AcctTransaction{amount=2345, valueDate=null, transactionId=2, fitId=fit0, description=test0, tranCode=A, statusStr=null, refNo=ABC0, tranType=t}, AcctTransaction{amount=2591, valueDate=null, transactionId=3, fitId=fit2, description=test2, tranCode=A, statusStr=null, refNo=ABC2, tranType=t}, AcctTransaction{amount=2714, valueDate=null, transactionId=4, fitId=fit3, description=test3, tranCode=A, statusStr=null, refNo=ABC3, tranType=t}], currencyCode=AUD, accruedDrInterest=100, accruedTasmDuty=80, totalDebits=300, translatorId=7S8TI9250, asOfDate=08 July 2009, statusStr=O, accruedGovDrTax=20, numberOfCrTrans=20, balanceId=1, bankCode=FI_NBNZ, totalCredits=500, openingBalance=111, acctName=Test Account2, numberOfDrTrans=10, acctBSB=0612, acctNumber=10456333, bankMsgId=100, closingBalance=444}
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.collection.BasicCollectionPersister] Deleting rows of collection: [com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,936 DEBUG [net.sf.hibernate.SQL] delete from ACCT_TRANSACTION where BALANCE_ID=? and TRANSACTION_ID=? and TRAN_CODE=? and DESCRIPTION=? and AMOUNT=? and TRAN_TYPE=? and VALUE_DATE=? and STATUS=?
2009-07-09 11:08:40,951 DEBUG [net.sf.hibernate.collection.BasicCollectionPersister] done deleting collection rows: 4 deleted
2009-07-09 11:08:40,951 DEBUG [net.sf.hibernate.collection.BasicCollectionPersister] Updating rows of collection: com.connectserver.common.AcctBalance.transactions#1
2009-07-09 11:08:40,951 DEBUG [net.sf.hibernate.collection.BasicCollectionPersister] done updating rows: 0 updated
2009-07-09 11:08:40,951 DEBUG [net.sf.hibernate.collection.BasicCollectionPersister] [color=#FF0000]Inserting rows of collection: [/color][com.connectserver.common.AcctBalance.transactions#1]
2009-07-09 11:08:40,951 DEBUG [net.sf.hibernate.SQL] insert into ACCT_TRANSACTION (BALANCE_ID, TRANSACTION_ID, TRAN_CODE, DESCRIPTION, AMOUNT, TRAN_TYPE, VALUE_DATE, STATUS) values (?, ?, ?, ?, ?, ?, ?, ?)
2009-07-09 11:08:40,982 DEBUG [net.sf.hibernate.util.JDBCExceptionReporter] SQL Exception
java.sql.SQLException: ORA-00001: unique constraint (PS_ADMIN.ACCT_TRANSACTION_PK) violated

   at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
   at oracle.jdbc.oci8.OCIDBAccess.check_error(OCIDBAccess.java:2321)
   at oracle.jdbc.oci8.OCIDBAccess.executeFetch(OCIDBAccess.java:1741)
   at oracle.jdbc.oci8.OCIDBAccess.parseExecuteFetch(OCIDBAccess.java:1902)
   at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:2047)
   at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1940)
   at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2709)
   at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:589)
   at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:365)
   at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
   at net.sf.hibernate.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:622)
   at net.sf.hibernate.impl.ScheduledCollectionUpdate.execute(ScheduledCollectionUpdate.java:49)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2338)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
   at com.common.utilities.db.HibernateHelper.closeSession(Unknown Source)
   at com.common.utilities.HibernateUtils.query(Unknown Source)
   at com.connectserver.common.ConnectMgrBean.getAccountBalancesByMsgId(Unknown Source)
   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:597)
   at org.jboss.invocation.Invocation.performCall(Invocation.java:359)
   at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:237)
   at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:158)
   at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:169)
   at org.jboss.ejb.plugins.CallValidationInterceptor.invoke(CallValidationInterceptor.java:63)
   at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:121)
   at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:350)
   at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:181)
   at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:168)
   at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:205)
   at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:138)
   at org.jboss.ejb.SessionContainer.internalInvoke(SessionContainer.java:648)
   at org.jboss.ejb.Container.invoke(Container.java:960)
   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:597)
   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
   at org.jboss.invocation.unified.server.UnifiedInvoker.invoke(UnifiedInvoker.java:231)
   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:597)
   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
   at javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:288)
   at $Proxy14.invoke(Unknown Source)
   at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:809)
   at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:608)
   at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:420)
   at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:173)
2009-07-09 11:08:40,982 WARN  [net.sf.hibernate.util.JDBCExceptionReporter] SQL Error: 1, SQLState: 23000
2009-07-09 11:08:40,982 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] ORA-00001: unique constraint (PS_ADMIN.ACCT_TRANSACTION_PK) violated

2009-07-09 11:08:40,982 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] could not insert collection rows: [com.connectserver.common.AcctBalance.transactions#1]
java.sql.SQLException: ORA-00001: unique constraint (PS_ADMIN.ACCT_TRANSACTION_PK) violated

   at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
   at oracle.jdbc.oci8.OCIDBAccess.check_error(OCIDBAccess.java:2321)


Top
 Profile  
 
 Post subject: Re: Session.find in Hibernate internally calls update and fail
PostPosted: Thu Jul 09, 2009 2:00 am 
Newbie

Joined: Fri Jun 05, 2009 3:01 am
Posts: 4
Hi All,

I found the solution for this issue. Adding it here so that it will be usefull for anyone else who gets the same issue in future.

In my AcctBalance class I had a sat for transactions.

Code:
private Set<AcctTransaction> transactions =  new HashSet<AcctTransaction>();


This is how I was setting this set in my DAO class:

Code:
   public void setTransactions(Set<AcctTransaction> transactions) {
      this.transactions = transactions;
   }


This causes the problem when Hibernate closes it's session. Then it tries to comapare the persistent set and the passed set and might find that the HashCode is different in two sets. Then it decides to update it in the database and fails as the PK of transaction records is coming from a sequence and auto assigned from the database. Then it fails to update becasue it'll violate the unique constraints of PK.

This is how you should set the child records set in POJO class.

Code:
   public void setTransactions(Set<AcctTransaction> transactions) {
      this.transactions.addAll(transactions);
   }


This resolved my issue.


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