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

Michael Wiles commented on OPENJPA-2668:
----------------------------------------

So having had another go at this I've made some progress...

The issue stems fundamentally from how the CriteriaQuery is cached by Spring 
Data and then it is changed when openjpa hydrates an "In" where clause.

If you look at the code in 
[Expressions.java|https://github.com/apache/openjpa/blob/9f26ed29bf31b5c8ab68c5257d42e8c88765cf9b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java#L1436]
 you'll notice how the Expressions$In class modifies the _exps property. This 
property btw comes out of the CriteriaQuery so changes this class changes the 
CriteriaQuery.

Now as far as I can make out, this is the only expression that is changing the 
CriteriaQuery. 

So I suspect if we simply clone the Expressions.In class and then operate on 
that one, it should work. 

So something like adding:
{code:java}
    private Expressions.In<?> copy() {
            In<?> in = new Expressions.In<>(this.e);
            in._exps.addAll(this._exps);
            return in;
        }
{code}

And then after renaming the current toKernelExpression we make this method:


{code:java}
 @Override
        org.apache.openjpa.kernel.exps.Expression 
toKernelExpression(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
            return copy().toKernelExpression0(factory, q);
        }
{code}



> CriteriaQuery instances should not change their state based on Query 
> instances created from them
> ------------------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-2668
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-2668
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 2.4.1
>            Reporter: Oliver Drotbohm
>            Assignee: Mark Struberg
>            Priority: Major
>
> JPA has a two-step, programatic query creation process: first, you create a 
> {{CriteriaQuery}} to build up the general query structure, add constraints on 
> it etc. Then you take that instance and create a {{(Typed)Query}} instance 
> from it using the currently available {{EntityManager}} and bind parameters 
> to it.
> According to [reports we got for Spring Data 
> JPA|https://jira.spring.io/browse/DATAJPA-969], the latter step changes the 
> state of the {{CriteriaQuery}} instance, so that subsequent creations of 
> {{Query}} instances from it (and subsequent parameter bindings in turn) don't 
> actually get applied correctly. 
> Even if the subsequent creation and parameter binding got applied, that 
> change of state in {{CriteriaQuery}} instances is problematic in concurrent 
> access scenarios as the bindings might override each other partially.
> Generally speaking I'd recommend to keep the {{CriteriaQuery}} instances 
> immutable with regards to the creation of {{Query}} instances from them and 
> the subsequent handling of those {{Query}} instances.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to