-->
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.  [ 7 posts ] 
Author Message
 Post subject: Unidirectional manytomany relation not persisting
PostPosted: Tue Jul 15, 2008 9:11 am 
Newbie

Joined: Wed Dec 26, 2007 9:36 am
Posts: 7
I have unidirectional manytomany relation that is not working. Teacher has a reference (collection) for students, but the student does not have reference to the Teacher.

When I merge(teacher) like this getJpaTemplate().merge(teacher), with a new student added to the collection, the relation is not updated in the join table (teacher_student) and there are no exceptions either. However both teacher and student are saved properly in their tables. I have done a lot of searching but still not able to find the solution...please help!

Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Teacher implements Comparable, Serializable {

@Id
protected TeacherId teacherId;

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@BatchSize(size = 3)
protected List<Student> students;

public addOrUpdateStudent(Student student) {
    if (students == null) {
        students = new ArrayList<Student>();
    }
    int index = students.indexOf(student);
    if (index == -1) {
        students.add(student);
    } else {
        students.set(index, student);
    }
      
}
...
}


Code:
@Entity
public class Student implements Comparable, Serializable {
   @Id
   @GeneratedValue
   private int id;
...
}


I am using hibernate 3.2.1.ga, postgres 8.2-504.jdbc3 and java 1.6.0.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 3:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

have you tried to also add
Code:
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)

to your students property?

If this does not help, turn on SQL debug trace and see check which SQL statements gets issued.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 5:21 am 
Newbie

Joined: Wed Dec 26, 2007 9:36 am
Posts: 7
Since I had already included cascade = {CascadeType.PERSIST, CascadeType.MERGE} in my definition I could not see how CascadeType.SAVE_UPDATE would help, however I still tried but it did not help either. The following sqls are generated when I try to add a new student to an already existing teacher:

Code:
Hibernate: select teacher....
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into Student(...)


In sql, it retreives the correct teacher (pessimistic locking) in first select, then select the nextval for student's id, and inserts the student in the database, but there is not statement for putting the relation between teacher and student. Am I missing something here?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 6:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

could you maybe provide the full configuration for Teacher, TeacherId and Student? Ant maybe also the code which creates/modifies the teacher and his students.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 7:11 am 
Newbie

Joined: Wed Dec 26, 2007 9:36 am
Posts: 7
I am working for a client so I had to change the file names etc. However while trying to do these changes for you, I have got a bit closer to the root of the problem.

I have a service class and a DAO class.


Code:
public class TeacherDaoImpl extends JpaDaoSupport implements TeacherDao {
   public Teacher saveUpdateTeacherStudent(Teacher teacher) {
      return getJpaTemplate().merge(teacher);
   }
   
   public Teacher getTeacher(int id) {
      Teacher teacher = getJpaTemplate().find(Teacher.class, id);
      return teacher;
   }
   
   public Teacher attachStudentToTeacher(int teacherId, Student student) {
      Teacher teacher = getTeacher(teacherId);
      teacher.addOrUpdateStudent(student);
      return teacher;
   }
...
}


In my sevice class (where spring transaction are configured), this does not save the relation between teacher and student, but saves the student in its table.
Code:
Teacher teacher = teacherDao.getTeacher(0);
Student student = new Student();
student.setName("Petter Solberg");
teacher.addOrUpdateStudent(student);
teacherDao.saveUpdateTeacherStudent(teacher);


However if I use the other dao method (attachStudentToTeacher) then everything works fine:
Code:
teacherDao.attachStudentToTeacher(0, student);


So this means I have misunderstood something regarding scope of the persistenceContext. Shouldn't it be able to see that the student collection has been changed, when it tries to save the parent teacher?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 2:54 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

it really depends what teacherDao.saveUpdateTeacherStudent(teacher) does. Also how exactly are the transactions configured? Where are the transaction boundaries? I think JpaDaoSupport is a Spring class, right? Does it execute each of its methods in its own transaction? What type of environment are you running in? A J2EE container, standalone, ... ?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 6:07 am 
Newbie

Joined: Wed Dec 26, 2007 9:36 am
Posts: 7
Sorry for the late reply. I have been away for a few days.

Yes JpaDaoSupport is a Spring class which executes each of its methods in its own transaction. Furthermore I have used Spring's declarative transaction on the service layer. Can it be because I have transaction first on service layer and then jpaDaoSupport per method transaction in the dao layer? My service layer is calling methods from dao layer.

saveUpdateTeacherStudent method is a one liner which merges the teacher object. I am running standard Java 6 environment.


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