-->
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: How to add a unique index constraint
PostPosted: Fri Jul 30, 2004 4:38 am 
Newbie

Joined: Fri Jul 30, 2004 4:17 am
Posts: 5
Location: Sydney, Australia
Hello,

I'm new to Hibernate and orms, and just tinkering around with it, and wondering how you would create an exception when inserting a new User into the database, but the username has already been taken?

e.g. my the user table is

create users (
id integer primary key,
username varchar(20) unique key,
password char(16)
);


I've already got hibernate inserting a new user into my database, and using a sequence so I am happy with that, but when I try to insert a new user with an existing username it'll come back with "Could not execute JDBC batch update", due to SEVERE: ERROR: duplicate key violates unique constraint "users_username_key", which makes total sense.

Is there a way to add a unique attribute to user.username mapping so that Hibernate will automatically test that a user already exists when I try to .save()? I've tried unique="true" but it didn't seem to do anything..
Or should I be doing a select for a user with that username and testing if I get a row coming back?

my xml is currently:
<class name="com.magicmonster.intranet.users.businessobjects.UserBO" table="users">
<id name="id" type="long" column="id">
<generator class="sequence">
<param name="sequence">users_s</param>
</generator>
</id>
<property name="username"/>
<property name="password"/>
</class>

thanks,
Jurn


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 4:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You have to do a manual test.


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.