-->
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.  [ 2 posts ] 
Author Message
 Post subject: Saved data not subsequently retrieved
PostPosted: Wed Apr 04, 2012 10:31 am 
Newbie

Joined: Fri Sep 09, 2011 6:41 am
Posts: 3
Hi,

Hope someone can help. I'm using Spring/Hibernate and I have Hibernate beans representing Course and CourseSession. Course has many CourseSessions represented by @OneToMany. When I add a new CourseSession I want to be able to re-get the Course and see the new CourseSession in the @OneToMany list.

I can see that between saving and getting the Course again the row is already in the database (as expected), so it's not a transaction issue. I'm guessing therefore that Hibernate is giving me a cached version, but surely the cache should contain the update?

My code to save the course session and get the course are below, along with the Course and CourseSession beans. Can anyone tell me why the cache is 'wrong' and what I can do to resolve the problem.

Many thanks,
Neil

Code:
public class CourseSessionsDAOImpl extends HibernateDaoSupport implements CourseSessionsDAO {
...
   public CourseSession save(CourseSession courseSession) {

       CourseSession savedCourseSession = null;
            if (courseSession.getTimestamp()==null) {
                Integer courseSessionId = (Integer) getSession().save( courseSession );
                return findById(courseSessionId);
            }
            else {
                savedCourseSession = (CourseSession) getSession().merge(courseSession);
            }
            return savedCourseSession;
   }
}

public class CoursesDAOImpl extends HibernateDaoSupport implements CoursesDAO {
   public Course findById(java.lang.Integer id) {
      Course instance = (Course) getSession().get("com.encounter.bean.course.Course", id);
      return instance;
   }


Code:
@Entity
@Table(name = "courses", catalog = "richarn")
public class Course implements java.io.Serializable {
        ...
   private List<CourseSession> courseSessions = new ArrayList<CourseSession>(0);

   @OneToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, mappedBy = "course")
   public List<CourseSession> getCourseSessions() {
      return this.courseSessions;
   }
   public void setCourseSessions(List<CourseSession> courseSessions) {
      this.courseSessions = courseSessions;
   }
}

@Entity
@Table(name = "course_sessions", catalog = "richarn")
public class CourseSession implements java.io.Serializable {
   private Integer id;
   private Integer courseId;
   private Course course;
        ...
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "SCHEDULED_COURSE_ID", nullable = false)
   public Course getCourse() {
      return this.course;
   }
   public void setCourse(Course course) {
      this.course = course;
   }
   @Column(name = "SCHEDULED_COURSE_ID", nullable = false, insertable=false, updatable=false)
   public Integer getCourseId() {
      return this.courseId;
   }
   public void setCourseId(Integer courseId) {
      this.courseId = courseId;
   }
}

_________________
Hibernate 3.6.1 : Spring 3.0.3


Top
 Profile  
 
 Post subject: Re: Saved data not subsequently retrieved
PostPosted: Wed Apr 11, 2012 5:54 am 
Newbie

Joined: Fri Sep 09, 2011 6:41 am
Posts: 3
Anyone?

_________________
Hibernate 3.6.1 : Spring 3.0.3


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