• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unconsistent entity object collection state

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot
 
reply
    Bookmark Topic Watch Topic
  • New Topic