i have a hibernate problem, probably due to lazy/eager loading or something similiar. I have a model class student and a model class century. The student has an attribute century. Before I save the stdent in the dao i save the referenced century. But when I save the student, the century is null everywhere, except for it's name. I tried @Fetch(FetchMode.JOIN), @ManyToOne(fetch = FetchType.EAGER) and getHibernateTemplate().initialize(student.getCentury()); so far but nothing will work. Some ideas would be great, thanks in advance!
Student class:
// @Fetch(FetchMode.JOIN)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "CENTURY_ID")
public Century getCentury() {
return century;
}
StudentDao:
load-method inherited from SuperDao:
@SuppressWarnings("unchecked")
public T load(Long id) {
T object = (T) getSession().get(
classOfData, id);
log.error(String.format("%s with id %d loaded.", CLASS_NAME_OF_DATA, id));
if (object == null) {
log.error("Object not found");
}
return object;
}
save method in StudentDao:
@Override
public Student save(Student student) throws HibernateException {
// getHibernateTemplate().initialize(student.getCentury());
log.error("Saving student:" + student.toString());
Century century = centuryDao.load(student.getCentury().getId());
System.out.println("instudentdao:" + century.getManiple());
centuryDao.save(student.getCentury());
getSession().saveOrUpdate(student);
log.error(String.format("Student with id %d saved.", student.getId()));
return student;
}
StudentAction load method is passed down to Dao:
private void loadStudent(String id) {
create = request.getParameter("mode").equals("create");
edit = request.getParameter("mode").equals("edit");
view = request.getParameter("mode").equals("view");
if (request.getParameter("mode").equals("create"))
student = new Student();
else
student = studentService.load(Long.valueOf(id));
}
log for (fetch = FetchType.EAGER)
2013-11-17 13:15:59,819 ERROR http-bio-8080-exec-9 - <Saving student:Student [id=1, firstName=hans, lastName=wurst, gender=null, city=HAMBURG, postalCode=22123, street=SHEMALESTREET, streetNumber=23, company=Company [id=null, name1=OLE, name2=null, nameShort=null, city=null, postalCode=null, street=null, streetNumber=null, email=null, phone=null, fax=null, status=null, supervisors=null], phone=1234123, email=olko@olko.olko, century=Century [id=null, name=Zenturie, maniple=null, students=null], birthday=Sun Jan 01 00:00:00 CET 2012, birthPlace=MUELLTONNE, userId=123, addressAddition=, status=ACTIVE, supervisor=Supervisor [id=null, firstName=null, lastName=, company=null, email=null, phone=null]]>
log for @JoinColumn(name = "CENTURY_ID")
2013-11-17 13:22:25,266 ERROR http-bio-8080-exec-10 - <Saving student:Student [id=1, firstName=hans, lastName=wurst, gender=null, city=HAMBURG, postalCode=22123, street=SHEMALESTREET, streetNumber=23, company=Company [id=null, name1=OLE, name2=null, nameShort=null, city=null, postalCode=null, street=null, streetNumber=null, email=null, phone=null, fax=null, status=null, supervisors=null], phone=1234123, email=olko@olko.olko, century=Century [id=null, name=Zenturie, maniple=null, students=null], birthday=Sun Jan 01 00:00:00 CET 2012, birthPlace=MUELLTONNE, userId=123, addressAddition=, status=ACTIVE, supervisor=Supervisor [id=null, firstName=null, lastName=, company=null, email=null, phone=null]]>
log for initialize()
2013-11-17 13:25:01,381 ERROR http-bio-8080-exec-10 - <Saving student:Student [id=1, firstName=hans, lastName=wurst, gender=null, city=HAMBURG, postalCode=22123, street=SHEMALESTREET, streetNumber=23, company=Company [id=null, name1=OLE, name2=null, nameShort=null, city=null, postalCode=null, street=null, streetNumber=null, email=null, phone=null, fax=null, status=null, supervisors=null], phone=1234123, email=olko@olko.olko, century=Century [id=null, name=Zenturie, maniple=null, students=null], birthday=Sun Jan 01 00:00:00 CET 2012, birthPlace=MUELLTONNE, userId=123, addressAddition=, status=ACTIVE, supervisor=Supervisor [id=null, firstName=null, lastName=, company=null, email=null, phone=null]]>