-->
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: Composite increment id?
PostPosted: Wed Mar 08, 2006 10:31 am 
Newbie

Joined: Fri Jan 20, 2006 1:32 pm
Posts: 11
Hi,

I am going to write simple programm where I want to store Garages and Cars (garage can have many cars but car can be only in one garage).
I would like to have increment id in both classes but Car should have unique id only within the garage and I'd like to have all cars in one table.

class Garage
Set cars;

class Car
Garage garage;

example:

ID NAME

01 garage1
---------------
01 car1
02 car2
03 car4

02 garage2
-------------
01 car5
02 car6
03 car7

Table CARS:
car1
car2
car3
car4
car5
car6
car7

Is there any solution to this problem?

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 10:41 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
yep.

you've got it pretty much summed up right here
Quote:
class Garage
Set cars;

class Car
Garage garage;


bi-directional relationship. just create a car, give it a garage, then save it. Then, either the DB, or your code, should assign it the next number car and voila :)

for car you'll want a auto-incrementing field to create the id: car1, car2, car3, etc to keep that unique.

your garage table will have an id, fk car id (which is car1, car2, etc) and an index column which assigns each car a unique number for that garage when saved

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 10:56 am 
Newbie

Joined: Fri Jan 20, 2006 1:32 pm
Posts: 11
kochcp wrote:
your garage table will have an id, fk car id (which is car1, car2, etc) and an index column which assigns each car a unique number for that garage when saved


could you please send me a mapping document? I don't know how to map the bold part...

Thank you

PS: it's important whether it is uni/bi-directional?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 12:26 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
something simple just like this in the car table

Code:
    <property
        name="indx"
        type="java.lang.Integer"
        column="indx_seq_no"
        not-null="true"
        length="10"
    >
    </property>


something like this for the collection mapping in the garage table
Code:
<list
        name="cars"
        cascade="whatever"
       

    >
        <key>
            <column name="carid"/>

        </key>
        <index column = "indx_seq_no"/>
        <one-to-many
            class="package.class.Cars"
        />
    </list>


read this as well
http://www.hibernate.org/193.html


then, the garage class needs to be in charge of the index column. To add a new car you would:

create a car
fetch the garage
find out what the last indx number was for that garage
assign the next index number to that car
add it to the collection for the garage
save

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 12:56 pm 
Newbie

Joined: Fri Jan 20, 2006 1:32 pm
Posts: 11
So I have to do it "manually" and it seems useless in cluster (I don't have cluster :-))


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.