-->
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.  [ 4 posts ] 
Author Message
 Post subject: Please help! can't get composite-id working
PostPosted: Fri Dec 19, 2003 5:21 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
I'm using Hibernate 2.1.1.
I'm getting really desperate because I just can't make this work. In the "Testing code" section below I'm using parameters that really exist in the "OP_TURNOS" table!
Every time I try to execute the code I get:

ERROR: ERROR: op_cont_rej_op_turnos_fk referential integrity violation - key referenced from op_controlo_rejeicao not found in op_turnos
( ... )

I'm printing every property before the "session.save" and all are ok.


NOTE: I didn't put the mapping files and POJO's for "Op", "Turno" and "TipoRejeicao" so that the text wouldn't be to long to read. There simple tables with an "id" as PK.

----------
DDL's
----------

CREATE TABLE OP_TURNOS (
op_fk INT8 NOT NULL
, turno_fk SMALLINT NOT NULL
, data_turno INT8 NOT NULL
, user_id CHAR(10) NOT NULL
, PRIMARY KEY (op_fk, turno_fk, data_turno)
, CONSTRAINT op_turnos_op_fk FOREIGN KEY (op_fk)
REFERENCES OP (id)
, CONSTRAINT op_turnos_turnos_fk FOREIGN KEY (turno_fk)
REFERENCES TURNOS (id)
);

CREATE TABLE OP_CONTROLO_REJEICAO (
op_fk INT8 NOT NULL
, turno_fk SMALLINT NOT NULL
, data_turno_fk INT8 NOT NULL
, tipo_rejeicao_fk INT8 NOT NULL
, user_id CHAR(10) NOT NULL
, PRIMARY KEY (op_fk, turno_fk, data_turno_fk, tipo_rejeicao_fk)
, CONSTRAINT tipo_rejeicao_fk FOREIGN KEY (tipo_rejeicao_fk)
REFERENCES TIPO_REJEICAO (id)
, CONSTRAINT op_cont_rej_op_turnos_fk FOREIGN KEY (op_fk, turno_fk, data_turno_fk)
REFERENCES OP_TURNOS (op_fk, turno_fk, data_turno)
);

-----------------------------------------------------
Mapping files (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"
/>

<!-- associations -->
<!-- bi-directional one-to-many association to OpControloRejeicao -->
<set
name="opControloRejeicaos"
lazy="true"
inverse="true"
>
<key>
<column name="op_fk" />
<column name="turno_fk" />
<column name="data_turno_fk" />
</key>
<one-to-many
class="pt.vo.OpControloRejeicao"
/>
</set>

</class>
</hibernate-mapping>

OpControloRejeicao

<?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.OpControloRejeicao"
table="op_controlo_rejeicao"
>

<composite-id name="comp_id" class="pt.vo.OpControloRejeicaoPK">
<!-- bi-directional many-to-one association to TipoRejeicao -->
<key-many-to-one
name="tipoRejeicao"
class="pt.vo.TipoRejeicao"
>
<column name="tipo_rejeicao_fk" />
</key-many-to-one>
<!-- bi-directional many-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="userId"
type="java.lang.String"
column="user_id"
not-null="true"
length="10"
/>

<!-- associations -->

</class>
</hibernate-mapping>

----------
POJO's
----------

OpTurno.java

package pt.vo;

import java.io.Serializable;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class OpTurno implements Serializable {

/** identifier field */
private pt.vo.OpTurnoPK comp_id;

/** persistent field */
private String userId;

/** persistent field */
private Set opControloRejeicaos;

/** full constructor */
public OpTurno(pt.vo.OpTurnoPK comp_id, String userId, Set opControloRejeicaos) {
this.comp_id = comp_id;
this.userId = userId;
this.opControloRejeicaos = opControloRejeicaos;
}

/** default constructor */
public OpTurno() {
}

public pt.vo.OpTurnoPK getComp_id() {
return this.comp_id;
}

public void setComp_id(pt.vo.OpTurnoPK comp_id) {
this.comp_id = comp_id;
}

public String getUserId() {
return this.userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public Set getOpControloRejeicaos() {
return this.opControloRejeicaos;
}

public void setOpControloRejeicaos(Set opControloRejeicaos) {
this.opControloRejeicaos = opControloRejeicaos;
}

public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof OpTurno) ) return false;
OpTurno castOther = (OpTurno) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}
}

OpTurnoPK.java

package pt.vo;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class OpTurnoPK implements Serializable {

/** identifier field */
private long dataTurno;

/** identifier field */
private pt.vo.Turno turno;

/** identifier field */
private pt.vo.Op op;

/** full constructor */
public OpTurnoPK(long dataTurno, pt.vo.Turno turno, pt.vo.Op op) {
this.dataTurno = dataTurno;
this.turno = turno;
this.op = op;
}

/** default constructor */
public OpTurnoPK() {
}

public long getDataTurno() {
return this.dataTurno;
}

public void setDataTurno(long dataTurno) {
this.dataTurno = dataTurno;
}

public pt.vo.Turno getTurno() {
return this.turno;
}

public void setTurno(pt.vo.Turno turno) {
this.turno = turno;
}

public pt.vo.Op getOp() {
return this.op;
}

public void setOp(pt.vo.Op op) {
this.op = op;
}

public String toString() {
return new ToStringBuilder(this)
.append("dataTurno", getDataTurno())
.append("turno", getTurno())
.append("op", getOp())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof OpTurnoPK) ) return false;
OpTurnoPK castOther = (OpTurnoPK) other;
return new EqualsBuilder()
.append(this.getDataTurno(), castOther.getDataTurno())
.append(this.getTurno(), castOther.getTurno())
.append(this.getOp(), castOther.getOp())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getDataTurno())
.append(getTurno())
.append(getOp())
.toHashCode();
}
}

OpControloRejeicao.java

package pt.vo;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class OpControloRejeicao implements Serializable {

/** identifier field */
private pt.vo.OpControloRejeicaoPK comp_id;

/** persistent field */
private String userId;

/** full constructor */
public OpControloRejeicao(pt.vo.OpControloRejeicaoPK comp_id, String userId) {
this.comp_id = comp_id;
this.userId = userId;
}

/** default constructor */
public OpControloRejeicao() {
}

public pt.vo.OpControloRejeicaoPK getComp_id() {
return this.comp_id;
}

public void setComp_id(pt.vo.OpControloRejeicaoPK comp_id) {
this.comp_id = comp_id;
}

public String getUserId() {
return this.userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof OpControloRejeicao) ) return false;
OpControloRejeicao castOther = (OpControloRejeicao) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}
}

OpControloRejeicaoPK.java

package pt.vo;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class OpControloRejeicaoPK implements Serializable {

/** identifier field */
private pt.vo.TipoRejeicao tipoRejeicao;

/** identifier field */
private pt.vo.OpTurno opTurno;

/** full constructor */
public OpControloRejeicaoPK(pt.vo.TipoRejeicao tipoRejeicao, pt.vo.OpTurno opTurno) {
this.tipoRejeicao = tipoRejeicao;
this.opTurno = opTurno;
}

/** default constructor */
public OpControloRejeicaoPK() {
}

public pt.vo.TipoRejeicao getTipoRejeicao() {
return this.tipoRejeicao;
}

public void setTipoRejeicao(pt.vo.TipoRejeicao tipoRejeicao) {
this.tipoRejeicao = tipoRejeicao;
}

public pt.vo.OpTurno getOpTurno() {
return this.opTurno;
}

public void setOpTurno(pt.vo.OpTurno opTurno) {
this.opTurno = opTurno;
}

public String toString() {
return new ToStringBuilder(this)
.append("tipoRejeicao", getTipoRejeicao())
.append("opTurno", getOpTurno())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof OpControloRejeicaoPK) ) return false;
OpControloRejeicaoPK castOther = (OpControloRejeicaoPK) other;
return new EqualsBuilder()
.append(this.getTipoRejeicao(), castOther.getTipoRejeicao())
.append(this.getOpTurno(), castOther.getOpTurno())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getTipoRejeicao())
.append(getOpTurno())
.toHashCode();
}
}

----------------
Testing code
----------------

( ... )
OpControloRejeicao op_controlo_rej = new OpControloRejeicao();
OpControloRejeicaoPK op_controlo_rej_pk = new OpControloRejeicaoPK();

TipoRejeicao tipo_rejeicao = new TipoRejeicao();
tipo_rejeicao.setId(new Long(1));
op_controlo_rej_pk.setTipoRejeicao(tipo_rejeicao);

OpTurnoPK op_turno_pk = new OpTurnoPK();
op_turno_pk.setDataTurno(new Long(21122003).longValue());

Op op = new Op();
op.setId(new Long(1));
op_turno_pk.setOp(op);

Turno turno = new Turno();
turno.setId(new Short(new Integer(2).shortValue()));
op_turno_pk.setTurno(turno);

OpTurno op_turno = new OpTurno();
op_turno.setComp_id(op_turno_pk);
op_controlo_rej_pk.setOpTurno(op_turno);

op_controlo_rej.setComp_id(op_controlo_rej_pk);
op_controlo_rej.setUserId("mike");
HashSet op_cont_rejs = new HashSet();
op_cont_rejs.add(op_controlo_rej);
op_turno.setOpControloRejeicaos(op_cont_rejs);
( ... )


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 1:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
What do you persist ? (saveOrUpdate)
try to play with cascade, the OpTurno must eb saved before the OpControloRejeicao. No cascade is defined right now, so Hibernate can't automatically insert it in proper order.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 4:34 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
epbernard wrote:
What do you persist ? (saveOrUpdate)
try to play with cascade, the OpTurno must eb saved before the OpControloRejeicao. No cascade is defined right now, so Hibernate can't automatically insert it in proper order.


I want to persist "op_controlo_rej" of type OpControloRejeicao.
What I do is create a OpControloRejeicaoPK to populate "op_controlo_rej" PK identifier.
I just want to do a "session.save(op_controlo_rej)".
I don't want to save a OpTurno because it's already in the DB. OpTurno is one of the properties of OpControloRejeicaoPK.

Any ideas please?
Need help on this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 4:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
nerotnt wrote:
I don't want to save a OpTurno because it's already in the DB. OpTurno is one of the properties of OpControloRejeicaoPK.

referential integrity violation means OpTurno is not in the DB.
You don't have to save it, load it and set it, Hibernate is clever enough to know that you did not do any change on OpTurne and won't update it.

Beware, you mismatch object and tables in your head:
session.save(op_controlo_rej) is a non sence
session.save(OpControloRejeicao) is what you have to "think"

_________________
Emmanuel


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