-->
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.  [ 2 posts ] 
Author Message
 Post subject: Difficulties when mapping a class to multiple tables
PostPosted: Thu Jan 20, 2005 12:44 pm 
Newbie

Joined: Thu Jan 20, 2005 12:02 pm
Posts: 2
Quote:
Hibernate version: 2

Database: Oracle

Mapping documents:

<hibernate-mapping package="defi.tre.komodo.type.hibernate">
<class name="HibernateWarrant" table="TWRTKOM">

<id column="ID" name="id" type="integer" length="22">
<generator class="assigned"/>
</id>

<property column="NAME" name="name" type="string"/>
<property column="AGE" name="age" type="integer"/>

<property column="PLACE_ID name="placeId" type="integer"/>


<property code but how? >
use a one-to-many ?

<set role="code">
<key column="place_id"/>
<one-to-many class="integer"/>
</set>

</class>
</hibernate-mapping>



Hi!

First, I know that there is no way to map a class to multiple tables in Hibernate 2 (and I can't use Hib3, 'cause the managers fears the "beta" state of the release, too bad... ). So, I just wanted to know a way to write my mapping.

Here is a description:

My warrant has a name, an age, and a code associated with it. The code depends of the place where my warrant live.

The table of the warrant is:

Code:
create table TWRTKOM(
ID NUMBER(22) not null,
NAME VARCHAR2(100),
AGE NUMBER(22),
PLACE_ID NUMBER(22));


the table with the codes is:

Code:
create table TPLCKOM(
PLACE_ID NUMBER(22),
INTERNET_CODE VARCHAR(100));


My POJO is:

Code:
class Warrant {

Long id;
String name;
Integer age;
String code;
Integer codeId;

}


Please, do you know any way to do this kind of mapping?

One of the difficulty is that the code depends on the placeId, and not the warrant id, so there is no one-to-one mapping between warrant table and code table.
The second difficulty is that I want to avoid, if possible, to create a class containing just the code, and the corresponding mapping. So a one-to-many mapping seems a bit too much for this problem, (since there is only one code, not a collection of codes, and since I need to create a wrapping object just containing the fields "id" and "code").
The last little thing is that I want to avoid putting the code directly in the warrant, because I already have the code table, and I can't change it.

It is like getting the property code from another table, depending on the value of the placeId.

I can change the warrant table , if there is a better database design for this problem, but I can't change the code table.

(Sorry for my bad english writing)

Thanks!
Cyril


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 1:14 pm 
Newbie

Joined: Thu Jan 20, 2005 12:02 pm
Posts: 2
I finally solve that by creating an object for the code, and a many-to-one association.

I'm still interested in a more elegant solution (something in the mapping of the warrant), but the creation and the mapping of this simple object is quite easy and straightforward:

Code:
<hibernate-mapping package="hibernate">
   <class name="HibernateWarrant" table="TWRTKOM">
      
      <id column="ID" name="id" type="integer" length="22">
         <generator class="assigned"/>
      </id>

      <property column="NAME" name="name" type="string"/>
      <property column="AGE" name="age" type="integer"/>

<property column="PLACE_ID name="placeId" type="integer"/>


<many-to-one column="PLACE_ID" name="internetCode" class="HibernateInternetCode"/>

   </class>
   
   <class name="HibernateInternetCode" table="TPLCKOM">
      <id column="ID" name="id" type="integer" length="22">
         <generator class="assigned"/>
      </id>
      <property column="INTERNET_CODE" length="100" name="code"/>
   </class>
</hibernate-mapping>


I think this solution will be okay (but perhaps a one-to-many would be better).

Don't hesitate to post another solution for this problem, I would be glad to learn a new way of doing this kind of thing (a reference to another table from a table).

Cyril


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.