-->
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.  [ 1 post ] 
Author Message
 Post subject: hbm2java uses java.util.Map gets/sets for many-to-one? why?
PostPosted: Wed Mar 09, 2005 11:14 am 
Newbie

Joined: Wed Mar 09, 2005 10:22 am
Posts: 1
Hello. I have attached the relevant parts of my mapping document below.
I have a one-to-many relationship between a Course object and an Event object, since a course may be presented numerous times.

I'm using hbm2java to create my java objects. The Course object is created as I would expect with a Set of event objects, but the Event object is not. I would expect the Event object to contain a reference to a single Course object, but instead the generated code uses java.util.Map. When I tried to use the code as generated, I received an "object is not an instance of declaring class". When I change my generated pojo to reference a single Course object, it works great.

My questions are this:
    Do I have my many-to-one relationship specified correctly in the Event object? I followed examples in the documentation.
    Why does hbm2java create the course object gets/sets as Map objects instead of references to a single Course class?
    What is the advantage of this?
    If this is the default code generation is correct, how do I use a HashMap to store a reference to a single object correctly?



Thank you for your time,
Perry Tew


Generated Code by hbm2java for Event.java:
Code:
/** nullable persistent field */
private java.util.Map course;

public java.util.Map getCourse() {
   return this.course;
}

public void setCourse(java.util.Map course) {
   this.course = course;
}



The altered Event.java that works:
Code:
/** nullable persistent field */
private Course course;

public Course getCourse() {
   return this.course;
}

public void setCourse(Course course) {
   this.course = course;
}



My attempt to use the generated code, which failed (the Course object does exist in the db with the proper id):
Code:
Session hSession = HibernateUtil.currentSession();
Long courseId = new Long(1);

Course course =  (Course) hSession.load(Course.class, courseId);

Event event = new Event();
event.setDateOfMeeting(new Date());
event.setCity("Atlanta");
event.setState("GA");
event.setStartTime("now");
event.setEndTime("then");
event.setNumberOfAttendees(new Integer(5));
event.setContactName("joe");
event.setContactPhone("123");
event.setContactEmail("joe@aol.com");

//============================
HashMap courses = new HashMap();
courses.put(course, course);  // not sure where to put the course? key, value?  I tried both.
event.setCourse(courses);
//============================

Transaction tx = hSession.beginTransaction();
hSession.saveOrUpdate(event);
tx.commit();




Here is my code using the Event class as I modified it to contain a single reference to a Course object. It works well. (the Course object does exist in the db with the proper id):

Code:
Session hSession = HibernateUtil.currentSession();
Long courseId = new Long(1);

Course course =  (Course) hSession.load(Course.class, courseId);

Event event = new Event();
event.setDateOfMeeting(new Date());
event.setCity("Atlanta");
event.setState("GA");
event.setStartTime("now");
event.setEndTime("then");
event.setNumberOfAttendees(new Integer(5));
event.setContactName("joe");
event.setContactPhone("123");
event.setContactEmail("joe@aol.com");

//============================
event.setCourse(course);
//============================


Transaction tx = hSession.beginTransaction();
hSession.saveOrUpdate(event);
tx.commit();






Hibernate version: 3.0
Name and version of the database you are using: Oracle 9.2.0
The generated SQL (show_sql=true): N/A
Debug level Hibernate log excerpt: N/A
Mapping documents:
Code:
<hibernate-mapping default-lazy="true">

   <class name="cv.speakerbureau.Course" table="spk_course" select-before-update="true" rowid="rowid">
      <id name="id" column="course_id" type="long" unsaved-value="null">
         <generator class="sequence">
            <param name="sequence">spk_id_seq</param>
         </generator>
      </id>

      <property name="id" column="course_id" type="long" insert="false" update="false" not-null="true"/>
      <property name="title" column="title" type="string" length="200" not-null="true"/>
      <property name="courseNo" column="course_num" type="string" length="20" not-null="true"/>

      <set name="events" lazy="true">
         <key column="course_id"/>
         <one-to-many class="cv.speakerbureau.Event"/>
      </set>

  </class>



   <class name="cv.speakerbureau.Event" table="spk_event" select-before-update="true" rowid="rowid">
      <id name="id" column="event_id" type="long" unsaved-value="null">
         <generator class="sequence">
            <param name="sequence">spk_id_seq</param>
         </generator>
      </id>

      <property name="id" column="event_id" type="long" insert="false" update="false" not-null="true"/>


      <property name="dateOfMeeting" column="date_of_meeting" type="date" not-null="true"/>
      <property name="city" column="city" type="string" length="50" not-null="true"/>
      <property name="state" column="state" type="string" length="2" not-null="true"/>
      <many-to-one name="course" column="course_id" class="cv.speakerbureau.Course" not-null="false" fetch="join" />

   </class>

</hibernate-mapping>




Here is the log output from hbm2java.:

DEBUG 2005-03-09 08:29:06,736 [main] (ClassMapping.java:68) - Processing mapping for class: cv.speakerbureau.Event
INFO 2005-03-09 08:29:06,737 [main] (FieldProperty.java:52) - fieldname = id
INFO 2005-03-09 08:29:06,741 [main] (FieldProperty.java:52) - fieldname = dateOfMeeting
INFO 2005-03-09 08:29:06,742 [main] (FieldProperty.java:52) - fieldname = city
INFO 2005-03-09 08:29:06,743 [main] (FieldProperty.java:52) - fieldname = state
INFO 2005-03-09 08:29:06,754 [main] (FieldProperty.java:52) - fieldname = course


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.