I'm using Hibernate 2.1.2 (with 2.1.1 have the same problem).
What I don't understand is why can I insert the first time a
ManPrev and right after a
HistoricoManPrev and when I try to insert a second
ManPrev (which works well) I get the following error when I insert the second
HistoricoManPrev:
Code:
net.sf.hibernate.JDBCException: could not insert: [pt.comseal.arsol.vo.HistoricoManPrev#2]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:479)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:443)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
( ... )
Caused by: java.sql.SQLException: ERROR: fk_historico_man_prev referential integrity violation - key referenced from historico_man_prev not found in mp_mpo
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:131)
at org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connection.java:505)
( ... )
And I have the register he's looking for!
In my DB I have in
MpMpo:
Code:
(register #1)
man_prev_fk: 1
man_prev_oper_fk: 1
estado: "A"
(register #2)
man_prev_fk: 2
man_prev_oper_fk: 1
estado: "I"
I have the following structure DB:
Code:
historico_man_prev
|
|
|---- > mp_mpo ----------> man_prev_oper
|
|
|-------------> man_prev
historico_man_prev (PK):
id
mp_mpo (PK):
man_prev_fk
man_prev_oper_fk
man_prev (PK):
id
man_prev_oper (PK):
id
--------------------------------------------
Mapping files (generated with R3)
--------------------------------------------
HistoricoManPrevCode:
<?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>
<class
name="vo.HistoricoManPrev"
table="historico_man_prev"
>
<id
name="id"
type="long"
column="id"
>
<generator class="increment" />
</id>
<property
name="tipoManPrev"
type="java.lang.String"
column="tipo_man_prev"
length="1"
/>
<!-- associations -->
<!-- bi-directional many-to-one association to MpMpo -->
<many-to-one
name="mpMpo"
class="vo.MpMpo"
not-null="true"
>
<column name="man_prev_fk" />
<column name="man_prev_oper_fk" />
</many-to-one>
</class>
</hibernate-mapping>
MpMpoCode:
<?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>
<class
name="vo.MpMpo"
table="mp_mpo"
>
<composite-id name="comp_id" class="vo.MpMpoPK">
<!-- bi-directional many-to-one association to ManPrevOper -->
<key-many-to-one
name="manPrevOper"
class="vo.ManPrevOper"
>
<column name="man_prev_oper_fk" />
</key-many-to-one>
<!-- bi-directional many-to-one association to ManPrev -->
<key-many-to-one
name="manPrev"
class="vo.ManPrev"
>
<column name="man_prev_fk" />
</key-many-to-one>
</composite-id>
<property
name="estado"
type="java.lang.String"
column="estado"
length="1"
/>
<!-- associations -->
<!-- bi-directional one-to-many association to HistoricoManPrev -->
<set
name="historicoManPrevs"
lazy="true"
inverse="true"
>
<key>
<column name="man_prev_fk" />
<column name="man_prev_oper_fk" />
</key>
<one-to-many
class="vo.HistoricoManPrev"
/>
</set>
</class>
</hibernate-mapping>
ManPrevCode:
<?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>
<class
name="vo.ManPrev"
table="man_prev"
>
<id
name="id"
type="long"
column="id"
>
<generator class="increment" />
</id>
<property
name="tipoManPrev"
type="java.lang.String"
column="tipo_man_prev"
length="1"
/>
<!-- associations -->
<!-- bi-directional one-to-many association to MpMpo -->
<set
name="mpMpos"
lazy="true"
inverse="true"
>
<key>
<column name="man_prev_fk" />
</key>
<one-to-many
class="vo.MpMpo"
/>
</set>
</class>
</hibernate-mapping>
ManPrevOperCode:
Similar with ManPrev.
---------------------
Testing code
---------------------
To insert a
ManPrev:
Code:
ManPrev man_prev = new ManPrev();
man_prev.setTipoManPrev("xpto");
MpMpo mp_mpo = new MpMpo();
MpMpoPK mp_mpo_pk = new MpMpoPK();
mp_mpo_pk.setManPrev(man_prev);
ManPrevOper man_prev_oper = new ManPrevOper();
man_prev_oper.setId(new Long(1));
mp_mpo_pk.setManPrevOper(man_prev_oper);
mp_mpo.setComp_id(mp_mpo_pk);
mp_mpo.setEstado("A");
HashSet mp_mpos = new HashSet();
mp_mpos.add(mp_mpo);
man_prev.setMpMpos(mp_mpos);
ManPrevBO.inserirManPrev(man_prev);
To insert a
HistoricoManPrev:
Code:
HistoricoManPrev historico_man_prev = new HistoricoManPrev();
ManPrev man_prev = new ManPrev();
man_prev.setId(new Long(2));
MpMpo mp_mpo = new MpMpo();
MpMpoPK mp_mpo_pk = new MpMpoPK();
mp_mpo_pk.setManPrev(man_prev);
ManPrevOper man_prev_oper = new ManPrevOper();
man_prev_oper.setId(new Long(1));
mp_mpo_pk.setManPrevOper(man_prev_oper);
mp_mpo.setComp_id(mp_mpo_pk);
historico_man_prev.setMpMpo(mp_mpo);
historico_man_prev.setTipoManPrev("xpto");
ManPrevBO.insertHistoricoManPrev(historico_man_prev);
-------------------------
Business method
-------------------------
To insert a
ManPrev:
Code:
( ... )
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.save(man_prev);
if(man_prev.getMpMpos() != null) {
Iterator it = man_prev.getMpMpos().iterator();
while(it.hasNext()) { session.save((MpMpo)it.next()); }
}
transaction.commit();
( ... )
To insert a
HistoricoManPrev:
Code:
( ... )
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.save(historico_man_prev);
transaction.commit();
( ... )
Why isn't he inserting the secong
HistoricoManPrev!?
Please help. It's getting me crazy.