All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: About Question enthuware.oce-jpad.v6.2.479 :
PostPosted: Tue Feb 14, 2017 6:33 am 
Offline

Joined: Fri Oct 09, 2015 3:52 am
Posts: 9
I am confused about option 3 being invalid. You said this was wrong because "order_customerJoin.get("creditCards") will return a Collection". I didn't know this was not allowed.

I tried to recreate this configuration using EclipseLink (with JPA 2.1), and it seems to work fine. I also translated it to JPQL (with some minor change because of the order_customerJoin.creditCards.cardType), and it also works fine as a JPQL query.

What have I missed?

Here is my code:
Code:
   public void testVisaCustomerOrderQuery() {
      CriteriaBuilder cb = em.getCriteriaBuilder();
      String cardType = "VISA";

      {
         CriteriaQuery<CustomerOrder> cq = cb.createQuery(CustomerOrder.class);
         Root custOrderRoot = cq.from(CustomerOrder.class);
         Join<CustomerOrder, Customer> order_customerJoin = custOrderRoot.join("customer");
         Predicate p = cb.equal(order_customerJoin.get("creditCards").get("cardType"), cardType);
         cq.select(custOrderRoot).where(p);

         TypedQuery<CustomerOrder> tq = em.createQuery(cq);
         List<CustomerOrder> retVal = tq.getResultList();
         for (CustomerOrder co : retVal) {
            System.out.println(co);
         }
      }
      
      {
         TypedQuery<CustomerOrder> q = em.createQuery(""
               + "SELECT custOrderRoot "
               + "FROM CustomerOrder custOrderRoot "
               + "JOIN custOrderRoot.customer order_customerJoin "
               + "JOIN order_customerJoin.creditCards creditCards "
               + "WHERE creditCards.cardType = '"+cardType+"'",
               CustomerOrder.class);
         List<CustomerOrder> retVal = q.getResultList();
         for (CustomerOrder co : retVal) {
            System.out.println(co);
         }
      }
   }


In the log output, both queries are translated to the same SQL (as do the other correct answers):
Code:
[EL Fine]: sql: 2017-02-14 12:17:51.836--ServerSession(2052256418)--Connection(1822651119)--Thread(Thread[main,5,main])--SELECT t1.ID, t1.DISCOUNT, t1.CUSTOMER_ID FROM testdb.CUSTOMER t0, testdb.CREDITCARD t2, testdb.CUSTOMERORDER t1 WHERE ((t2.CARDTYPE = ?) AND ((t0.ID = t1.CUSTOMER_ID) AND (t2.CUSTOMER_ID = t0.ID)))
   bind => [VISA]
[EL Fine]: sql: 2017-02-14 12:17:51.848--ServerSession(2052256418)--Connection(1822651119)--Thread(Thread[main,5,main])--SELECT ID FROM testdb.CUSTOMER WHERE (ID = ?)
   bind => [1]
(id=1,discount=25.0)
(id=4,discount=50.0)
[EL Fine]: sql: 2017-02-14 12:17:52.036--ServerSession(2052256418)--Connection(1822651119)--Thread(Thread[main,5,main])--SELECT t1.ID, t1.DISCOUNT, t1.CUSTOMER_ID FROM testdb.CUSTOMER t0, testdb.CREDITCARD t2, testdb.CUSTOMERORDER t1 WHERE ((t2.CARDTYPE = ?) AND ((t0.ID = t1.CUSTOMER_ID) AND (t2.CUSTOMER_ID = t0.ID)))
   bind => [VISA]
(id=1,discount=25.0)
(id=4,discount=50.0)


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.479 :
PostPosted: Tue Feb 14, 2017 11:55 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
As per section 4.4.4 (pg 140) of JPA 2.0 specification, "It is syntactically illegal to compose a path expression from a path expression that evaluates to a collection. For example, if o designates Order, the path expression o.lineItems.product is illegal since navigation to lineItems results in a collection."

I will dig more into this to see how it applies to CriteriaBuilder queries. But sometimes JPA implementations do not strictly follow the specification so if some query works on a particular implementation that doesn't necessarily mean that is legally valid.

HTH,
Paul.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.479 :
PostPosted: Sun Feb 19, 2017 4:01 pm 
Offline

Joined: Fri Oct 09, 2015 3:52 am
Posts: 9
Thanks Paul for the spec reference and the clarification.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum