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.
|