-->
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: Generated Primary and Composite key in the same map...
PostPosted: Tue Mar 23, 2004 9:35 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
Note: I am using Hibernate version 2.1.1, PostgreSQL version 7.4.1

Hi,
I have a user map for which, as well as generating a primary key, need to make sure that the username and userpassword are unique as well to ensure that no 2 records with the same username, userpassword combination exist.

In the map shown below, I tried to add a composite-key section but am getting errors. How do I resolve this issue???

My map is:

<hibernate-mapping package="test">
<class name="User" table="usertbl">
<id name="ID" column="user_id" type="integer" unsaved-value="0">
<generator class="sequence">
<param name="sequence">seq_user_mytable</param>
</generator>
</id>
...... Other property definitions ...............
<composite-id name="CompID" class="User">
<key-property name="UserName" column="username" type="string"/>
<key-property name="UserPassword" column="userpassword" type="string"/>
</composite-id>

<set name="ArtistChoice" table="choicetbl">
<key column="user_id"/>
<many-to-many column="artist_id" class="test.Artist"/>
</set>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 23, 2004 9:48 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Don't make the username and password part of an id. (There can only be one id per class...) Assign a unique constraint to the properties:

Code:
<property name="UserName" column="username" type="string" unique="true"/>

This ensures that no two usernames exist that are the same.

If you want the combination of two properties to be unique, you can create multi column indices like so:
Code:
<property name="foo" type="string">
  <column name="foo" unique-key="foo-bar-key"/>
</property>
<property name="bar" type="string">
  <column name="bar" unique-key="foo-bar-key"/>
</property>


Top
 Profile  
 
 Post subject: Greg...
PostPosted: Tue Mar 23, 2004 10:20 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
thanks for your quick reply. I have amended my map with the following lines:

<property name="UserName" type="string">
<column name="username" unique-key="UserPassKey"/>
</property>
<property name="UserPassword" type="string">
<column name="userpassword" unique-key="UserPassKey"/>
</property>

This still allows me to enter duplicate records with the same username password comnbination. What am I doing wrong or have forgotten to do??

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 12:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Simply defining a property as unique in the mapping file, in and of itself, is not going to do anything. If you ran the mapping file through the schema generator tool, it would generate unique constraints for you on the corresponding database columns.

If you have are not using Hibernate's schema generation, simply alter those columns to have a unique constraint on them directly in the database.


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.