-->
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.  [ 2 posts ] 
Author Message
 Post subject: The recovery for a set don't work in 2 of 3 entities in M:1
PostPosted: Thu Nov 02, 2006 12:51 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Hello, I have a problem to retrieve a set of entities. I have the next relationships:

Alumno --- one-to-many --- FormasRptas
Ramo --- one-to-many --- FormasRptas
Forma --- one-to-many --- FormasRptas

For example, I load 3 entities, they have relation with FormasRptas:

Alumno al = (Alumno) sess.load(Alumno.class, idAl);
Ramo r = (Ramo) sess.load(Ramo.class, idRamo);
Forma f = (Forma) sess.load(Forma.class, idForma);

and I create one entity FormaRpta

FormaRpta fr = new FormaRpta();

and I put propieties. Next, I call:

al.getFormas_rptas().add(fr);

and it's work's fine, but when I call

r.getFormas_rptas().add(fr);

this don't work and

f.getFormas_rptas().add(fr);

this don't work :(

When I call

al.getFormas_rptas().iterator();

work's fine too, but

r.getFormas_rptas().iterator();

and

f.getFormas_rptas().iterator();

don't. what is wrong??? Please, help me!!!
Paola

Hibernate version: Hibernate 3.1.3

Mapping documents:

This is Persona.hbm.xml (Alumno is a subclass)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="encuestas.Persona"
table="PERSONA"
lazy="true"
abstract="true">

<id name="id"
type="long"
column="PERSONA_ID">
<generator class="native"/>
</id>
<property name="rut"
type="long"
not-null="true">
<column name="RUT"/>
</property>

<joined-subclass name="encuestas.Alumno" table="ALUMNO">
<key column="ALUMNO_ID"/>

<property name="password"
type="string"
not-null="true">
<column name="PASSW"/>
</property>

<set name="Formas_rptas"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="alumnoID" />
</key>
<one-to-many class="encuestas.FormaRpta"/>
</set>

<set name="ramos" lazy="true" table="ALUMNO_RAMO">
<key column="alumnoID"/>
<composite-element class="encuestas.AlumnoRamo">
<parent name="alumno"/>
<many-to-one name="ramo"
class="encuestas.Ramo"
column="ramoID"
not-null="true"/>
<property name="semestre" column="SEMESTRE" not-null="true"/>
<property name="agno" column="AGNO" not-null="true"/>
<property name="estado" column="ESTADO" not-null="true"/>
</composite-element>
</set>

</joined-subclass>
</class>
</hibernate-mapping>

This is Ramo.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="encuestas.Ramo" table="RAMO">

<id name="id" type="long" column="ID">
<!-- <meta attribute="finder-method">findByID</meta> -->
<generator class="native"/>
</id>
<property name="nombre"
type="string"
not-null="true">
<column name="NOMBRE"/>
</property>

<set name="Formas_rptas"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="ramoID" />
</key>
<one-to-many class="encuestas.FormaRpta"/>
</set>

</class>

</hibernate-mapping>

This is Forma.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="encuestas.Forma" table="FORMA">
<id name="id" type="long" column="ID">
<!-- <meta attribute="finder-method">findByID</meta> -->
<generator class="native" />
</id>
<property name="titulo"
type="string"
not-null="true"
update="false">
<column name="TITULO"/>
</property>

<set name="detalles" lazy="true" inverse="true"
cascade="delete" >
<key>
<column name="formaId" />
</key>
<one-to-many class="encuestas.Detalle"/>
</set>

<set name="Formas_rptas"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="formaID" />
</key>
<one-to-many class="encuestas.FormaRpta"/>
</set>

</class>

</hibernate-mapping>

And, this is FormaRpta

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="encuestas.FormaRpta" table="FORMA_RPTA">

<id name="id" type="long" column="ID">
<!-- <meta attribute="finder-method">findByID</meta> -->
<generator class="native"/>
</id>

<property name="Fecha_rpta"
type="string"
not-null="true"
update="false">
<column name="FECHA_RPTA"/>
</property>

<many-to-one
name="alumno"
class="encuestas.Persona"
>
<!-- Used by code generator
<meta attribute="finder-method">findByAuthorID</meta> -->
<!-- Used as a DDL hint -->
<column name="alumnoID" not-null="true" />
</many-to-one>

<many-to-one
name="forma"
class="encuestas.Forma"
>
<!-- Used by code generator
<meta attribute="finder-method">findByAuthorID</meta> -->
<!-- Used as a DDL hint -->
<column name="formaID" not-null="true" />
</many-to-one>

<many-to-one
name="ramo"
class="encuestas.Ramo"
>
<!-- Used by code generator
<meta attribute="finder-method">findByAuthorID</meta> -->
<!-- Used as a DDL hint -->
<column name="ramoID" not-null="true" />
</many-to-one>

</class>

</hibernate-mapping>

The classes:

public class Ramo implements Serializable{

private Long id;
private String nombre;
private Set alumnos = new HashSet();
private Set Formas_rptas = new HashSet();

public Ramo() {};

public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getNombre(){
return nombre;
}
public void setNombre(String nombre){
this.nombre = nombre;
}

public Set getAlumnos(){
return alumnos;
}
public void setAlumnos(Set alumnos){
this.alumnos = alumnos;
}
public void addAlumno(Alumno alumno) {
if(alumno == null)
throw new IllegalArgumentException("Alumno Nulo!");
alumno.getRamos().add(this);
alumnos.add(alumno);

}

public Set getFormas_rptas(){
return Formas_rptas;
}
public void setFormas_rptas(Set formas_rptas){
this.Formas_rptas = formas_rptas;
}
}


public class Forma implements Serializable{

private Long id;
private String titulo;
private Set detalles = new HashSet();
private Set Formas_rptas = new HashSet();

public Forma() {}

public Long getId(){
return id;
}
public void setId(Long alumno_id){
this.id = alumno_id;
}

public String getTitulo(){
return titulo;
}
public void setTitulo(String titulo_alum){
this.titulo = titulo_alum;
}
public Set getDetalles(){
return detalles;
}
public void setDetalles(Set detalles){
this.detalles = detalles;
}

public void addDetalle(Detalle detalle) {
if(detalle == null)
throw new IllegalArgumentException("detalle Nulo!");
detalle.setUna_forma(this);
detalles.add(detalle);
}
public Set getFormas_rptas(){
return Formas_rptas;
}
public void setFormas_rptas(Set formas_rptas){
this.Formas_rptas = formas_rptas;
}
}

public class Alumno extends Persona
implements Serializable
{
//private long id;
private String password;
private Set Formas_rptas = new HashSet();
private Set ramos = new HashSet();

public Alumno() {super();}

public String getPassword() {
return password;
}
public void setPassword(String pass) {
this.password = pass;
}
public Set getFormas_rptas(){
return Formas_rptas;
}
public void setFormas_rptas(Set formas_rptas){
this.Formas_rptas = formas_rptas;
}

public void addFormas_rptas(FormaRpta forma) {
if(forma == null)
throw new IllegalArgumentException("forma rpta Nula!");
forma.setAlumno(this);
Formas_rptas.add(forma);
}

public Set getRamos(){
return ramos;
}
public void setRamos(Set ramos){
this.ramos = ramos;
}

public void addRamo(Ramo ramo) {
if(ramo == null)
throw new IllegalArgumentException("Ramo Nulo!");
ramo.getAlumnos().add(this);
ramos.add(ramo);

}
}

public class FormaRpta
implements Serializable
{
private Long id;
private String Fecha_rpta;
private Alumno alumno;
private Forma forma;
private Ramo ramo;

public FormaRpta() {}

public Long getId(){
return this.id;
}
public void setId(Long forma_rpta_id){
this.id = forma_rpta_id;
}

public String getFecha_rpta(){
return Fecha_rpta;
}
public void setFecha_rpta(String fecha_rpta){
this.Fecha_rpta = fecha_rpta;
}

public Alumno getAlumno(){
return alumno;
}
public void setAlumno(Alumno alum){
this.alumno = alum;
}

public Forma getForma(){
return forma;
}
public void setForma(Forma forma){
this.forma = forma;
}

public Ramo getRamo(){
return ramo;
}
public void setRamo(Ramo ramo){
this.ramo = ramo;
}
}

Code between sessionFactory.openSession() and session.close():

Long idAl = new Long(1);
Long idRamo = new Long(1);
Long idForma = new Long(1);

Alumno al = (Alumno) sess.load(Alumno.class, idAl);
Ramo r = (Ramo) sess.load(Ramo.class, idRamo);
Forma f = (Forma) sess.load(Forma.class, idForma);
out.println("\n de ALUMNO");
for (Iterator it = al.getFormas_rptas().iterator(); it.hasNext();) {
FormaRpta frp = (FormaRpta) it.next();
out.println(" FR: "+frp.getId());
}

out.println("\n de RAMO");
for (Iterator it = r.getFormas_rptas().iterator(); it.hasNext();) {
FormaRpta frp = (FormaRpta) it.next();
out.println(" FR: "+frp.getId());
}

out.println("\n de FORMA");
for (Iterator it = f.getFormas_rptas().iterator(); it.hasNext();) {
FormaRpta frp = (FormaRpta) it.next();
out.println(" FR : "+frp.getId());
}

Full stack trace of any exception that occurs:

No errors in compile. Just when I load the page web, print fine to Alumno, but not for Ramo y Forma (is blank, print nothing)

Name and version of the database you are using:

Mysql 5.0.22-Debian_0ubuntu6.06.2

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

thanks for read this. Please, help me if you can.


Top
 Profile  
 
 Post subject: Now I know why: ternary relation problem
PostPosted: Fri Nov 10, 2006 7:58 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
I did have 4 entities, 3 in one-to-many relation. with 3 is a ternary relationship (and I was mapping wrong). I' ll read solutions about that. Thank's for read me, and I m sorry for don't realizing before.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.