• Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.4.2
    • Fix Version/s: None
    • Component/s: criteria, jpa, query, sql
    • Labels:
    • Environment:
      Windows 10 Pro (v 1709)
      Tomee 7.0.4 (Tomcat 8.5.20)
      OpenJPA 2.4.2 (JPA 2.1)
      MySQL 5.7


      Summary: Programatically, whether using a criteria query, JPQL or native query, the child class data contained in the joined table cannot be accessed. However, a native query executed in MySQL Workbench does return fields in both the parent and child tables.

      This problem was posted a month ago on both the OpenJPA Nabble forum:


      and Stack Overflow:


      No solution has been forthcoming.


      The Ledger entity holds the details of a transaction including, optionally, the associated Person entity (denominated counterparty) . The Person entity holds a List of Ledger entries (denominated transactions) in which that person engaged. The entities are linked in a bi-directional relationship using a join table (trx_person) containing columns for the pertinent and The database is MySQL 5.7 and the ORM is OpenJPA 2.4.2 (JPA 2.1) running under Tomee 7.0.4 (Tomcat 8.5.20) on Windows 10 Pro (v 1709). Saving a transaction writes data to the ledger and person tables and records the respective IDs in the trx_person join table. The problem arises when attempting to execute a query (whether criteria, JPQL or native) to retrieve data from the parent and child tables, resulting in either an error or the failure to retrieve data from the child table.

      Ledger Entity (parent):

      @ManyToOne(fetch = FetchType.EAGER,
      cascade = {CascadeType.PERSIST, CascadeType.MERGE{color:#000000}})
      name = "trx_person",
      joinColumns =
      @JoinColumn(name = "trx_ID", referencedColumnName = "id"),
      inverseJoinColumns =
      @JoinColumn(name = "person_ID", referencedColumnName = "id")
      protected Person counterparty;

      Person Entity

      @OneToMany(fetch = FetchType.EAGER, mappedBy = "counterparty")
      private List<Ledger> transactions = new ArrayList<>();

      Test Class

      public class LedgerEx extends BaseDAOImpl<Ledger, Integer>
      implements Serializable {
      private static final long serialVersionUID = 1L;

      private List<Ledger> execute(Query theQuery) {
      List<Ledger> results = null;
      String qStr = theQuery.unwrap(org.apache.openjpa.persistence.QueryImpl.class).getQueryString();"UNWRAPPED QUERY: " + qStr);
      try {
      results = theQuery.getResultList();
      } catch (Exception e) {
      return results;

      public List<Ledger> retrieveCriteria(String lastName) {
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Ledger> q = cb.createQuery(Ledger.class);
      Root<Ledger> ledger = q.from(Ledger.class);
      Join<Ledger, Person> person = ledger.join(Ledger_.counterparty, JoinType.LEFT);
      List<Predicate> predicates = new ArrayList<Predicate>();
      Path<String> lnPath = person.get(Person_.lastName);
      predicates.add(, lastName + "%"));;
      q.where(predicates.toArray(new Predicate[predicates.size()]));
      Query theQuery = em.createQuery(q);"CRITERIA QUERY");
      return execute(theQuery);

      public List<Ledger> retrieveJPQL(String lastName) {
      String ln = lastName.trim() + "%";
      StringBuilder selectBldr = new StringBuilder("SELECT l FROM Ledger l ");
      selectBldr.append("LEFT JOIN l.counterparty cp WHERE ")
      .append("cp.lastName LIKE :lastName");
      Query theQuery = null;
      String qStr = selectBldr.toString();
      theQuery = em.createQuery(qStr, Ledger.class);
      theQuery.setParameter("lastName", ln);"JPQL QUERY");
      return execute(theQuery);

      public List<Ledger> retrieveNative(String lastName) {
      StringBuilder selectBldr = new StringBuilder("SELECT * FROM Ledger l ");
      selectBldr.append("LEFT OUTER JOIN trx_person tp ON = tp.trx_ID ")
      .append("LEFT OUTER JOIN person p ON = tp.person_ID");
      String ln = lastName.trim() + "%";
      selectBldr.append(" WHERE p.last_name LIKE '" + ln + "'");
      Query theQuery = null;
      String qStr = selectBldr.toString();
      theQuery = em.createNativeQuery(qStr, Ledger.class);"NATIVE QUERY");
      return execute(theQuery);

      Criteria Query:

      UNWRAPPED QUERY: SELECT DISTINCT l FROM Ledger l LEFT JOIN l.counterparty ? WHERE l.counterparty.lastName LIKE 'bel%'


      UNWRAPPED QUERY: SELECT l FROM Ledger l LEFT JOIN l.counterparty cp WHERE cp.lastName LIKE :lastName

      In either case, an NPE is thrown by the join method of the org.apache.openjpa.jdbc.sql SelectImpl class, which is caused by a null argument fk (ForeignKey):

      <openjpa-2.4.2-r422266:1777108 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Failed to execute query "SELECT l FROM Ledger l LEFT JOIN l.counterparty cp WHERE cp.lastName LIKE :lastName". Check the query syntax for correctness. See nested exception for details.
      at org.apache.openjpa.kernel.QueryImpl.execute(
      at org.apache.openjpa.kernel.QueryImpl.execute(
      at org.apache.openjpa.kernel.DelegatingQuery.execute(
      at org.apache.openjpa.persistence.QueryImpl.execute(
      at org.apache.openjpa.persistence.QueryImpl.getResultList(
      at rfpeake.scc.persistence.LedgerEx.retrieveJPQL(
      at rfpeake.scc.view.JournalView.test(
      at rfpeake.scc.view.JournalView$$OwbInterceptProxy0.test(rfpeake/scc/view/
      at rfpeake.scc.view.JournalView$$OwbNormalScopeProxy0.test(rfpeake/scc/view/
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(
      at java.lang.reflect.Method.invoke(
      at org.apache.el.parser.AstValue.invoke(
      at org.apache.el.MethodExpressionImpl.invoke(
      at org.apache.webbeans.el22.WrappedMethodExpression.invoke(
      at org.apache.webbeans.el22.WrappedMethodExpression.invoke(
      at org.apache.myfaces.view.facelets.el.ContextAwareTagMethodExpression.invoke(
      at org.apache.myfaces.application.ActionListenerImpl.processAction(
      at javax.faces.component.UICommand.broadcast(
      at javax.faces.component.UIViewRoot._broadcastAll(
      at javax.faces.component.UIViewRoot.broadcastEvents(
      at javax.faces.component.UIViewRoot._process(
      at javax.faces.component.UIViewRoot.processApplication(
      at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(
      at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(
      at org.apache.myfaces.lifecycle.LifecycleImpl.execute(
      at javax.faces.webapp.FacesServlet.service(
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(
      at org.apache.openejb.server.httpd.EEFilter.doFilter(
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(
      at org.apache.catalina.core.StandardWrapperValve.invoke(
      at org.apache.catalina.core.StandardContextValve.invoke(
      at org.apache.tomee.catalina.OpenEJBValve.invoke(
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
      at org.apache.catalina.core.StandardHostValve.invoke(
      at org.apache.catalina.valves.ErrorReportValve.invoke(
      at org.apache.tomee.catalina.OpenEJBSecurityListener$RequestCapturer.invoke(
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(
      at org.apache.catalina.core.StandardEngineValve.invoke(
      at org.apache.catalina.connector.CoyoteAdapter.service(
      at org.apache.coyote.http11.Http11Processor.service(
      at org.apache.coyote.AbstractProcessorLight.process(
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(
      at java.util.concurrent.ThreadPoolExecutor.runWorker(
      at java.util.concurrent.ThreadPoolExecutor$
      at org.apache.tomcat.util.threads.TaskThread$
      Caused by: java.lang.NullPointerException
      at org.apache.openjpa.jdbc.sql.SelectImpl$SelectJoins.join(
      at org.apache.openjpa.jdbc.sql.SelectImpl$SelectJoins.outerJoinRelation(
      at org.apache.openjpa.jdbc.meta.strats.RelationFieldStrategy.joinRelation(
      at org.apache.openjpa.jdbc.meta.FieldMapping.joinRelation(
      at org.apache.openjpa.jdbc.kernel.exps.PCPath.joinRelation(
      at org.apache.openjpa.jdbc.kernel.exps.PCPath.initialize(
      at org.apache.openjpa.jdbc.kernel.exps.Variable.initialize(
      at org.apache.openjpa.jdbc.kernel.exps.BindVariableExpression.initialize(
      at org.apache.openjpa.jdbc.kernel.exps.BindVariableAndExpression.initialize(
      at org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.initialize(
      at org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.newSelect(
      at org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.evaluate(
      at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.createWhereSelects(
      at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.executeQuery(
      at org.apache.openjpa.kernel.ExpressionStoreQuery$DataStoreExecutor.executeQuery(
      at org.apache.openjpa.kernel.QueryImpl.execute(
      at org.apache.openjpa.kernel.QueryImpl.execute(
      ... 54 more

      Here is the subject method:

      package org.apache.openjpa.jdbc.sql;
      public class SelectImpl

      private Joins join(String name, ForeignKey fk, ClassMapping target,
      int subs, boolean inverse, boolean toMany, boolean outer) {
      . . . 
      if (createJoin) {
      boolean createIndex = true;
      table1 = (inverse) ? fk.getPrimaryKeyTable() : fk.getTable(); // NPE
      if (correlatedVar != null)
      createIndex = false; // not to create here
      alias1 = _sel.getTableIndex(table1, this, createIndex);

      Native Query:

      UNWRAPPED QUERY: SELECT * FROM Ledger l LEFT OUTER JOIN trx_person tp ON = tp.trx_ID LEFT OUTER JOIN person p ON = tp.person_ID WHERE p.last_name LIKE 'bel%' 

      26473 sccPU TRACE [http-nio-8080-exec-1] openjpa.jdbc.SQL - <t 2001566939, conn 1897759772> executing prepstmnt 92722447
      SELECT *
      FROM Ledger l LEFT OUTER JOIN trx_person tp ON = tp.trx_ID LEFT
      OUTER JOIN person p ON = tp.person_ID
      WHERE p.last_name LIKE 'bel%'

      26478 sccPU TRACE [http-nio-8080-exec-1] openjpa.jdbc.SQL - <t 2001566939, conn 1897759772> [5 ms] spent
      May 17, 2018 1:51:47 PM rfpeake.scc.view.JournalView processTrxList
      INFO: resultCount = 5
      May 17, 2018 1:51:47 PM rfpeake.scc.view.JournalView display
      WARNING: ledger.counterparty is null

      While no error is thrown, the Person entity (ledger.counterparty) is not returned. It seems that the join table annotations are detected upon writing data, but the join table information is not utilized when querying data. Executing the query in MySQL Workbench returns data from both tables and will return all columns using: SELECT * FROM ledger l.

      See screenshot attached.





        1. SampleQuery.PNG
          23 kB
          Robert F. Peake
        2. image-2018-05-19-12-34-48-475.png
          18 kB
          Robert F. Peake



            • Assignee:
              OldSplice Robert F. Peake
            • Votes:
              0 Vote for this issue
              1 Start watching this issue


              • Created: