• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

EntityExistsException  RSS feed

 
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
 
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.
 
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
Boost this thread!