Results 1 to 6 of 6

Thread: Initializing of properties

  1. #1

    Default Initializing of properties

    HI All

    I have a problem that I just cant seem to solve. In my DAO that extends HibernateDaoSupport I execute the following code:

    List students = getHibernateTemplate( ).find( "from Student s where s.userId=? and s.password=?", new Object[ ] { userId, password } );

    Student is a class with a user and password property that has been mapped to 2 fields. It also contains a reference to a teacher object. It is mapped to the teacher as a many-to-one association.

    The teacher object has a set of students that is also mapped.

    I use a valid login to verify a student and indead the student is loaded using hibernate. The problem is that the teacher property in the Student class isnt initialized. This is correct. I want to control where the teacher property is loaded.

    In my DAO I have this code:
    Student student = students.get(0);
    student.getTeacher();
    student.getTeacher().getName(); // Name is a property of teacher

    I have tried litterly everything. I have put lazy="true", lazy="false" everywhere in the mappings of Student and Teacher. I have even called getHibernateTemplate().initialize(student.getTeach er()). Nothing and I mean nothing seams to initizlize the teacher object. I dont get any errors or nothing. When I debug using eclipse it indicates that all the fields in the Teacher object are null.

    To make it clear I only want to initialize the teacher object when I call student.getTeacher() in my DAO

    Can anybody help?

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    If there is a relationship between the teacher and the student (java code, hibernate mappings and database) then Hibernate will load it. If in your case it doesn't then either the relationship is not seen by Hibernate or the relationship is null - i.e. you have a student without a teacher.
    Take a look at the Hibernate or Spring samples - both provide examples of one-to-many mappings.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Is student or teacher final? If so, CGLIB cannot create a subclass and will (almost) silently fail.
    Colin Yates
    SpringSource - http://www.springsource.com - Spring Training, Consulting, and Support - "From the Source"
    Please read http://www.springframework.org/documentation
    Co-Author of Expert Spring MVC + Web Flow.

  4. #4

    Default

    The teacher and student properties aren't final.

    Teacher has a private Set<Student> students with a getter and setter
    Student has a private Teacher teacher with a getter and setter

    When I load a teacher then the students list is initialized. The other way around, initializing the teacher when loading a student doesnt work.

    Student.java contains
    /**
    * @hibernate.many-to-one name="teacher" column="TeacherId" not-null="false" class="com.test.model.Teacher"
    */

    Teacher.java contains
    /**
    * @hibernate.set inverse="true" cascade="all-delete-orphan"
    * @hibernate.collection-key column="TeacherId"
    * @hibernate.collection-one-to-many class="com.test.model.Student" column="TeacherId"
    */

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Ah; I do not use the hibernate annotations

    It is a very very simple relationship you are defining, so I suspect it is missing data (i.e. the foreign key Student.teacher is null).

    Sorry I cannot be more help.
    Colin Yates
    SpringSource - http://www.springsource.com - Spring Training, Consulting, and Support - "From the Source"
    Please read http://www.springframework.org/documentation
    Co-Author of Expert Spring MVC + Web Flow.

  6. #6

    Default

    A hbm.xml file has been generated. When it comes down to the student teacher relation it contains this:

    <many-to-one
    name="teacher"
    class="com.test.model.Teacher"
    cascade="none"
    outer-join="true"
    update="true"
    insert="true"
    column="TeacherId"
    not-null="false"
    />

    If I load the teacher then the student is in the teachers list of students so the database has the right information.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •