I’m using Hibernate with Spring and Hibernate return this error:
Caused by: java.sql.BatchUpdateException: Column 'ID_TAREA_FK' cannot be null
I have two classes
public class Tarea {
private List<Sesion> sesiones;
}
public class Sesion {
private Tarea tarea;
}
With their getters and setters. In my code, I don’t forget
t.getSesiones().add(s);
s.setTarea(t);
The database tables are:
TAREAS { ID_TAREA, … }, SESIONES { ID_SESION, ID_TAREA_FK, … }
The pieco of code in Tarea.hbm.xml for the list sesiones is:
<bag name="sesiones" table="SESIONES" inverse="true" cascade="all">
<key column="ID_TAREA" not-null="true" />
<one-to-many class="com.jonasurbano.tareas.domain.sesiones.Sesion" />
</bag>
In Sesion.hbm.xml:
<many-to-one name="tarea" class="com.jonasurbano.tareas.domain.tareas.Tarea" not-null="true">
<column name="ID_TAREA_FK" />
</many-to-one>
What am I doing wrongly? I’ve read Hibernate documentation and this post Hibernate One To Many Problem but it doesn’t work for me.
After working out this problem, I would like to know if there is some way to avoid the reference Tarea in class Sesion because I think this property is redundant.
Thanks a lot!
Check your database constraints. Probably
ID_TAREA_FKhasnot nullconstraint, try to remove this constraint, because Hibernate saves the identity column first with null values at all other columns.Delete
not-null=”true”.Rewrite mapping in one string:
Read this if something wrong with mapping and nothing helps you http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html#collections-bidirectional