-->
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.  [ 12 posts ] 
Author Message
 Post subject: Please help! referential integrity violation
PostPosted: Wed Dec 17, 2003 12:12 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 12:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try doing a session.flush() between the saves


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 12:20 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gloeglm wrote:
Try doing a session.flush() between the saves


With "session.flush()" between "saves" it gives me the following:

Hibernate: insert into op_turnos (user_id, dt_hr, data_turno, turno_fk, op_fk) values (?, ?, ?, ?, ?)
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


Any clue?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 12:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
one-to-one associations by default have to share the same primary key. As this is not the case here (if i understand your mappings), this cannot work. Try looking at the property-ref attribute, which might help you out.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 12:35 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gloeglm wrote:
one-to-one associations by default have to share the same primary key. As this is not the case here (if i understand your mappings), this cannot work. Try looking at the property-ref attribute, which might help you out.



Can I use "property-ref" with a "composite-id"?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 12:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Don't really know - to be honest, I absolutely don't see through the crazy model you have here.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 2:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
nerotnt wrote:
Can I use "property-ref" with a "composite-id"?

Try, it should work. If succeed, add it the the wiki, this is a common question (even if it sounds like bad design)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2003 5:05 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
epbernard wrote:
nerotnt wrote:
Can I use "property-ref" with a "composite-id"?

Try, it should work. If succeed, add it the the wiki, this is a common question (even if it sounds like bad design)


Ok. I'll give it a shot.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2003 6:32 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
epbernard wrote:
Try, it should work. If succeed, add it the the wiki, this is a common question (even if it sounds like bad design)


I've switched to version 2.1.1.

1) Without the "property-ref" he gives me the following error:

net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: pt.comseal.arsol.vo.OpTurno
( ... )


2) If I put the "property-ref" in the mapping file for "OpControloProducao" like this:

<one-to-one
name="opTurno"
class="pt.comseal.arsol.vo.OpTurno"
outer-join="auto"
property-ref="comp_id"
/>

I get the following error:

net.sf.hibernate.MappingException: property-ref not found: comp_id in class: pt.comseal.arsol.vo.OpTurno

------------
Summary
------------
"OpTurno" has a "comp_id" of type "OpTurnoPK" that is a "composite-id" for "OpTurno".

Am I doing this correctly?

NOTE:
"OpControloProducao" gets it's "composite-id" from "OpTurno". That's why I try to "session.save" the "OpTurno" first.

Please help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2003 7:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
property-ref names a property of the referenced class, not a column


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2003 8:08 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
property-ref names a property of the referenced class, not a column


Correct. That's what I thought.
And that 's whats happening in my example.
In the DB I have a composite PK of (op_fk, turno_fk, data_turno_fk) on table "op_controlo_producao" which references the composite PK (op_fk_turno_fk, data_turno) of my op_turno table.

Now "OpControloProducao" has a class "OpControloProducaoPK" to map this "composite-id" and in "OpControloProducao" mapping file I put:

<one-to-one
name="opTurno"
class="pt.vo.OpTurno"
outer-join="auto"
property-ref="comp_id"
/>

to reference the property "comp_id" in class "OpTurno" that is type "OpTurnoPK".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2003 8:41 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
property-ref names a property of the referenced class, not a column


I "think" I solved my problem without using "property-ref".
Looks kind of strange but tried the following:

"OpTurnoPK" being the identifier of "OpTurno", and
"OpTurnoPK" being the identifier of "OpControloProducao" eliminating "OpControloProducaoPK".

Now it seems to work.
I'm afraid that this might have implications on delete's or other operations.

Can someone confirm this plese?


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