File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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: http://aspose.com/file-tools
 
subject: unique constraint
 
Similar Threads
Non numeric id mapping
Reducing the amounts of connections
PRIMARY KEY
Can I change the value of the PK (Hibernate)
Update value of primary key impossible