aspose file tools*
The moose likes Object Relational Mapping and the fly likes EntityExistsException how to know which key i duplicate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "EntityExistsException how to know which key i duplicate?" Watch "EntityExistsException how to know which key i duplicate?" New topic
Author

EntityExistsException how to know which key i duplicate?

Emi Garcia
Greenhorn

Joined: Nov 02, 2010
Posts: 16
i have a desktop app. in which i use JPA hibernate for ORM.

i have an entity that has some unique fields. suppose table T fields = id, a, b ,c where a and b are unique

so when i try to create a new T record if i put some duplicate value for a or b, i get the EntityExistsException, thats ok, but i want to inform to the user which is the key that its being duplicated. so the user can change the input.(validation).

in the output console i see Duplicate entry '12345' for key 'a' but i dont know how to extract that info from the exception. i have printed the message and the stacktrace. but i cant find i there.


am i supposed to check whith other queries if the value already exists before trying to insert?? i dont think so.

i hope you can understand me and help me! thanks
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

If you're using an IDE, work in debug mode and try inspecting the exception object contents. Check if it exists in some property.
Worst case try parsing the String, but that would be bad design because if the String content changes tomorrow you might show incorrect values.


If you are not laughing at yourself, then you just didn't get the joke.
Emi Garcia
Greenhorn

Joined: Nov 02, 2010
Posts: 16
ok i've done this:




i think it is horrible! very hardcoded! if the sql error changes in the future or if the name of column in the databse change...

i dont know why cev.getConstraintName is returning null, that must work and it will make the code a bit more cleaner.

what do you think about?? any other solution
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

I was worried about foreign keys here, until I re read and found that it is unique keys.

Consider there are currently (1,1) , (1,2), (2,1) and (2,2) entries in the database.
If the user is trying to enter (1,2), How do I say which one is unique, both of them are unique and the user can change it to either (5,2) or (1,7)?
Emi Garcia
Greenhorn

Joined: Nov 02, 2010
Posts: 16
i dont understand you?

do you want to know what a unique key is? a unike key is used for a filed like credit card number, so the credit card number its not a primary key and its not a foreign key, but it must be a unique key, because nobody except you is alowed to have the same credit card number as you. if that happens there is wrong information in de DB or in the user input
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Is "A" and "B" separately unique? or (A,B) is a composite unique?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Emi Garcia wrote:
..... in the output console I see Duplicate entry '12345' for key 'a' but I dont know how to extract that info from the exception. i have printed the message and the stacktrace. but I cant find I there.

How did I miss that!! So, it's a separate unique index on column "a". Was it not possible to capture the unique index name and tell which column (attribute) is actually problematic?
Emi Garcia
Greenhorn

Joined: Nov 02, 2010
Posts: 16
Arun Kumarr wrote:
Emi Garcia wrote:
..... in the output console I see Duplicate entry '12345' for key 'a' but I dont know how to extract that info from the exception. i have printed the message and the stacktrace. but I cant find I there.

How did I miss that!! So, it's a separate unique index on column "a". Was it not possible to capture the unique index name and tell which column (attribute) is actually problematic?


yeahh it was that! so what do you think of my solution? i dont think its the best
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Definitely not the best.
There is no elegant solution seem to be present for this problem, currently AFAIK.

We can try something like this:

Case 1: While creating the unique index supply a name for the index and once you get the error message, find out if the message contains your "unique index name".
Of course, you need to check all the index names which are present in the table one by one(should not take much time). If we are doing this, then this must go as a
separate class and not in every catch block.

Case 2: We can use a function to do the insert and inside the function handle the mysql error and see if we can return a custom error code/message from the database.
If we do this, we can probably handle it in the code once we identify the custom error code/message.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EntityExistsException how to know which key i duplicate?