• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unique constraint

 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swamy,
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.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic