Uploaded image for project: 'OpenJPA'
  1. OpenJPA
  2. OPENJPA-2631

ClassCastException occurs when an equals comparison query is executed on an entity with an @EmbeddedId that contains more than one field.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.1.2, 2.2.3, 2.4.1
    • 2.1.2, 2.2.1.1, 2.2.3, 2.4.2
    • criteria, query, sql
    • None

    Description

      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

      Attachments

        1. OPENJPA-2631-2.1.x.patch
          51 kB
          Heath Thomann

        Issue Links

          Activity

            People

              jpaheath Heath Thomann
              jpaheath Heath Thomann
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: