Implement an option for the Entity Manager to identify an entity based on an
alternative key.
---------------------------------------------------------------------------------------------
Key: OPENJPA-390
URL: https://issues.apache.org/jira/browse/OPENJPA-390
Project: OpenJPA
Issue Type: Wish
Components: jpa
Environment: Any
Reporter: Chadwick M Baatz
Priority: Minor
Right now if one has used JPA to map entities in a service that accepts request
to perform database storage the Entity Manager lacks the inherent capability to
recognize duplicate entities based on alternative keys. Since it often makes
for a better database design to create an entity with both an id for relational
mapping and an alternative key for uniqueness, it would save redundant DAO
logic if the Entity Manager could perform this logic. For example, let's say
that there are two entities A and B where A owns and instance of B , each have
a long id value, and each entity has a alternative key which is defined as a
unique constraint. In a application that receives random requests it is quite
possible that two requests would arrive where either entity being passed into
the entity manager is a duplicate of a managed entity. Since most users of
services neither know of or care about the id value at entity creation, it's
assumed that the id value isn't set.
Under this scenario a merge request to the EntityManager would result in a
persist action, which would fail due to the unique constraint in the database.
It would be nice if there was some way to tell the Entity Manager to look at
search an alternate key if the id value wasn't set and merge changes if a
matching managed entity is found. I have built a small framework for my own
projects that looks at the named queries of an entity for a specific name,
which if found is used to perform the ID look up. This works well for finding
and setting the id value for detached objects, but it gets a tad more
complicated when a entity contains other entities. I can think of another
non-framework solution, which would be to create the table without an id column
and use the alternative key as the primary key, but that has some drawbacks as
well. It seems to me like this is a condition that many users would come
across and would have to either create broiler plate DAOs or their own
frameworks to populate the id values on detached objects with alternative keys.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.