[ 
https://issues.apache.org/jira/browse/OPENJPA-2680?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15682959#comment-15682959
 ] 

Wouter van der Kamp commented on OPENJPA-2680:
----------------------------------------------

Hi, thanks for your quick reply. The spec is indeed not very clear. 

Merging the entity state is not necessary when the object is new, because there 
is no existing record in the database to merge the object with. Note that the 
primary key field in my example equals NULL and OpenJPA  translates this to 0 
in the select query.

When working with a lot of entities the number of "select ... where id = 0 " 
queries which hit the database becomes very large and therefore it's becoming a 
performance issue.

> entityManager.merge() sends unnecessary select statements to the database 
> --------------------------------------------------------------------------
>
>                 Key: OPENJPA-2680
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-2680
>             Project: OpenJPA
>          Issue Type: Bug
>    Affects Versions: 2.4.1
>            Reporter: Wouter van der Kamp
>         Attachments: jpa-test.zip
>
>
> See attachment jpa-test.zip.
> Test.test1() does a merge() on a new object with a @GeneratedValue id. Here 
> OpenJPA sends this query to the database:
> {code:sql}
> SELECT t0.name FROM Country t0 WHERE t0.id = ? [params=(long) 0]
> {code}
> Presumably to check whether the entity is already present in the database, 
> but this is unnecessary because it's a new object without an id (id == null). 
> Workaround for this bug is to use persist() in stead of merge(). But this is 
> not always possible, for example when using an object which is not known by 
> the EntityManager. This is Test.test2(), where OpenJPA sends the query:
> {code:sql}
> SELECT t1.id, t1.name, t0.name FROM Citizen t0 LEFT OUTER JOIN Country t1 ON 
> t0.countryId = t1.id WHERE t0.id = ? [params=(long) 0]
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to