0

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]]>
olkoza
  • 715
  • 2
  • 17
  • 35
  • S, you're calling this method with a Student whose century only has its name initialized. The (main) problem is not in this method, but in the code that calls this method. How do you create the student and its century before calling this method? – JB Nizet Nov 17 '13 at 12:38
  • The StudentDao inherits from a generic 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; }` – olkoza Nov 17 '13 at 12:58
  • Yes, so what? Where is the code that creates the student and its century, and then calls the save method? – JB Nizet Nov 17 '13 at 13:03
  • `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)); }` The load method is passed down to the Dao. Sorry i'm trying to figure out how to format code right in comments. – olkoza Nov 17 '13 at 13:05
  • Don't post code in comments. Edit your question. – JB Nizet Nov 17 '13 at 13:09
  • ok thanks i did not see the hint in the commentfield – olkoza Nov 17 '13 at 13:27

0 Answers0