-->
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: component vs entity mapping
PostPosted: Tue Mar 13, 2007 5:28 pm 
Newbie

Joined: Tue Aug 29, 2006 2:10 pm
Posts: 11
Given the following tables and no possibility of changing them

CREATE TABLE CAR (
ID NUMBER (10),
NAME VARCHAR (50) NOT NULL,
PRIMARY KEY (ID)
);

CREATE TABLE PART (
CAR_ID NUMBER (10),
SEQUENCE NUMBER(3),
NAME VARCHAR (50),
PRIMARY KEY (CAR_ID, SEQUENCE),
FOREIGN KEY (CAR_ID) REFERENCES CAR(ID)
);

What is the most effective mapping? I want value semantics on PART (i.e. when I delete a CAR, I want all associated PARTs deleted as well). It seems composite-element mappings only allow you to use the primary key of the parent as the child's primary key (or perhaps I'm just mapping it wrong - a real possibility).


Top
 Profile  
 
 Post subject: Looks okay to me...
PostPosted: Tue Mar 13, 2007 8:53 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
It looks like you've got a one to many relationship set up just fine. The problem I've found as a newcomer to Hibernate is that if you get away from a easily defined "id" in your entity then mapping things seems to become exponentially harder.

In my entities always using <id> (usually a simple integer sequence or auto-increment in MySQL) as well as a <natural-id>. Anyway, since you can't change PARTS I'd consider making that a component instead of an entity.

Maybe a mapping in your Car entity like this might work... (I haven't tested it so my XML might be a little off)

Code:
<set name="parts" table="PART" cascade="delete" >
  <key column="CAR_ID"/>
  <composite-element class="foo.bar.PartComponent">
    <property name="sequence" column="SEQUENCE" type="int"/>
    <property name="name" column="NAME" type="string"/>
  </composite-element>
</set>


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.