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