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;
}
}