It's not a secret anymore!
The moose likes EJB and other Java EE Technologies and the fly likes unique constraint Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "unique constraint" Watch "unique constraint" New topic

unique constraint

Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
I used a table that has a autonumber thru a control table. Lets call this column the pk.
I have another column userLoginName which I keep unique. Since I am not allowed to change the pk I will have the benifit of being able to change the loginName under some rare circumstances.

When I create a entity bean and the loginName already exiusts it throws a CreateException. And there is no sure way of knowing what is the cause ( unlike wityh a duplicateKeyException ) because loginname here is not a Primary key just a unique field.

The only way to handle this seems to be that I do a findXXX method first to check the unique key existence.

What do u guys thiunk? Got any sugegstions?

Is that a bad design to have a automumber pk and a unique loginName?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33131

That is a great design! You recognize that users should be able to change their login name.

Doing the findXXX is a good way of checking.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You should not rely on the receipt on a FinderException to tell you that an entity does not exist in the DB. For a CMT bean, the container might throw this exception before the DB is accessed.

You could instead not use a finder, try and create the entity in the DB and handle CreateException. The problem is that the client cannot be sure what happened as the container may throw this exception after the transaction was committed. So, on balance, I guess the least worst approach is to use the finder method.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I agree. Here's the link:
subject: unique constraint
It's not a secret anymore!