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
|