Hi All

This ticket <https://jira.spring.io/browse/DATAJPA-969> was logged against
spring data jpa, where a subsequent ticket
<https://issues.apache.org/jira/browse/OPENJPA-2668> was logged against
openjpa...

Having a vested interest in spring data plying nice with openjpa I'm taken
it upon myself to look into this...

Now it is true that CriteriaQuery is cached by spring data - which I think
is reasonable.

They suggested they stop caching altogether - which I don't think is a good
idea.

So what happens on the openjpa side is that when the CriteriaQuery is built
into an expression it changes the original CriteriaQuery - it hydrates it
with the actual parameters (parameter values) and so when the query is
called again it does not work.

The following simple test fails as the second time the query is run the
parameters will still by Michael and James.

    public void Members() {
        repository.save(new Member(2L, "Michael"));
        repository.save(new Member(3L, "James"));

        assertThat(repository.findByNameIn(Lists.newArrayList("Michael",
"James"))).hasSize(2);

assertThat(repository.findByNameIn(Lists.newArrayList("Michael"))).hasSize(1);
    }


The important thing to note however is that, if I'm not mistaken this
CriteriaQuery modification *only *occurs in the In expression filter. see
https://github.com/apache/openjpa/blob/master/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
line
1436.

You'll notice the _exps property is cleared and then added to... but this
is the only time, that I can see, where the _exps property is edited in
situ.

Without majorly changing how things work, I've changed it so that it acts
on a copy of the Expresssion and not on the original expression and it
works.

Added the following copy method to Expressions$In

        private Expressions.In<?> copy() {
            In<?> in = new Expressions.In<>(this.e);
            in._exps.addAll(this._exps);
            return in;
        }

Renamed the current toKernelExpression method to differentiate (added a 0)
and then changed the current one to:

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

And now my test passes.

Can I request this code be looked at and potentially included? I'm fairly
convinced there's no reason why it shouldn't work.

Should I create a pull request?

Thanks

-- 
see my blog:
http://analysis102.blogspot.com
http://audiblethoughts.blogspot.com
http://outsideofficehours.blogspot.com

Reply via email to