EntityManager.persist should through EntityExistsException instead of PersistenceException

Description

The persist method of the EntityManager interface is not implemented according to the specification. If handed an already existing entity, it throws an instance of PersistenceException instead of EntityExistsException. The fact that the exception's cause is an instance of DuplicateEntityException does not help clients to determine the reason for the failure in a vendor-independent fashion. Changing this behavior will most likely not break existing clients, because they often catch instances of PersistenceException, which will also catch EntityExistsException instances. However, it will enable clients to detect the reason for the failure and thereby allow for better error reporting to the user. In fact, in case no transaction is running while this call fails, clients can also recover from the failure and queued actions can be committed afterwards.

Environment

Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200)
Java version: 1.5.0_22
Java home: C:\Program Files\Java\jdk1.5.0_22\jre
Default locale: de_DE, platform encoding: Cp1252
OS name: "windows 7" version: "6.1" arch: "amd64" Family: "windows"

Activity

Show:
Michael Rudolf
September 29, 2011, 9:28 PM

Replace DuplicateEntityException in the above description with NonUniqueObjectException, sorry for the confusion.

Michael Rudolf
September 29, 2011, 9:37 PM
Strong Liu
September 30, 2011, 11:25 AM

patch applied, thanks for the pull request.

Assignee

Strong Liu

Reporter

Michael Rudolf

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure