-->
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: parent tbl with sequence and child tbl with composite-key
PostPosted: Wed Jul 27, 2005 9:37 pm 
Beginner
Beginner

Joined: Thu Jul 21, 2005 10:28 am
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

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

<hibernate-mapping>

<class
name="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"
table="CCC.AGNT_MSG">

<id name="id" type="long" unsaved-value="0">
<column name="MSG_ID" sql-type="NUMBER" not-null="true" />
<generator class="sequence">
<param name="sequence">CCC.SEQ_AGNT_MSG</param>
</generator>
</id>

<set name="recipients" table="CCC.MSG_RECIPIENT" lazy="false" cascade="all" inverse="true">
<key column="MSG_ID" />
<one-to-many class="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO" />
</set>

<property name="fromDate" type="date">
<column name="MSG_START_DATE" sql-type="DATE" not-null="false" />
</property>

<property name="toDate" type="date">
<column name="MSG_END_DATE" sql-type="DATE" not-null="false" />
</property>

<property name="messageText" type="string">
<column name="MSG_TEXT" sql-type="VARCHAR2(500)" not-null="false" />
</property>

<property name="createUser" type="string">
<column name="CREATE_USER_ID" sql-type="VARCHAR2(20)"
not-null="false" />
</property>

<property name="createDate" type="date">
<column name="CREATE_DATE" sql-type="DATE" not-null="false" />
</property>

<property name="modUser" type="string">
<column name="UPDATE_USER_ID" sql-type="VARCHAR2(20)"
not-null="false" />
</property>

<property name="modDate" type="date">
<column name="UPDATE_DATE" sql-type="DATE" not-null="false" />
</property>

<property name="categoryCode" type="string">
<column name="MSG_CATG_CD" sql-type="VARCHAR2(20)" not-null="false" />
</property>

<property name="behaviorCode" type="string">
<column name="MSG_BHVR_CD" sql-type="VARCHAR2(20)" not-null="false" />
</property>

<many-to-one name="messageCategoryDVO" column="MSG_CATG_CD" class="com.starwood.saratoga.dataaccess.dvo.MessageCategoryDVO" not-null="true" insert="false" update="false"/>
<many-to-one name="messageBehaviorDVO" column="MSG_BHVR_CD" class="com.starwood.saratoga.dataaccess.dvo.MessageBehaviorDVO" not-null="true" insert="false" update="false"/>

</class>

</hibernate-mapping>


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

<hibernate-mapping>

<class
name="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO"
table="CCC.MSG_RECIPIENT">

<composite-id name="compKey" class="com.starwood.saratoga.dataaccess.dao.MessageRecipientCompositeKey" unsaved-value="any">
<key-property name="messageId" type="long" column="MSG_ID" />
<key-property name="recipientId" type="string" column="RECIPIENT_ID" />
</composite-id>

<property name="receiverCategoryCode" type="string">
<column name="RCVR_CATG_CD" sql-type="VARCHAR2(20)" not-null="false" />
</property>

<many-to-one name="recvCatgCd" column="RCVR_CATG_CD" class="com.starwood.saratoga.dataaccess.dvo.ReceiverCategoryDVO" not-null="true" insert="false" update="false"/>

</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.save(agentMessageDVO);
tx.commit();

Full stack trace of any exception that occurs:
JDBCException W org.hibernate.util.JDBCExceptionReporter SQL Error: 2291, SQLState: 23000
[7/27/05 20:37:39:684 EDT] 603077dc JDBCException E org.hibernate.util.JDBCExceptionReporter ORA-02291: integrity constraint (CCC.FK_MSG_RECIPIENT_AGNT_MSG_01) violated - parent key not found

[7/27/05 20:37:39:684 EDT] 603077dc JDBCException W org.hibernate.util.JDBCExceptionReporter SQL Error: 2291, SQLState: 23000
[7/27/05 20:37:39:684 EDT] 603077dc JDBCException E org.hibernate.util.JDBCExceptionReporter ORA-02291: integrity constraint (CCC.FK_MSG_RECIPIENT_AGNT_MSG_01) violated - parent key not found

[7/27/05 20:37:39:694 EDT] 603077dc AbstractFlush E org.hibernate.event.def.AbstractFlushingEventListener Could not synchronize database state with session
[7/27/05 20:37:39:764 EDT] 603077dc AbstractFlush E org.hibernate.event.def.AbstractFlushingEventListener TRAS0014I: The following exception was logged org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at com.starwood.saratoga.dataaccess.dao.AgentMessageDAO.save(AgentMessageDAO.java:49)
at com.starwood.saratoga.struts.messages.edit.MessagesAddAction.execute(MessagesAddAction.java:210)
at com.ibm.wps.portlets.struts.WpsRequestProcessor.processActionPerform(WpsRequestProcessor.java:335)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at com.ibm.wps.portlets.struts.WpsStrutsPortlet.processActionPerformed(WpsStrutsPortlet.java:1753)
at com.ibm.wps.portlets.struts.WpsStrutsPortlet.actionPerformed(WpsStrutsPortlet.java:1460)
at com.ibm.wps.pe.pc.legacy.SPIPortletInterceptorImpl.handleEvents(SPIPortletInterceptorImpl.java(Compiled Code))
at com.ibm.wps.pe.pc.legacy.invoker.impl.PortletDelegateImpl._dispatch(PortletDelegateImpl.java(Compiled Code))
at com.ibm.wps.pe.pc.legacy.invoker.impl.PortletDelegateImpl.access$200(PortletDelegateImpl.java(Inlined Compiled Code))
at com.ibm.wps.pe.pc.legacy.invoker.impl.PortletDelegateImpl$Context.callPortlet(PortletDelegateImpl.java(Inlined Compiled Code))
at com.ibm.wps.pe.pc.legacy.services.PortletFilterServiceImpl.doFilter(PortletFilterServiceImpl.java(Compiled Code))
at com.ibm.wps.pe.pc.legacy.services.PortletFilter.doFilter(PortletFilter.java(Inlined Compiled Code))
at com.ibm.wps.pe.pc.legacy.invoker.impl.PortletDelegateImpl.dispatch(PortletDelegateImpl.java(Compiled Code))
at org.apache.jetspeed.portlet.Portlet.doPost(Portlet.java(Compiled Code))
at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
at com.ibm.wps.pe.pc.legacy.cache.CacheablePortlet.service(CacheablePortlet.java(Compiled Code))
at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
at org.apache.jetspeed.portlet.Portlet.service(Portlet.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java(Inlined Compiled Code))
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java(Inlined Compiled Code))
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java(Compiled Code))
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java(Compiled Code))
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.include(WebAppRequestDispatcher.java(Compiled Code))
at com.ibm.wps.pe.pc.legacy.invoker.impl.PortletInvokerImpl.callMethod(PortletInvokerImpl.java(Compiled Code))
at com.ibm.wps.pe.pc.legacy.invoker.impl.PortletInvokerImpl.action(PortletInvokerImpl.java:82)
at com.ibm.wps.pe.pc.legacy.PortletContainerImpl.callPortletMethod(PortletContainerImpl.java(Compiled Code))
at com.ibm.wps.pe.pc.legacy.EventEnvironmentImpl.includePortlet(EventEnvironmentImpl.java:177)
at com.ibm.wps.pe.pc.legacy.event.ActionEventImpl.prepare(ActionEventImpl.java:121)
at com.ibm.wps.pe.pc.legacy.event.EventQueueManager.processEventLoop(EventQueueManager.java:83)
at com.ibm.wps.pe.pc.legacy.PortletContainerImpl.performEventPhase(PortletContainerImpl.java(Compiled Code))
at com.ibm.wps.pe.pc.legacy.PortletContainerImpl.performEvents(PortletContainerImpl.java:210)
at com.ibm.wps.pe.pc.PortletContainerImpl.performEvents(PortletContainerImpl.java(Compiled Code))
at com.ibm.wps.engine.phases.WPActionPhase.processPortlets(WPActionPhase.java:1170)
at com.ibm.wps.engine.phases.WPActionPhase.execute(WPActionPhase.java:561)
at com.ibm.wps.state.phases.AbstractActionPhase.next(AbstractActionPhase.java:130)
at com.ibm.wps.engine.Servlet.callPortal(Servlet.java:638)
at com.ibm.wps.engine.Servlet.doGet(Servlet.java:496)
at com.ibm.wps.engine.Servlet.doPost(Servlet.java:664)
at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java(Inlined Compiled Code))
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java(Compiled Code))
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:76)
at com.ibm.wps.state.filter.StateCleanup.doFilter(StateCleanup.java:71)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:132)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:71)
at com.ibm.wps.mappingurl.impl.URLAnalyzer.doFilter(URLAnalyzer.java:174)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:132)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:71)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java(Compiled Code))
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java(Compiled Code))
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:204)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:125)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:286)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (CCC.FK_MSG_RECIPIENT_AGNT_MSG_01) violated - parent key not found

at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:458)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3907)
at com.ibm.ws.rsadapter.jdbc.WSJdbcStatement.executeBatch(WSJdbcStatement.java:352)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 78 more

Name and version of the database you are using: Oracle 9i

The generated SQL (show_sql=true):

Hibernate: insert into CCC.AGNT_MSG (MSG_START_DATE, MSG_END_DATE, MSG_TEXT, CREATE_USER_ID, CREATE_DATE, UPDATE_USER_ID, UPDATE_DATE, MSG_CATG_CD, MSG_BHVR_CD, MSG_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: insert into CCC.MSG_RECIPIENT (RCVR_CATG_CD, MSG_ID, RECIPIENT_ID) values (?, ?, ?)

Debug level Hibernate log excerpt: N/A

Ok, so here's the problem ... I am trying to write to a parent record. The primary key [MSG_ID] is generated by an Oracle Sequence.

There is a simple collection that I am trying to write out to a child table which could be 1 or more records. According to ALL the documentation I read, this is doable. But, here's the catch, the two fields in the second table [MSG_ID] and [RECIPIENT_ID] is a composite-key in the child table.

The MSG_ID in this child table MUST be the same as the MSG_ID in the parent table (the one that was assigned via the sequence). According to the documentation I just read for H3, Chapter 9.4 on using Components as a composite-key, it says and I quote:

Quote:
You can't use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.


So ... am I SOL? I know what the RECIPIENT_ID is ... I know where the MSG_ID, I just can't assign it because I don't know what it is yet, and that is being handled for me.

I am stuck with this composite-pk in the child table ... and I can't really change the databse, and yes, I do know what the Hibernate authors feel about composite-id's.

If I am SOL, just say so. If you need more information, just say so. If I can work around this with a hack, just say so. I thought moving from 2.x to 3.x would get me out of a jam with this, but I guess not.

Any help I can get would be much appreciated! Thanks!

Tom


Top
 Profile  
 
 Post subject: parent tbl with child tbl composite-key SOLVED???
PostPosted: Thu Jul 28, 2005 8:41 pm 
Beginner
Beginner

Joined: Thu Jul 21, 2005 10:28 am
Posts: 21
Ok, I think I may have solved my problem with this! Here is the correct mapping XML files which solved my problem.

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

<hibernate-mapping>

   <class
      name="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"
      table="CCC.AGNT_MSG">

      <id name="id" type="long" unsaved-value="null">
         <column name="MSG_ID" sql-type="NUMBER" />
            <generator class="sequence">
            <param name="sequence">CCC.SEQ_AGNT_MSG</param>
         </generator>
      </id>

      <set name="recipients" cascade="all">
         <key column="MSG_ID" />
         <one-to-many class="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO" />
      </set>

      <property name="fromDate" type="date">
         <column name="MSG_START_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="toDate" type="date">
         <column name="MSG_END_DATE" sql-type="DATE" not-null="false" />
      </property>
      
      <property name="messageText" type="string">
         <column name="MSG_TEXT" sql-type="VARCHAR2(500)" not-null="false" />
      </property>

      <property name="createUser" type="string">
         <column name="CREATE_USER_ID" sql-type="VARCHAR2(20)"
            not-null="false" />
      </property>

      <property name="createDate" type="date">
         <column name="CREATE_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="modUser" type="string">
         <column name="UPDATE_USER_ID" sql-type="VARCHAR2(20)"
            not-null="false" />
      </property>

      <property name="modDate" type="date">
         <column name="UPDATE_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="categoryCode" type="string">
         <column name="MSG_CATG_CD" sql-type="VARCHAR2(20)" not-null="false" />
      </property>

      <property name="behaviorCode" type="string">
         <column name="MSG_BHVR_CD" sql-type="VARCHAR2(20)" not-null="false" />
      </property>

      <many-to-one name="messageCategoryDVO" column="MSG_CATG_CD"  class="com.starwood.saratoga.dataaccess.dvo.MessageCategoryDVO" not-null="true" insert="false" update="false"/>
      <many-to-one name="messageBehaviorDVO" column="MSG_BHVR_CD"  class="com.starwood.saratoga.dataaccess.dvo.MessageBehaviorDVO" not-null="true" insert="false" update="false"/>   

   </class>

</hibernate-mapping>



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

<hibernate-mapping>

   <class
      name="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO"
      table="CCC.MSG_RECIPIENT">

      <composite-id>
         <key-many-to-one name="myParent" class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO">
            <column name="MSG_ID"/>
         </key-many-to-one>
         <key-property name="recipientId" type="string" column="RECIPIENT_ID" />
      </composite-id>

      <property name="receiverCategoryCode" type="string">
         <column name="RCVR_CATG_CD" sql-type="VARCHAR2(20)" not-null="true" />
      </property>

      <many-to-one name="myParent"   column="MSG_ID"        class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO" not-null="true"     insert="false" update="false" />
      <many-to-one name="recvCatgCd" column="RCVR_CATG_CD"  class="com.starwood.saratoga.dataaccess.dvo.ReceiverCategoryDVO" not-null="true" insert="false" update="false"/>

   </class>

</hibernate-mapping>



The KEY pardon the pun was the following:
Code:
<key-many-to-one>


I looked in the Hibernate books and documentation I had and couldn't find too much on this ... hours and hours and hours of pain before I found this.

I hope helps someone else out![/code][/b]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 5:02 am 
Newbie

Joined: Fri Jul 15, 2005 6:35 am
Posts: 12
(..sorry for my english...)

I have the same problem, but my mistake is that in the parent table,
exists a trigger to increment the sequence.
So Hibernate get a number (i say 1) but oracle insert the parent with the key=2.

After that, it try to insert a child with the key=1 and raise the exception.

I hope this can be usefull for someone.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 7:20 am 
Newbie

Joined: Tue Feb 21, 2006 7:01 am
Posts: 1
Location: Malaysia
It seems that a lot of errors (ORA-02291 parent key not found) raised when using Hibernate with Oracle (in my case Oracle 10g XE) are due to another subsequent increment of the identifier when insertion is performed on the database.
Your Java application might hold the value, say 100, even from the log you will be able to see the value correctly, but once the method persist() or saveOrUpdate() is performed and the POJO is successfully persisted into the database, you will discovered from the database that the row is actually saved with identifier of value 101.
Is there any workaround or solution to this problem?

Edit: I have found out the cause of the increment. Just disable the trigger that perform another increment on the sequence before insertion oh each new row. I was careless to overlook the trigger for each table.


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.