-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problem in many-to-many with intermediate entity
PostPosted: Mon Nov 13, 2006 12:50 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Hello:

Please, need your help. I have

Teacher ---many-to-many --- Course

then, I did create a new entity (intermediate), because I have properties in the relation, This intermediate entity is TeacherCourse, then

Teacher ---one-to-many ---TeacherCourse
TeacherCourse --- many-to-one Course

My problem is:

I have 5 courses in DB, (a,b,c,d,e), and I have 2 teachers (F and G).
F to teach a,b,c
G to teach d, e

it's work fine, but if I attempt give other course for G, and this course belong F, it's don't work. Back to front is the same, I can't give to F a G's course.

I use set. Bag is the solution??? Other???

Please Help me

Hibernate version: Hibernate 3.1.3

Mapping documents:

This is TeacherCourse

<?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.TeacherCourse" table="TEACHER_COURSE">

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

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

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

<many-to-one
name="course"
class="encuestas.Course"
>
<column name="courseID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>

Course:

<?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.Course" table="COURSE">

<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="Teacher_course"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="courseID" />
</key>
<one-to-many class="encuestas.TeacherCourse"/>
</set>
</class>
</hibernate-mapping>

Teacher, is subclass of Persona
..
<joined-subclass name="encuestas.Teache" table="Teacher">
<key column="Teacher_ID"/>
<property name="tipo"
type="string"
not-null="true">
<column name="TIPO"/>
</property>

<set name="Teacher_course"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="teacherID" />
</key>
<one-to-many class="encuestas.TeacherCourse"/>
</set>

</joined-subclass>
..

Extract for Course

public class Course implements Serializable{

private Long id;
...
private Set Teacher_course = new HashSet();
..
<getter and setter>
---
and TeacherCourse

public class TeacherCourse implements Serializable {
private Long id;
..
private Course course;
private Teacher teache;
<getters and setters>
...

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

Query q = sess.createQuery("from Persona p where p.rut = :rut");
q.setString("rut", rut);
Teacher p = (Teacher) q.uniqueResult();
p.getId();
Course r = (Course) sess.load(Course.class, id_ramo);
TeacherCourse rp = new TeacherCourse();
rp.setSemestre_dicta(sem_act);
rp.setTeacher(p);
rp.setCourse(r);
r.getTeacher_course().add(rp);
p.getTeacher_course().add(rp);
sess.save(rp);



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

Please, help me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 15, 2006 8:17 am 
Newbie

Joined: Fri Feb 24, 2006 3:09 am
Posts: 6
The solution is use many-to-many instead of one-to-many in
<set name="Teacher_course"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="teacherID" />
</key>
<one-to-many class="encuestas.TeacherCourse"/>
</set>
and use cascade="all" instead of just delete.
For additonal help use the link http://forum.hibernate.org/viewtopic.ph ... 38#2329653
-Poonam


Top
 Profile  
 
 Post subject: Many to many ...
PostPosted: Wed Nov 15, 2006 9:56 am 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Thank's, but the mapping in many-to-many don't hold relationship's properties.

Before, I do the mapping with compose element,

<joined-subclass....Teacher>
<set name="courses" lazy="true" table="TEACHER_COURSE">
<key column="teacherID"/>
<composite-element class="encuestas.TeacherCourse">
<parent name="teacher"/>
<many-to-one name="course"
class="encuestas.Course"
column="courseID"
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> -->


but it's not bidirectional, and I need that.

The problem is: One entity (A) have 2 or more relationship of type many-to-one, with bidirection

A many-to-one B
A many-to-one C

In
A a = new A();
a.setProperty1(prop1);
a.setProperty2(prop2);
a.setOneB(b); //b is un object of type B
a.setOneC(c); //c is un object of type C
b.getThe_As().add(a);--->This Ok
sess.save(a); // it's work fine
out.println("hello1"); ---> ok
c.getThe_As().add(a);------> This don't work, bidirection crash
out.println("hello2"); ---> nothing :(

It's a bug????

Someone of Hibernate Team help me, please!!!


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