File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EntityExistsException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EntityExistsException" Watch "EntityExistsException" New topic


krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185

I have a small doubt on this:
How come the persistent provider knows that the entity which would be persisted already exist in the database?
Lets say I am persisting an entity. Would the persistent provider checks the entire table if there is a row with the same values that the entity which is going to be persisted exists in that particular table?
Please excuse if this is not the related forum to post this question

Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
LakshmiNarayana Kodali

Joined: Oct 30, 2008
Posts: 16
EntityExistsException occurs when trying to persist a new entity instance into database but that entity already exists in database.

The persistence provider will write an Insert statement for the persist operation.

And if the DB Manager executes that statement, the insert operation fails beacuse of PrimaryKey Constraint.

So that's why the EntityExistsException will occur.

LakshmiNarayana Kodali
SCJP 5(98%), SCWCD(100%), SCBCD in progress
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Why would the DB gives primary key constraint exception?
lets say I have an Employee bean with properties name and salary.
I can insert an employee with name as 'XXX' salary as 1000.
I can insert an employee again with name as 'XXX' and salary as 1000.
The DB won't raise any primary key constraint over here. The DB may give unique exception if I make the name as unique.
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Well. the Relational Databases Laws state that every row in the table must be uniquely identified, i.e. each table should have a primary key..
That is the reason you have to specify a field with the @id notation while mapping it to a database.
that field with @id notation becomes the primary key.
The example in your case does not have any primary key, and i am not sure how could you use the Java Persistence API in such a case.


Rahul Babbar
I agree. Here's the link:
subject: EntityExistsException
It's not a secret anymore!