Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EntityExistsException

 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
 
LakshmiNarayana Kodali
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic