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

ASF subversion and git services commented on OPENJPA-2631:
----------------------------------------------------------

Commit ea5bc9682441ae421c45bd40b905e5beed9062a8 in openjpa's branch 
refs/heads/OPENJPA-2931 from Richard Zowalla
[ https://gitbox.apache.org/repos/asf?p=openjpa.git;h=ea5bc9682 ]

[OPENJPA-2931] Give criteria paths the metadata of the type they navigate to

Comparing an entity with a composite primary key on a path navigated from a
join, such as cb.equal(join.get(Topic_.subject), subject), failed with a
NullPointerException in EqualExpression. Since OPENJPA-2631 the store reads
the metadata of the compared path to decide whether to compare all primary
key columns, but PathImpl only set metadata on root paths: a path starting at
a registered join variable or a correlated join carried none, and a path
navigated from a root kept the metadata of the root instead of the type of
the field.

PathImpl now sets the metadata the way the JPQL parser does: a path starting
at a variable carries the metadata of the variable, and traversing a field
sets the metadata of the field's embedded or declared type, keeping the
previous metadata for basic and multi-valued fields.


> ClassCastException occurs when an equals comparison query is executed on an 
> entity with an @EmbeddedId that contains more than one field.
> -----------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-2631
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-2631
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: criteria, query, sql
>    Affects Versions: 2.1.2, 2.2.3, 2.4.1
>            Reporter: Heath Thomann
>            Assignee: Heath Thomann
>            Priority: Major
>             Fix For: 2.1.2, 2.2.1.1, 2.2.3, 2.4.2
>
>         Attachments: OPENJPA-2631-2.1.x.patch
>
>
> Take the following entity:
> @Entity
> public class Subject implements Serializable {
>       @EmbeddedId
>       private SubjectKey key;
> .......
> Where SubjectKey is as follows:
> @Embeddable
> public class SubjectKey implements Serializable {
>       private Integer subjectNummer;
>       private String subjectTypeCode;
> ......
> As you can see we have a composite primary key.  With this, take this query:
> TypedQuery<Subject> query = em.createQuery("select s from Subject s where s = 
> :subject", Subject.class);
> query.setParameter("subject", s);
> Subject s2 = query.getSingleResult();
> This query will yield the following exception:
> java.lang.ClassCastException: 
> org.apache.openjpa.persistence.embed.compositepk.SubjectKey cannot be cast to 
> [Ljava.lang.Object;]
> at org.apache.openjpa.jdbc.kernel.exps.Param.appendTo(Param.java:149)
> If we execute a corresponding 'em.find' of Subject, this exception doesn't 
> occur.  Furthermore, if you execute the same query for an entity with an 
> @EmbeddedId that only contains one field, all will work as expected.  The 
> issue here is with an equals query where the entity contains an @EmbeddableId 
> with more than two fields.
> While investigating/debugging this issue, I've found further issues when 
> creating the query using CriteriaBuilder; both with an @Embeddable and 
> @IdClass composite PKs.  I will leave it as an exercise for the reader to 
> view the attached test case to see how each issue can occur.  Each test 
> method details what issue it recreated before the fixes to this issue.  I'm 
> also attaching a patch with a proposed fix for the issues.  
> Thanks,
> Heath Thomann



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to