I'm using Hibernate 2.0.3.
My mapping file where generated with Middlegen R3.
OpTurno
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="pt.vo.OpTurno"
table="op_turnos"
>
<composite-id name="comp_id" class="pt.vo.OpTurnoPK">
<key-property
name="dataTurno"
column="data_turno"
type="long"
length="8"
/>
<!-- bi-directional many-to-one association to Turno -->
<key-many-to-one
name="turno"
class="pt.vo.Turno"
>
<column name="turno_fk" />
</key-many-to-one>
<!-- bi-directional many-to-one association to Op -->
<key-many-to-one
name="op"
class="pt.vo.Op"
>
<column name="op_fk" />
</key-many-to-one>
</composite-id>
<property
name="userId"
type="java.lang.String"
column="user_id"
not-null="true"
length="10"
/>
<property
name="dtHr"
type="java.sql.Timestamp"
column="dt_hr"
not-null="true"
length="8"
/>
<!-- associations -->
<!-- bi-directional one-to-one association to OpControloProducao -->
<one-to-one
name="opControloProducao"
class="pt.vo.OpControloProducao"
outer-join="auto"
constrained="true"
/>
</class>
</hibernate-mapping>
OpControloProducao
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="pt.vo.OpControloProducao"
table="op_controlo_producao"
>
<composite-id name="comp_id" class="pt.vo.OpControloProducaoPK">
<!-- bi-directional one-to-one association to OpTurno -->
<key-many-to-one
name="opTurno"
class="pt.vo.OpTurno"
>
<column name="op_fk" />
<column name="turno_fk" />
<column name="data_turno_fk" />
</key-many-to-one>
</composite-id>
<property
name="totalProducao"
type="int"
column="total_producao"
length="4"
/>
<property
name="userId"
type="java.lang.String"
column="user_id"
not-null="true"
length="10"
/>
<property
name="dtHr"
type="java.sql.Timestamp"
column="dt_hr"
not-null="true"
length="8"
/>
<!-- associations -->
<!-- bi-directional one-to-one association to OpTurno -->
<one-to-one
name="opTurno"
class="pt.comseal.arsol.vo.OpTurno"
outer-join="auto"
/>
</class>
</hibernate-mapping>
---------------------
My method
---------------------
public static void insertOpContProd(Long op_id, short turno_id, long data_turno, int qtd, String user_id, Date dt_hr) throws ControloBOException, BadFactoryException {
Session session = null;
Transaction transaction = null;
OpControloProducao op_controlo_producao = new OpControloProducao();
OpControloProducaoPK op_controlo_producao_pk = new OpControloProducaoPK();
try {
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
OpTurnoPK op_turno_pk = new OpTurnoPK();
op_turno_pk.setDataTurno(data_turno);
Op op = new Op();
op.setId(new Long(1));
op_turno_pk.setOp(op);
Turno turno = new Turno();
turno.setId(new Short(turno_id));
op_turno_pk.setTurno(turno);
OpTurno op_turno = new OpTurno();
op_turno.setComp_id(op_turno_pk);
op_turno.setUserId(user_id);
op_turno.setDtHr(dt_hr);
op_controlo_producao_pk.setOpTurno(op_turno);
op_controlo_producao.setComp_id(op_controlo_producao_pk);
op_controlo_producao.setTotalProducao(qtd);
op_controlo_producao.setUserId(user_id);
op_controlo_producao.setDtHr(dt_hr);
op_turno.setOpControloProducao(op_controlo_producao);
session.save(op_turno);
session.save(op_controlo_producao);
transaction.commit();
session.close();
}
catch(HibernateException e) {
try {
if(transaction != null) transaction.rollback();
}
catch(HibernateException e1) {}
throw new ControloBOException("Error");
}
catch(BadFactoryException e) {
log.fatal(e.getMessage());
throw new BadFactoryException(e.getMessage());
}
finally {
try {
if(session != null) session.close();
}
catch(HibernateException e) {log.error("Error");}
}
}
--------------------
Every time I try to execute this method, it gives me:
Hibernate: insert into op_controlo_producao (total_producao, user_id, dt_hr, op_fk, turno_fk, data_turno_fk) values (?, ?, ?, ?, ?, ?)
WARN : SQL Error: 0, SQLState: null
ERROR: ERROR: op_cont_prod_op_turnos_fk referential integrity violation - key referenced from op_controlo_producao not found in op_turnos
ERROR: Could not synchronize database state with session
java.sql.SQLException: ERROR: op_cont_prod_op_turnos_fk referential integrity violation - key referenced from op_controlo_producao not found in op_turnos
( ... )
---------------------
It's strange because I'm doing the "session.save(op_turno)" first.
Can someone please help me? I'm getting desperate. I'm on a deadline.