-->
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: Making composite unique constraint
PostPosted: Fri Nov 05, 2004 12:57 pm 
Newbie

Joined: Fri Nov 05, 2004 12:12 pm
Posts: 2
Hello,

At first I would like to say that although I am new to Hibernate, I already have an impression that Hibernate rules.

Now, after reading through all the documentation and FAQ, I have an unsolved domain problem. Let's say, I have a database of dudes and gangs of dudes. Each dude has a nickname that is unique in his gang. I know how to make a single attribute unique, but I am puzzled with what would be the best way to tell about this composite uniqueness to Hibernate, without losing the information that one component of the unique key represents one-to-many relationship.

I have:

Hibernate version: 2.1

Mapping documents:

<class name="Gang" table="gangs">
<id name="id" column="id" type="java.lang.Long">
<generator class="sequence"/>
</id>
...
</class>

<class name="Dude" table="dudes">
<id name="id" column="id" type="java.lang.Long">
<generator class="sequence"/>
</id>
<many-to-one name="gang" class="Gang" column="gang_id" not-null="true"/>
<property name="nickName" column="nickname" type="string" not-null="true"/>
</class>

Name and version of the database you are using: PostgreSQL 7.3.4

Now I would like to tell to Hibernate that the combination of "gang" and "nickName" is unique, so, that SQL generated by net.sf.hibernate.tool.hbm2ddl.SchemaExport would be something like this:

...
create table dudes (
id int8 not null,
gang_id int8 not null,
nickname varchar(255) not null,
primary key (id),
unique (gang_id, nickname),
);
...
anter table dudes add constrait FK_ARGLE_BARGLE_CLIPF_CLOPF foreign key
(gang_id) references gangs;
...

I'd appreciate any hints that would lead me to a neat solution to this problem.

Thank You in advance,

--
Asko Seeba


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 2:58 pm 
Newbie

Joined: Fri Nov 05, 2004 12:12 pm
Posts: 2
I figured it out from Google. The solution is to use unique-key attribute of column tag.

Sorry for disturbing with trivialities.

--
Asko Seeba


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.