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.  [ 3 posts ] 
Author Message
 Post subject: Problems in many-to-many association using 2 one-to-many.
PostPosted: Thu Feb 21, 2008 12:20 pm 
Newbie

Joined: Tue Feb 19, 2008 6:48 am
Posts: 17
Hi.

I'm trying to map a many to many association with a entity association.

I have Etapa, EtapaTarefa and Tarefa. The relation is always saved by Etapa.

When i create the first time the relationship, everything works. When i update, the code inserts a new version of Etapa, and then (when is trying to update the relation) generates an exception.

I know that is better to use single key, but in this case is not up to me to decide to use a single field key.

Hibernate version:
3.2.6

Mapping documents:

Etapa Entity
Code:
<hibernate-mapping>
   <joined-subclass name="Etapa" table="ETAPA" extends="A">
      <key>
         <column name="NUMEROCONTEUDO" />
         <column name="VERSAOCONTEUDO" />
      </key>

      <set name="eapaTarefa" table="ETAPATAREFA" cascade="all">
         <key>
            <column name="NUMEROCONTEUDOETAPA" />
            <column name="VERSAOCONTEUDOETAPA" />
         </key>
         <one-to-many class="EtapaTarefa" />
      </set>
   </joined-subclass>
</hibernate-mapping>


Tarefa entity:
Code:
<hibernate-mapping>
   <joined-subclass name="Tarefa" table="TAREFA" extends="A">
      <key>
         <column name="NUMEROCONTEUDO" />
         <column name="VERSAOCONTEUDO" />
      </key>

      <set name="etapaTarefa" table="ETAPATAREFA">
         <key>
            <column name="NUMEROCONTEUDOTAREFA" />
            <column name="VERSAOCONTEUDOTAREFA" />
         </key>
         <one-to-many class="EtapaTarefa" />
      </set>
   </joined-subclass>
</hibernate-mapping>


EtapaTarefa association entity:
Code:
<hibernate-mapping>
   <class name="EtapaTarefa" table="ETAPATAREFA">
      <composite-id name="id" class="EtapaTarefa$Id" unsaved-value="undefined">
         <key-property name="numeroConteudoEtapa" column="NUMEROCONTEUDOETAPA" type="integer" />
         <key-property name="versaoConteudoEtapa" column="VERSAOCONTEUDOETAPA" type="integer" />
         <key-property name="numeroConteudoTarefa" column="NUMEROCONTEUDOTAREFA" type="integer" />
         <key-property name="versaoConteudoTarefa" column="VERSAOCONTEUDOTAREFA" type="integer" />
      </composite-id>

      <property name="tipo" column="TIPO" type="string" not-null="true" length="2" />

      <many-to-one name="tarefa" class="Tarefa" insert="false" update="false">
         <column name="NUMEROCONTEUDOTAREFA" />
         <column name="VERSAOCONTEUDOTAREFA" />
      </many-to-one>

      <many-to-one name="etapa" class="Etapa" insert="false" update="false">
         <column name="NUMEROCONTEUDOETAPA" />
         <column name="VERSAOCONTEUDOETAPA" />
      </many-to-one>
   </class>
</hibernate-mapping>


Full stack trace of any exception that occurs:

Code:
[2008-02-21 15:02:39]  WARN (RequestProcessor.java:516) Unhandled Exception thrown: class org.springframework.jdbc.UncategorizedSQLException
IPDMS[2008-02-21 15:02:39] ERROR (TransactionFilter.java:38) An exception ocurred in the transaction
javax.servlet.ServletException: org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Could not execute JDBC batch update; uncategorized SQLException for SQL [update ETAPATAREFA set NUMEROCONTEUDOETAPA=null, VERSAOCONTEUDOETAPA=null where NUMEROCONTEUDOETAPA=? and VERSAOCONTEUDOETAPA=?]; SQL state [72000]; error code [1407]; ORA-01407: cannot update ("IPDMS25"."ETAPATAREFA"."NUMEROCONTEUDOETAPA") to NULL
; nested exception is java.sql.BatchUpdateException: ORA-01407: cannot update ("TEST"."ETAPATAREFA"."NUMEROCONTEUDOETAPA") to NULL

   at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
   at com.sinfic.ipdms.comum.processor.RequestProcessor.processActionPerform(RequestProcessor.java:80)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)


Name and version of the database you are using:
Oracle10g

The generated SQL (show_sql=true):
Hibernate: insert into ETAPA (NUMEROCONTEUDO, VERSAOCONTEUDO) values (?, ?)
Hibernate: update ETAPATAREFA set NUMEROCONTEUDOETAPA=null, VERSAOCONTEUDOETAPA=null where NUMEROCONTEUDOETAPA=? and VERSAOCONTEUDOETAPA=?


[/code]

_________________
João Simas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 4:05 am 
Newbie

Joined: Thu Feb 21, 2008 8:39 am
Posts: 15
I'm trying to do the same, but my code is a bit different:

-no inheritance.
-I use key-many-to-one (but no luck with it) in the association entity.

take a look, it might help:

http://forum.hibernate.org/viewtopic.php?t=984049

Could you show how did you implement the java classes?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 2:01 pm 
Newbie

Joined: Tue Feb 19, 2008 6:48 am
Posts: 17
I have already changed my mapping having read the http://forum.hibernate.org/viewtopic.php?t=984049. But i still having problems.

Here are my classes:

Code:
public class Etapa extends A {

   private Set<EtapaTarefa> relacoesEtapaTarefa;
   

   public Set<EtapaTarefa> getRelacoesEtapaTarefa() {
      return this.relacoesEtapaTarefa;
   }

   public void setRelacoesEtapaTarefa(Set<EtapaTarefa> relacoesEtapaTarefa) {
      this.relacoesEtapaTarefa = relacoesEtapaTarefa;
   }
}


Code:
public class Tarefa extends A {

   private Set<EtapaTarefa> relacoesEtapaTarefa;

   public Set<EtapaTarefa> getRelacoesEtapaTarefa() {
      return this.relacoesEtapaTarefa;
   }

   public void setRelacoesEtapaTarefa(Set<EtapaTarefa> relacoesEtapaTarefa) {
      this.relacoesEtapaTarefa = relacoesEtapaTarefa;
   }
}


Code:
public class EtapaTarefa implements Serializable {

   public static class Id implements Serializable {

      private Etapa etapa;
      private Tarefa tarefa;

      public Id() {
      }

      public Id(Etapa etapa, Tarefa tarefa) {
         this.etapa = etapa;
         this.tarefa = tarefa;
      }

      @Override
      public boolean equals(Object obj) {
         if (obj instanceof Id) {
            Id objId = (Id) obj;
            EqualsBuilder equals = new EqualsBuilder();
            equals.append(this.etapa, objId.getEtapa());
            equals.append(this.tarefa, objId.getTarefa());
            return equals.isEquals();
         }
         return false;
      }

      @Override
      public int hashCode() {
         HashCodeBuilder builder = new HashCodeBuilder();
         builder.append(this.etapa.hashCode());
         builder.append(this.tarefa.hashCode());
         return builder.toHashCode();
      }

      @Override
      public String toString() {
         return this.etapa.toString() + " - " + this.tarefa.toString();
      }

      public Etapa getEtapa() {
         return etapa;
      }

      public void setEtapa(Etapa etapa) {
         this.etapa = etapa;
      }

      public Tarefa getTarefa() {
         return tarefa;
      }

      public void setTarefa(Tarefa tarefa) {
         this.tarefa = tarefa;
      }
   }

   // primary key
   private Id id;

   // fields
   private String tipo;

   public EtapaTarefa() {
      this.id = new Id();
   }

   public EtapaTarefa(Id id) {
      this.id = id;
   }


   public String getTipo() {
      return this.tipo;
   }

   public void setTipo(String tipo) {
      this.tipo = tipo;
   }

   public Id getId() {
      return this.id;
   }

   public void setId(Id id) {
      this.id = id;
   }

   @Override
   public boolean equals(Object obj) {
      if (obj instanceof EtapaTarefa && this.getId() != null) {
         return this.getId().equals(((EtapaTarefa) obj).getId());
      }
      return false;
   }

   @Override
   public int hashCode() {
      if (this.getId() != null) {
         return this.getId().hashCode();
      }
      return 0;
   }
}



When i'm saving a Etapa, two strange things happen:
1. The etapatarefa entry is inserted before etapa (that gives a contraint violation)
2. After the insert, occurs a update to null!

Se sql output:

Code:
Hibernate: insert into ETAPATAREFA (TIPO, NUMEROCONTEUDOETAPA, VERSAOCONTEUDOETAPA, NUMEROCONTEUDOTAREFA, VERSAOCONTEUDOTAREFA) values (?,  ?, ?, ?, ?)
Hibernate: insert into ETAPATAREFA (TIPO, NUMEROCONTEUDOETAPA, VERSAOCONTEUDOETAPA, NUMEROCONTEUDOTAREFA, VERSAOCONTEUDOTAREFA) values (?, ?, ?, ?, ?)
Hibernate: insert into A (numeroConteudo, versaoConteudo) values (?, ?)
Hibernate: insert into ETAPA (NUMEROCONTEUDO, VERSAOCONTEUDO) values (?, ?)
Hibernate: update ETAPATAREFA set NUMEROCONTEUDOETAPA=null, VERSAOCONTEUDOETAPA=null where NUMEROCONTEUDOETAPA=? and VERSAOCONTEUDOETAPA=?
IPDMS[2008-02-22 17:56:36]  WARN (JDBCExceptionReporter.java:77) SQL Error: 1407, SQLState: 72000
IPDMS[2008-02-22 17:56:36] ERROR (JDBCExceptionReporter.java:78) ORA-01407: cannot update ("TESTE"."ETAPATAREFA"."NUMEROCONTEUDOETAPA") to NULL

IPDMS[2008-02-22 17:56:36]  WARN (JDBCExceptionReporter.java:77) SQL Error: 1407, SQLState: 72000
IPDMS[2008-02-22 17:56:36] ERROR (JDBCExceptionReporter.java:78) ORA-01407: cannot update ("TESTE"."ETAPATAREFA"."NUMEROCONTEUDOETAPA") to NULL

IPDMS[2008-02-22 17:56:36] ERROR (AbstractFlushingEventListener.java:301) Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:169)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)


Can anyone help me.

_________________
João Simas


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