-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EclipseLink does not throw EntityExistsException as expected for PostgreSQL and SQLServer #2170
Comments
Java records are not valid entities. So the results are implementation specific and may vary. Try classes instead of records. |
@AleksNo Same thing happens with classes (I just put in the record for brevity) |
OK, i have read the description of this Exception: "Thrown by the persistence provider when EntityManager.persist(Object) is called and the entity already exists." I guess, it is meant, that if there is already a managed entity with an ID and you try to persist a detached entity with the same ID then the exception will be thrown. But if there is no managed entity (in the EntityManager context) with the ID and you try to persist a detached entity with this ID then Eclipselink creates an INSERT statement and throws it against the database. And it fails because the database server blocks it because you cannot insert a second row with the same ID. So in your case: use EntityManager.merge(Object) instead. |
It is part of Jakarta Persistence 3.2 Improvement into EclipseLink 5.0 is in progress |
@AleksNo When I run this same test against Derby, DB2, or Oracle an EntityExistsException is thrown. For more context here is the full test (from a Jakarta Data level): Receipt r = receipts.insert(new Receipt(1200L, "C0002-12-002", 102.20f));
try {
receipts.insert(new Receipt(1200L, "C0002-10-002", 22.99f));
fail("Inserted an entity with an Id that already exists.");
} catch (EntityExistsException x) {
// expected
} Where I'm working on the Jakarta Data implementation for Open Liberty and without consistent behavior from the underlying entity manager we cannot provide the correct exceptions from our layer. |
When attempting to re-create this issue only using JPA API + Eclipselink I did find that in all cases Eclipselink does only throw Sorry for the misunderstanding I think my original testing showed a stack trace with line numbers that were offset from my current dev environment which resulted in me thinking we were missing a Thanks for taking the time to review this issue. |
Describe the bug
When attempting to persist an entity that already exists in the database we are using the entity manager persist method.
We attempt to catch an
EntityExistsException
, but EclipseLink throws aorg.eclipse.persistence.exceptions.DatabaseException
This seems to be contrary to the Jakarta Persistence javadoc for the persist() method.
Entity:
Entity Manager Usage:
Exception Stack
To Reproduce
Steps/resources to reproduce the behavior:
EclipseLink-4002
IBM Semeru Runtime Open Edition (17.0.11+9)
JPA context like
persistence.xml
settings or related system properties (in case of JPA)Database provider/version
Expected behavior
I would expect the
persist()
method to throw an EntityExistsExceptionAdditional context
It seems what makes PostgreSQL and SQLServer unique is that both JDBC drivers fail to throw a
SQLIntegrityConstraintViolationException
as specified by the JDBC 4.1 specification.Both drivers have open issues for this:
But both of these drivers do not seem to be interested in implementing this exception.
EclipseLink should handle this in a way that results in an EntityExistsException for consistent behavior between JDBC drivers.
The text was updated successfully, but these errors were encountered: