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.  [ 4 posts ] 
Author Message
 Post subject: How many ways can you map a derived property?
PostPosted: Tue Apr 05, 2005 3:46 am 
Newbie

Joined: Tue Apr 05, 2005 3:26 am
Posts: 3
Hi all,

This is my 2nd week using Hibernate and I've been pretty impressed and overwhelm by all the features. Currently, I am trying to map a "derived property" and not sure the right way to go about it. Here's the problem statement:

I have three tables:

table: SHIRT
ID NUMERIC
SIZE VARCHAR
DESCRIPTION VARCHAR

table: APPLE
ID NUMERIC
COLOR VARCHAR

table: SELLABLE_ITEM_TYPE
ID VARCHAR
NAME VARCHAR

The SELLABLE_ITEM_TYPE table contains only two rows, one for SHIRT and one for APPLE, as you may have guessed.

The Shirt bean contains the following properties: id, size, description, and type. The mapping for all the properties is pretty straightforward, except type.

Type is of a SellableItemType object. I'm not quite sure how to tell Hibernate to map this.

A few things I've tried:

Code:
<one-to-one name="type" >
    <formula>id</formula>
    <formula>'SHIRT'</formula>
</one-to-one>


but obviously this won't work since SELLABLE_ITEM_TYPE and SHIRT table does not share any property-ref. Is there a way to tell Hibernate that there's no property ref?

Another thing I've tried is using formula and property:

Code:
<property name="type"
   type="com.blah.SellableItemType"
   insert="false"  update="false"
   formula="select * from SELLABLE_ITEM_TYPE SI where SI.ID='SHIRT''"
/>


and I'm getting this error:

Code:
Caused by: java.sql.SQLException: Syntax error or access violation,  message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select * from SELLABLE_ITEM_TYPE SI where SI.ID='SHIRT' as' at line 1"


What's the right way to use formula here? Is it possible to map a formula expression to a complex object?

I read through a few posts but couldn't find similar items discussed. My apology if this is a common question. If so, please send a clue my way so I can narrow my search. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 05, 2005 12:45 pm 
Newbie

Joined: Tue Apr 05, 2005 3:26 am
Posts: 3
Just an update on this one:

I've decided to kill SELLABLE_ITEM_TYPE object and merged its properties to SHIRT and APPLE.

so the mapping becomes simpler:

in SHIRT.hbm.xml:

Code:
      <property name="name"
         insert="false"  update="false"
         formula="'APPLE'"
      />

      <property name="priority"
         insert="false"  update="false"
         formula="(select SIT.priority from SELLABLE_ITEM_TYPE SIT where SIT.name ='SHIRT')"
      />


Still curious though if there's a way in Hibernate to do what I originally set out to do.

The latest thinking was to use formula, get the ID from SELLABLE_ITEM_TYPE and uses a property binder. So, there's a SellableItemTypeEditor with a setAsText(String). I didn't try this so not sure if Hibernate will support it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 07, 2005 7:13 am 
Newbie

Joined: Tue Apr 05, 2005 3:51 am
Posts: 7
Location: Switzerland
I am not sure I understood what you are trying to do, but to me, it looks more like inheritence. If you think in term of classes and not tables, you could probably have a SellableItem interface which is implemented by both Shirt and Apple. Or you cane use a base class "SellableItem" and extend it by both Shirt and Apple. Have a look at "http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-subclass" on how to do it.

But ... I'm not really sure that's what you wanna do ...

Hope it helps !

Guillaume


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 07, 2005 10:51 am 
Newbie

Joined: Tue Apr 05, 2005 3:26 am
Posts: 3
It is related to inheritance but I don't think it's related to this specific question.

To simplify, the question is whether it's possible to have a one-to-many relationship when you don't have a connecting primary/foreign key. So, in terms of object model, your object A wants to have a reference to object B but in the data model, there's nothing that indicates that's the case. Obviously we're dealing with two legacy data models here :) Also, this is read only, no expectation to insert/update/delete on the Association.

Another solution is to add a link table between the two if you're allowed.


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