-->
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: Hibernate Team: This is a bug? Please, I need your help!!!
PostPosted: Tue Nov 21, 2006 3:08 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Hello: I'm a student and I need your help because my proyect depend on hibernate.

I have a one problem when I try to recovery a set from one-to-many relationship.

I have 3 classes: Alumno, Ramo and RamoAlumno. RamoAlumno is intermediate class, because in my model I have
Alumno--many-to-many--Ramo, and I have propieties in the relation.

Then, I have 2 relations from RamoAlumno

Alumno -- one-to-many -- RamoAlumno

and

Ramo -- one-to-many -- RamoAlumno

if I recovery one Alumno (Alumno is subclass of Persona):
Query q = sess.createQuery("from Persona r where r.rut = :rut");
q.setString("rut", rut);
Alumno p = (Alumno) q.uniqueResult();

and I load one Ramo:

Ramo r = (Ramo) sess.load(Ramo.class, id_ramo);

and I create a new RamoAlumno

RamoAlumno rp = new RamoAlumno();
rp.setEstado(estado); //one propiety of RamoAlumno
rp.setSemestre(sem); //other
rp.setAgno(agno); //and other
rp.setAlumno(p); // this is for relation to Alumno
rp.setRamo(r); // and Ramo
sess.save(rp); // this work
p.getRamo_alumno().add(rp); // this work fine
r.getRamo_alumno().add(rp); // this don't work

sess.getTransaction().commit();

This cause a big problem in the application, because I can't recovery set from one class. Neither I can to do

RamoAlumno.getRamo().getId(); // this don't work :(

to do join
if(RamoAlumno.getRamo().getId()==r.getId())

In the schema of database, RamoAlumno have all propieties and fk belong to Alumno and Ramo.

In summary, Why when I have one class with 2 or more relationships (RamoAlumno) just one relation work fine??? The other relation is dead?

Previously, I put others post about this problem, with others case.

Please, help.

Hibernate version: Hibernate 3.1.3

Mapping documents:

Alumno is a subclass of Persona:

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

(propieties.....)

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

</joined-subclass>
</class>

</hibernate-mapping>


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>
(propieties....)

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


</class>

</hibernate-mapping>


RamoAlumno.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.RamoAlumno" table="RAMO_ALUMNO">

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

<property name="semestre"
type="string"
not-null="true"
update="false">
<column name="SEMESTRE"/>
</property>
<property name="agno"
type="int"
not-null="true"
update="false">
<column name="AGNO"/>
</property>
<property name="estado"
type="string"
not-null="true"
>
<column name="ESTADO"/>
</property>

<many-to-one
name="alumno"
class="encuestas.Persona">
<column name="alumnoID" not-null="true" />
</many-to-one>

<many-to-one
name="ramo"
class="encuestas.Ramo"
>
<column name="ramoID" not-null="true" />
</many-to-one>

</class>

</hibernate-mapping>


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

Ramo r = (Ramo) sess.load(Ramo.class, idp);
out.println("xxxxx");

for (Iterator it = r.getRamo_alumno().iterator(); it.hasNext();) {
out.println("yyyyy");
RamoAlumno ra = (RamoAlumno) it.next();
out.println("<p>Alumno: "+ra.getAlumno().getNombre());
}

}
}


sess.getTransaction().commit();


Full stack trace of any exception that occurs:

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:

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 10:15 am 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
Quote:
RamoAlumno rp = new RamoAlumno();
rp.setEstado(estado); //one propiety of RamoAlumno
rp.setSemestre(sem); //other
rp.setAgno(agno); //and other
rp.setAlumno(p); // this is for relation to Alumno
rp.setRamo(r); // and Ramo
sess.save(rp); // this work
p.getRamo_alumno().add(rp); // this work fine
r.getRamo_alumno().add(rp); // this don't work

sess.getTransaction().commit();


Well typically you'd do something like the following:

Ramo r = session.load(...);
Alumno p = session.load(...);

Transaction transaction = sess.getTransaction();
transaction.beginTransaction();
RamoAlumno rp = new RamoAlumno();
rp.setEstado(estado);
rp.setSemestre(sem);
...
r.add( rp );
p.add( rp );
transaction.commit();

And inside your methods you'd have:

class Ramo {

public void add( RamoAlumno ra ) {
ramoAlumnos.add( ra );
ra.setRamo( this );
}
}

and vice versa for your Alumno class with a similiar method.

Remember when you use inverse=true on your one-to-many relationships Hibernate won't modify the DB when you change those. You have to modify the RamoAlumno object for hibernate to notice there has been a change that needs to be saved to the DB. That's why it's a good idea to do the above code and encapsulate those calls behind a method so you don't forget to modify the persistant side of the relationship.

I wonder if you tried removing the session.save() call and just let everything commit when the transaction is committed if it would work.

Charlie


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.