-->
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: Mapping question
PostPosted: Mon Jun 13, 2005 3:19 pm 
Newbie

Joined: Fri May 20, 2005 5:30 pm
Posts: 9
Hibernate version: 3

I have 2 tables : Client and Type. The Type table has a primary defined with 3 fields : APPLICATION, TABLE and CODE.

I would like to map a field corresponding to the client type.
How can I tell hibernate that my field clientTypeCode correspond to a TypeCode class for APPLICATION = 'XXX', TABLE = 'Client' and CODE = Client.clientTypeCode ?

I can put multiple columns in the many-to-one tag, but can I put static data as reference?

M_A


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 11:56 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Can you post more specific details? Do you have the valid clientTypeCodes stored in the DB, or are they hard-coded into your app?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 9:51 am 
Newbie

Joined: Fri May 20, 2005 5:30 pm
Posts: 9
Here's the details :

Table Client :

id number
Name varchar
Type varchar

Primary key : id

Table Type :

Application varchar
Table varchar
Code varchar

primary key : (Application, Table, Code)

Mapping Type :

<class name="Type" table="Type" lazy="false">
<composite-id>
<key-property name="Application" column="Application" type="java.lang.String" />
<key-property name="Table" column="Table" type="java.lang.String" />
<key-property name="Code" column="Code" type="java.lang.String" />
</composite-id>
</class>

Mapping Client :

<class name="Client" table="Client" lazy="false">
<id name="id" column="id" type="java.lang.Long">
<generator class="sequence">
<param name="sequence">CLIENT_ID</param>
</generator>
</id>

<property name="name" column="name" type="java.lang.String" not-null="true" />


!!!! Here's my problem :
<many-to-one name="type" column="Type" class="Type" />


The type is referencing the column Code of the table Type. Application and table must be provided hard-coded!!!

</class>

M_A


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 10:26 am 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
I'm having trouble understanding your relationship between the two tables. The foreign key to Type from Client must have the same columns as the primary key for Type, otherwise there is no way to create a relationship. The composite-id columns MUST match the foreign key columns, AFAIK.

Try putting a surrogate key (sequence number ID) on the type table, and then use that as the foreign key on the Client table for the type column. You can then create a many-to-one to the Type. Conversely, add all of the Type primary key columns as a foreign key on the Client.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 10:31 am 
Newbie

Joined: Fri May 20, 2005 5:30 pm
Posts: 9
I know this is bad design, but I can't change the tables.

Thnaks for your suggestions.

M_A


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.