-->
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.  [ 5 posts ] 
Author Message
 Post subject: Fail to add null value in not null attribute
PostPosted: Tue Apr 20, 2004 2:31 pm 
Newbie

Joined: Tue Apr 20, 2004 2:21 pm
Posts: 2
I'm trying to insert an object which has a Collection attribute, but the objects in the Collection are not being inserted because the foreign key is not appearing in the SQL command.

Shouldn't hibernate do this automatically?

I'm attaching some of my project files:

Code:
<hibernate-mapping>

    <class name="br.gov.mec.semtec.sipav.entidade.Avaliacao" table="TB_AVALIACAO">

        <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->

        <id name="id" type="long" unsaved-value="-1" >
            <column name="ID_AVALIACAO" sql-type="number(9)" not-null="true"/>
            <generator class="increment"/>
        </id>

        <property name="descricao">
            <column name="DESCRICAO" sql-type="varchar(500)" not-null="true"/>
        </property>

        <set name="roteiros" table="TB_ROTEIRO" lazy="true" cascade="all">
            <key column="ID_AVALIACAO"/>
            <one-to-many class="br.gov.mec.semtec.sipav.entidade.Roteiro"/>
        </set>

    </class>

</hibernate-mapping>



Code:
package br.gov.mec.semtec.sipav.entidade;

import java.util.Collection;
import br.gov.mec.semtec.sipav.util.*;

public class Avaliacao {
   private long id;
   private String descricao;   
   private Collection roteiros;
   
   public Avaliacao(){}
   
   
   public Avaliacao(long id, String descricao, Collection roteiros) {
      this.id = id;
      this.descricao = descricao;
      this.roteiros = roteiros;
   }

   public Avaliacao(String descricao, Collection roteiros) {
      this.id = Constantes.ID_INDEFINIDO;
      this.descricao = descricao;
      this.roteiros = roteiros;
   }

   public String getDescricao() {
      return descricao;
   }

   public long getId() {
      return id;
   }

   public void setDescricao(String string) {
      descricao = string;
   }

   public void setId(long l) {
      id = l;
   }

   public void setRoteiros(Collection collection) {
      roteiros = collection;
   }

   public Collection getRoteiros() {
      return roteiros;
   }

}



Hibernate generates the following SQL commands:
Hibernate: insert into TB_AVALIACAO (DESCRICAO, ID_AVALIACAO) values (?, ?)
Hibernate: insert into TB_ROTEIRO (DESCRICAO, ID_ROTEIRO) values (?, ?)

The error is:


Code:
20/04/2004 14:13:49 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ERROR:  ExecInsert: Fail to add null value in not null attribute id_avaliacao

20/04/2004 14:13:49 net.sf.hibernate.JDBCException <init>
SEVERE: Could not execute JDBC batch update
Batch entry 0 insert into TB_ROTEIRO (DESCRICAO, ID_ROTEIRO) values ( was aborted. Call getNextException() to see the cause.
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:105)
   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 br.gov.mec.semtec.sipav.persistencia.hibernate.EntidadeDAO.salvar(EntidadeDAO.java:74)
   at br.gov.mec.semtec.sipav.persistencia.hibernate.AvaliacaoDAO.inserir(AvaliacaoDAO.java:85)
   at br.gov.mec.semtec.sipav.controle.CadastroAvaliacoes.inserir(CadastroAvaliacoes.java:51)
   at br.gov.mec.semtec.sipav.controle.CadastroAvaliacoes.main(CadastroAvaliacoes.java:67)
20/04/2004 14:13:49 net.sf.hibernate.impl.SessionImpl execute
SEVERE: Could not synchronize database state with session
net.sf.hibernate.JDBCException: Could not execute JDBC batch update
   at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
   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 br.gov.mec.semtec.sipav.persistencia.hibernate.EntidadeDAO.salvar(EntidadeDAO.java:74)
   at br.gov.mec.semtec.sipav.persistencia.hibernate.AvaliacaoDAO.inserir(AvaliacaoDAO.java:85)
   at br.gov.mec.semtec.sipav.controle.CadastroAvaliacoes.inserir(CadastroAvaliacoes.java:51)
   at br.gov.mec.semtec.sipav.controle.CadastroAvaliacoes.main(CadastroAvaliacoes.java:67)
Caused by: Batch entry 0 insert into TB_ROTEIRO (DESCRICAO, ID_ROTEIRO) values ( was aborted. Call getNextException() to see the cause.
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:105)
   at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
   at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
   ... 7 more
br.gov.mec.semtec.sipav.excecao.PersistenciaException: net.sf.hibernate.JDBCException: Could not execute JDBC batch update
   at br.gov.mec.semtec.sipav.persistencia.hibernate.AvaliacaoDAO.inserir(AvaliacaoDAO.java:88)
   at br.gov.mec.semtec.sipav.controle.CadastroAvaliacoes.inserir(CadastroAvaliacoes.java:51)
   at br.gov.mec.semtec.sipav.controle.CadastroAvaliacoes.main(CadastroAvaliacoes.java:67)
Caused by: net.sf.hibernate.JDBCException: Could not execute JDBC batch update
   at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
   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 br.gov.mec.semtec.sipav.persistencia.hibernate.EntidadeDAO.salvar(EntidadeDAO.java:74)
   at br.gov.mec.semtec.sipav.persistencia.hibernate.AvaliacaoDAO.inserir(AvaliacaoDAO.java:85)
   ... 2 more
Caused by: Batch entry 0 insert into TB_ROTEIRO (DESCRICAO, ID_ROTEIRO) values ( was aborted. Call getNextException() to see the cause.
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:105)
   at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
   at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
   ... 7 more




Thanks in advance for any help,
acarla


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 5:22 pm 
Newbie

Joined: Tue Nov 04, 2003 7:31 pm
Posts: 5
I believe your missing an inverse="true" in the set. Checkout chapter 6 in the docos


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 9:59 am 
Newbie

Joined: Tue Apr 20, 2004 2:21 pm
Posts: 2
Is it really needed to create a bidirectional relationship in my entity classes?

When I put the inverse="true", I had to put

<many-to-one name="avaliacao" class="br.gov.mec.semtec.sipav.entidade.Avaliacao"
column="ID_AVALIACAO"/>

in the other mapping file and then it required a getter for property 'avaliacao' and I end up with the class 'Avaliacao' having a Collection of objects 'Roteiro' and 'Roteiro' having an attribute of 'Avaliacao'.

Am I doing something wrong, or it is impossible to hibernate save all the objects without having this undesirable biderectional relationship?

Thanks again!

acarla


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 10:41 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is of course required, as it correctly reflects your data model.

If you have A and B and your FK constraint on B says (not null): Always have a value for an A in this column, then you have to have a B that has the value! In other words: what should Hibernate save in the B table, if a B has no A? It can't just somehow look at your collection on the "other side" and figure out that this is a reference. What if there is no collection, no A?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 26, 2004 3:27 pm 
Newbie

Joined: Mon Feb 09, 2004 1:40 am
Posts: 13
Location: Moscow, Russia
christian wrote:
This is of course required, as it correctly reflects your data model.

If you have A and B and your FK constraint on B says (not null): Always have a value for an A in this column, then you have to have a B that has the value! In other words: what should Hibernate save in the B table, if a B has no A? It can't just somehow look at your collection on the "other side" and figure out that this is a reference. What if there is no collection, no A?


just delete it... B has no A, i mean.

I have this problem with cascade="all-delete-orphan".

_________________
God is Real, unless declared as Integer.


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