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

Pawel Veselov edited comment on OPENJPA-2931 at 12/23/24 3:33 AM:
------------------------------------------------------------------

`PathImpl.toValue()` is very selective on when to set metadata to the paths it 
created if:
* it's on a sub-query
* there is no parent

I'm fairly certain that's wrong. I don't know enough about the internals here, 
but it looks like adding 

{code:java}
path.setMetaData(q.getMetamodel().getRepository().getCachedMetaData(_parent.getJavaType()));
{code}

fixes this problem.

But I think the proper fix should be more extensive, and also cover the 
"correlated path" case, and safeguard against cases when 
`_parent.getJavaType()` is not what I think it is.



was (Author: pveselov):
`PathImpl.toValue()` is very selective on when to set metadata to the paths it 
created if:
* it's on a sub-query
* there is no parent

I'm fairly certain that's wrong. I don't know enough about the internals here, 
but it looks like adding 

{code:java}
path.setMetaData(q.getMetamodel().getRepository().getCachedMetaData(_parent.getJavaType()));
{code:java}

fixes this problem.

But I think the proper fix should be more extensive, and also cover the 
"correlated path" case, and safeguard against cases when 
`_parent.getJavaType()` is not what I think it is.


> Comparing objects with composite keys through criteria API doesn't work in 
> joins
> --------------------------------------------------------------------------------
>
>                 Key: OPENJPA-2931
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-2931
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: criteria, query, sql
>    Affects Versions: 3.1.2
>            Reporter: Pawel Veselov
>            Priority: Major
>
> I'm relatively certain this affects downstream as well.
> This is probably incomplete/incorrect fix for OPENJPA-2631.
> I've added this test to TestCompositePrimaryKeys.java:
> {code:java}
>     public void testFindUsingCriteriaBuilderOnSubjectWithJoin() {
>         Subject sub = em.find(Subject.class, new SubjectKey(1, "Type"));
>         CriteriaBuilder builder = em.getCriteriaBuilder();
>         CriteriaQuery<SubTopic> cq = builder.createQuery(SubTopic.class);
>         Root<SubTopic> root = cq.from(SubTopic.class);
>         Join<SubTopic, Topic> join = root.join(SubTopic_.topic);
>         cq.where(builder.equal(join.get(Topic_.subject), sub));
>         root.fetch(SubTopic_.topic);
>         TypedQuery<SubTopic> query = em.createQuery(cq);
>         SubTopic found = query.getSingleResult();
>         Assert.assertNotNull(found);
>         Assert.assertEquals(Integer.valueOf(8), found.getId());
>     }
> {code}
> Having a basic additional entity SubTopic:
> {code:java}
> @Entity
> @Table(name = "SUB_TOPIC")
> public class SubTopic {
>     @Id
>       @Column(name = "ID")
>       private Integer id;
>       @ManyToOne(fetch = FetchType.LAZY)
>       private Topic topic;
>       public SubTopic() {
>       }
> }
> {code}
> The test fails with:
> {noformat}
> Caused by: java.lang.NullPointerException
>       at 
> org.apache.openjpa.jdbc.kernel.exps.EqualExpression.appendTo(EqualExpression.java:89)
>       at 
> org.apache.openjpa.jdbc.kernel.exps.CompareEqualExpression.appendTo(CompareEqualExpression.java:113)
>       at 
> org.apache.openjpa.jdbc.kernel.exps.BindVariableAndExpression.appendTo(BindVariableAndExpression.java:61)
>       at 
> org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.buildWhere(SelectConstructor.java:313)
>       at 
> org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.evaluate(SelectConstructor.java:95)
>       at 
> org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.createWhereSelects(JDBCStoreQuery.java:372)
>       at 
> org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.executeQuery(JDBCStoreQuery.java:203)
>       at 
> org.apache.openjpa.kernel.ExpressionStoreQuery$DataStoreExecutor.executeQuery(ExpressionStoreQuery.java:831)
>       at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1060)
> {noformat}
> OPENJPA-2631 relies on the ClassMetadata being there for the evaluated paths, 
> but in case of joins, it's not set.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to