-->
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: help: simple question about unique value
PostPosted: Sun Jun 03, 2007 2:15 am 
Newbie

Joined: Mon Jan 29, 2007 11:17 pm
Posts: 6
Hi,

Simple question. I have a simple User table with three columns and everything works fine (my mapping file is included below):

- 'userid' an int which autoincrements
- 'username' a varchar[50]
- 'password' a varchar[50]

SIMPLE QUESTION: Everything works fine, except currently the username field is not unique. I like having the 'userid' field be a unique identifier for the entire record -- but what's the easiest way to enforce uniqueness on the username as well?

I know I could code it in Java -- do a lookup for any existing user with that name before proceedind with the create -- but I'm guessing Hibernate (or mySql?) provides an easy way to handle this.

Any help is greatly appreciated! Thanks!

boxlight

#
My mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="com.me.model.User" table="user">

<id name="userid" column="userid" >
<generator class="increment"/>
</id>

<property name="username">
<column name="username" />
</property>

<property name="password">
<column name="password"/>
</property>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 03, 2007 5:20 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
You can do it in the mapping file like this

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

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 03, 2007 9:55 pm 
Newbie

Joined: Mon Jan 29, 2007 11:17 pm
Posts: 6
himawan wrote:
You can do it in the mapping file like this

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


Thanks for the help -- modifying the XML it didn't seem to work, though. I can still create duplicates. :-(

Do I need to modify the mysql table as well?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 03, 2007 10:35 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Yes. How do you create your tables? If you use the property "hibernate.hbm2ddl.auto" in hibernate.cfg.xml, hibernate will create the tables with the right unique constraints.

Look at table 3.7 in the link below for information on the hibernate.hbm2ddl.auto property. You can set it to "create" to get hibernate create the tables for you.

http://www.hibernate.org/hib_docs/v3/re ... n-optional

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


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.