user1969951
user1969951

Reputation: 13

JPA: merge entity with existing id

I am working on a web application with JPA 2.0, and I open and close the EntityManager on each request. The user can create a new entity and also set the id-field (the field has a meaning and I cannot change the table structure). To store the new entity I have to use the "merge"-method of the EntityManager, but when there is already an existing entity with the same id it just gets updated. Do I have to check manually if there is already an entity with the same id, or can this be done in better way? Or is there something else wrong with my approach?

Upvotes: 1

Views: 1994

Answers (1)

SJuan76
SJuan76

Reputation: 24780

If you use persist method, an EntityPersistsException will be thrown for duplicated ids.

That said, from an user point of view, the sooner he know an ID is already used, the better (optimally, when the field is updated check it with ajax).

Upvotes: 1

Related Questions