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
|