• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unconsistent entity object collection state

 
moaad elamrani
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am having inconsistent object state when using Hibernate

I have class School that have a collection of student
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "LINK_SCH_STUDENT", joinColumns = @JoinColumn(name = "SCHOOL_ID"),
inverseJoinColumns = @JoinColumn(name = "STUDENT_ID"))
@LazyCollection(LazyCollectionOption.EXTRA)
private List<Students> associatedStudents;

I have a transactional method that links a student to a school:

@Transactional
LinkStudentToSchool (schoolId, StudendID){
flush();
int result = getSession().createSQLQuery("INSERT INTO " + getSchemaName()
+ ".LINK_SCH_STUDENT(SCHOOL_ID, STUDENT_ID) " +
"Values (:recipient_group_id, :contentId)")
}

I am writing integration test for this method.

@Transactional
TestLinkStudentToSchool {
school =new School ("GeorgiaTech", ....)
schoolId = saveOrUpdate (school)
studentID = saveOrUpdate (new Student ("LazyStudent", ....))

LinkStudentToSchool (schoolId, studentID)

school.getAssociatedStudent // RETURN NULL !!!
}

Why is this happening, I already called a transactional method to add a student a school (update the link table), but the state of the collection of students
in the school did not get updated !!

Is this a caching issue ? is it because I have nested @transactional?
I will really appreciate any feedback

Thanks


 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not be writing Insert statements. Hibernate will take care of persisting things when the @Transactional method ends and the transaction is flushed, otherwise you are going around the SessionFactory and Hibernate loses track of what is going on. ORM's like Hibernate deal with objects. You should be persisting objets not worrying about writing the queries that insert id's etc.

What happens if you just create the object and call session.save(...) in the @Transactional method (without the insert query)
 
moaad elamrani
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic