-->
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.  [ 1 post ] 
Author Message
 Post subject: Force inserting Parent/child
PostPosted: Wed Jan 18, 2006 6:01 am 
Beginner
Beginner

Joined: Wed May 18, 2005 9:48 am
Posts: 31
Hi all,

i have two tables joined by a foreign key.The relation between them are 1:n so many rows from child are from one Parent. I want that Child objects inherit foreign key from parent to all Primary key objects that owns. When I try to insert a new Parent with new childs gives me a key violation error. I think problems could be:

- It tries to insert first of all Child objects.

- Parent Foreign Key doesn´t have same value as Child Primary key.

What's wrong?. Here are my source code:


tIncidencias.hbm.xml (Parent)

Code:
<hibernate-mapping package="bbdd.modelos">

<class name="tIncidencias"
      table="incidencias" lazy="true">

   <!-- Common id property. -->
   <id name="Id_Inci"
      type="int"
      column="id_inci"
      unsaved-value="0">
      <generator class="native">
                    <param name="sequence">cod_incidencia_cod_inci_seq</param>
                </generator>
   </id>

   <!-- A versioned entity. -->
   <property   name="Num_Emple"
            type="int"
            column="num_emple"
            not-null="false"/>
        <property   name="Centro_Para"
            type="string"
            column="centro_para"
            not-null="false"/>
        <property   name="Tipo"
            type="string"
            column="tipo"
            not-null="false"/>
                               
   <!-- Limit item description to 4000 characters, Oracle. -->
   <property   name="Fecha"
            type="date"
            column="fecha"
            not-null="false"/>

   <property   name="Hora"
            type="time"
            column="hora"
            not-null="false"/>
                               
        <property   name="Estado"
            type="int"
            column="estado"
            not-null="false"/>
        <property   name="Prioridad"
            type="int"
            column="prioridad"
            not-null="false"/>
        <property   name="Ubicacion"
            type="string"
            column="ubicacion"
            not-null="false"/>
                               
   <!-- Limit item description to 4000 characters, Oracle. -->
   <property   name="Num_Serie"
            type="string"
            column="num_serie"
            not-null="false"/>

   <many-to-one name="Sistema"
                   class="tSistemas"
         column="id_sist"
         cascade="save-update"
         unique="true"/>
                               
                               
           <property   name="Breve"
            type="string"
            column="breve"
            not-null="false"/>
        <property   name="Detalle"
            type="string"
            column="detalle"
            not-null="false"/>
        <property   name="Id_Operador"
            type="int"
            column="id_ope"
            not-null="false"/>
                               
   <!-- Limit item description to 4000 characters, Oracle. -->
   <property   name="Observaciones"
            type="string"
            column="observaciones"
            not-null="false"/>

   <property   name="Com_Ope"
            type="int"
            column="com_ope"
            not-null="false"/>
                               
        <property   name="Fecha_Res"
            type="date"
            column="fecha_res"
            not-null="false"/>
        <property   name="Comentarios"
            type="string"
            column="comentarios"
            not-null="false"/>
        <property   name="Fecha_Resu"
            type="date"
            column="fecha_resu"
            not-null="false"/>
                               
   <!-- Limit item description to 4000 characters, Oracle. -->
   <property   name="Accion"
            type="string"
            column="accion"
            not-null="false"/>

   <property   name="Ope_Prod"
            type="int"
            column="ope_prod"
            not-null="false"/>
                               
        <property   name="Ope_Seg"
            type="int"
            column="ope_seg"
            not-null="false"/>
                               
        <property   name="Ope_Doseg"
            type="int"
            column="ope_doseg"
            not-null="false"/>
        <property   name="Datos"
            type="string"
            column="datos"
            not-null="false"/>
        <many-to-one name="Tipo_Inci"
                   class="tTipo_Inci"
         column="id_obj"
         cascade="save-update"
         unique="true"/>
                               
   <!-- Limit item description to 4000 characters, Oracle. -->
   <many-to-one name="Cod_Inci"
                   class="tCod_Inci"
         column="cod_inci"
         cascade="save-update"
         unique="true"/>
                       
        <set name="Adjuntos" inverse="true" cascade="all">
            <key column="id_adjunto" not-null="true"/>
            <many-to-many class="tAdjuntos" unique="true" />
        </set>
</class>


</hibernate-mapping>


tAdjuntos.hbm.xml (Child)

Code:
<hibernate-mapping package="bbdd.modelos">

<class name="tAdjuntos"
      table="adjuntos" lazy="true">

   <!-- Common id property. -->
        <id name="Id_Adjunto"
      type="int"
      column="id_adjunto"
      unsaved-value="0">
                <generator class="native">
                    <param name="sequence">incidencias_id_adjunto_seq</param>
                </generator>
   </id>
   <!-- A versioned entity. -->
   <property   name="Path"
            type="string"
            column="path"
            not-null="true"/>
</class>


</hibernate-mapping>


Insertion Code

Code:
newinci.setDetalle(request.getParameter("fDetalle"));
                    newinci.setNum_Emple(Integer.parseInt(sesion.getAttribute("sUsuario").toString()));
                    newinci.setPrioridad(Integer.parseInt(request.getParameter("fPrioridad")));
                    newinci.setCentro_Para(request.getParameter("fNumCentro"));
                    newinci.setFecha(dFechaNuevoCambio);
                    newinci.setBreve(request.getParameter("fBreve"));
                    newinci.setTipo_Inci(tipo_inci);
                    newinci.setSistema(sistema);
                    newinci.setCod_Inci(cod_inci);
                    newinci.setTipo("S");
                    if (request.getParameter("fAttach") != null) {
                        Set oAdjunto = new HashSet();
                        for (Iterator i=fFile.getAdjuntosWeb(request.getParameter("fAttach")).iterator(); i.hasNext();) {
                            tAdjuntos temp = new tAdjuntos();
                            temp.setPath("/wwwroot/archivos/incidencias/" + i.next());
                            temp.setId_Adjunto(newinci.getId_Inci());
                            oAdjunto.add(temp);
                        }
                    newinci.setAdjuntos(oAdjunto);
                    }
                     new IncidenciasDAO(oConexion.getSessionFactory().getCurrentSession()).save(newinci);


Generated Error

Code:
[ WARN] {18/01/06 10:46:46} (hibernate.util.JDBCExceptionReporter.logExceptions()) - SQL Error: 0, SQLState: null
[ERROR] {18/01/06 10:46:46} (hibernate.util.JDBCExceptionReporter.logExceptions()) - Batch entry 0 insert into adjuntos (path, id_adjunto) values (/wwwroot/archivos/incidencias/servidor.sql, 7) was aborted.  Call getNextException to see the cause.
[ WARN] {18/01/06 10:46:46} (hibernate.util.JDBCExceptionReporter.logExceptions()) - SQL Error: 0, SQLState: 23503
[ERROR] {18/01/06 10:46:46} (hibernate.util.JDBCExceptionReporter.logExceptions()) - ERROR: insert or update on table "adjuntos" violates foreign key constraint "adjuntos_id_adjunto_fkey"
[ERROR] {18/01/06 10:46:46} (event.def.AbstractFlushingEventListener.performExecutions()) - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:353)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
        at bbdd.Hibernate.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:98)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
        at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into adjuntos (path, id_adjunto) values (/wwwroot/archivos/incidencias/servidor.sql, 7) was aborted.  Call getNextException to see the cause.
        at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2387)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1257)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:334)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2446)
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
        ... 25 more
[ERROR] {18/01/06 10:46:46} (bbdd.Hibernate.HibernateThreadFilter.doFilter()) - Cleanup after exception!
[ERROR] {18/01/06 10:46:46} (catalina.core.StandardWrapperValve.invoke()) - Servlet.service() para servlet jsp lanzó excepción
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:353)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
        at bbdd.Hibernate.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:98)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
        at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into adjuntos (path, id_adjunto) values (/wwwroot/archivos/incidencias/servidor.sql, 7) was aborted.  Call getNextException to see the cause.
        at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2387)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1257)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:334)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2446)
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)


Thanks all.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.