-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-one hibernate mapping generating wrong types in POJO
PostPosted: Thu Oct 13, 2005 2:01 pm 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
Using Hibernate 3 with JDK 1.5 on a Mac OS X....

Got what I think is very simple mapping document (see below) which defines a many-to-one relationship between a resource object (resurs) and resources types (i.e. a resource must have a type). However when generating POJO classes from the HibernateToolTask the wrong type is assigned to the many-to-one property. Note, that the foreign key is not mapped to the primary key (identity) rather a unique column (string values).

Code:
<hibernate-mapping>

  <class name="Resurs" table="RESURS">
       
        <id name="id" column="RESURSID">
                <generator class="identity"/>
        </id>

        <many-to-one name="typ" property-ref="typ" class="Resurstyp">
                <column name="RESURSTYP" length="50" not-null="true"/>
        </many-to-one>

        <property name="beteckning">
                <column name="BETECKNING" length="50" not-null="true"/>
        </property>

  </class>
       
  <class name="Resurstyp" table="RESURSTYP">
       
        <id name="id" column="RESURTYPSID">
                <generator class="identity"/>
        </id>

        <property name="typ">
                <column name="RESURSTYP" length="50" not-null="true" unique="true"/>
        </property>

        <property name="beteckning">
                <column name="BETECKNING" length="50" not-null="true"/>
        </property>

  </class>
       
</hibernate-mapping>



The resulting Resurs.java classes starts with:
Code:
public class Resurs  implements java.io.Serializable {

     private Long id;
     private Resurstyp typ;
     private String beteckning;


The type for the typ property should be java.lang.String but instead the associated Class defined in the mapping is used. Why?


/ Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 2:07 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
young_matthewd wrote:
The type for the typ property should be java.lang.String


you have class="Resurstyp" in the mapping document. this is the correct behavior.


Top
 Profile  
 
 Post subject: Associated class
PostPosted: Thu Oct 13, 2005 4:06 pm 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
Ok. But then the hibernate documentation seems to be unclear. Looking at the examples in the packaged PDF reference and on the net the class attribute represents "the name of the associated class" which I assume is the object referenced (i.e. foreign object being keyed on).

How does Hibernate know what class to make the foreign key on? Can you point me to an example that works or is similar to what I want?

Thanks / Matthew


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 4:45 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
if you tell H class="Foo" in a many-to-one, it finds the identifier property for the class Foo. This property corresponds to the primary key of the many side. The PK column should be referenced by whatever column found in the many-to-one element located on the 'one side' (in your case, the column RESURSTYP ).


Top
 Profile  
 
 Post subject: Got it!
PostPosted: Sun Oct 16, 2005 3:28 pm 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
Little mental leap of faith. I assumed that the types of attributes created in the POJO classes would map directly to the database structure. In other words, I accepted that getTyp() in the Resurs class would be a String rather than the Resurstyp class.


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

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.