You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Zhanming Qi <op...@gmail.com> on 2010/05/24 16:46:24 UTC

JPA2.0 Criteria API question select()

Hi,

I download OpenJPA2.0 and try to use Criteria Query, and I download JPA2.0
Spec final release document form jcp.org

At page 263, it wrote this:

CriteriaBuilder cb = ...
CriteriaQuery<String> q = cb.createQuery(String.class);
Root<Customer> customer = q.from(Customer.class);
Join<Customer, Order> order = customer.join(Customer_.orders);
Join<Order, Item> item = order.join(Order_.lineItems);
q.select(customer.get(Customer_.name))
.where(cb.equal(item.get(Item_.product).get(Product_.productType),
"printer"));

and I try it like this
@Test
public void testCriteriaQueryJoins(){
	EntityManager em = entityManagerProvider.get();
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Corporation> q = cb.createQuery(Corporation.class);
	Root<Corporation> corporation = q.from(Corporation.class);
	Join<Corporation, Division> division =
corporation.join(Corporation_.divisions);
	Join<Division, Department> department =
division.join(Division_.departments);
	q.select(corporation.get(Corporation_.name))
	 .where(cb.equal(department.get(Department_.division).get(Division_.name),
DIVISION_NAME));
	TypedQuery<Corporation> tq = em.createQuery(q);
	List<Corporation> result = tq.getResultList();
	assertThat(result, notNullValue());
	for (Corporation c : result){
		System.out.println(c.getName());
	}
	em.clear();
}

But there is something wrong in this example.
At this line q.select(corporation.get(Corporation_.name)), and Eclipse tell
me that: the select method is wrong, and advise me to use multiselect(...)
instead

Is my code wrong? or document wrong? or some other wrong?

Thanks,
Zhanming.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/JPA2-0-Criteria-API-question-select-tp5094119p5094119.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: JPA2.0 Criteria API question select()

Posted by Daryl Stultz <da...@opentempo.com>.
On Thu, Jun 3, 2010 at 7:55 AM, Zhanming Qi <qi...@gmail.com> wrote:

>
> Hi,
>
> Thank you for your help, I have solved it.
>
> Was it Pinaki's suggestion? If not, perhaps you could share the solution
for others that may find this thread.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
http://www.opentempo.com
mailto:daryl.stultz@opentempo.com

Re: JPA2.0 Criteria API question select()

Posted by Zhanming Qi <qi...@gmail.com>.
Hi,

I am very sorry for response so late.
It's my mistake, and it was Pinaki's suggestion.

Thanks
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/JPA2-0-Criteria-API-question-select-tp5094119p5667802.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: JPA2.0 Criteria API question select()

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Jun 3, 2010 at 7:55 AM, Zhanming Qi <qi...@gmail.com> wrote:

>
> Hi,
>
> Thank you for your help, I have solved it.
>
> Was it Pinaki's suggestion? If not, perhaps you could share the solution
for others that may find this thread.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
http://www.opentempo.com
mailto:daryl.stultz@opentempo.com

Re: JPA2.0 Criteria API question select()

Posted by Zhanming Qi <qi...@gmail.com>.
Hi,

Thank you for your help, I have solved it.

Thanks.
Zhanming
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/JPA2-0-Criteria-API-question-select-tp5094119p5134832.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: JPA2.0 Criteria API question select()

Posted by Pinaki Poddar <pp...@apache.org>.
> It should be CriteriaQuery<Corporation> q = cb.createQuery(String.class); 

Compiler will not let you do that either. The API signature of
CriteriaBuilder is

    CriteriaQuery<T> createQuery(Class<T> resultClass);

So the correct code is
    CriteriaQuery<String> q = cb.createQuery(String.class);     


-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/JPA2-0-Criteria-API-question-select-tp5094119p5098447.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: JPA2.0 Criteria API question select()

Posted by Pinaki Poddar <pp...@apache.org>.

The API signature for select() enforces that generic type parameter T you
specified on a CriteriaQuery q is same as the type you are selecting in q.
That is the intended idea/advantage of strongly-typing an API, it reduces
the risk of runtime error. 

Now as per your posting, you had committed that the query will return a
Corporation type, by the following statement:  

   > CriteriaQuery<Corporation> q = cb.createQuery(Corporation.class); 

Few lines below, you stated that the query selects Corporation_.name (which,
I am assuming, is a String) 
  > q.select(corporation.get(Corporation_.name)) 

Hence, the compiler had rightfully complained.

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/JPA2-0-Criteria-API-question-select-tp5094119p5095806.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: JPA2.0 Criteria API question select()

Posted by Jeremy Bauer <te...@gmail.com>.
Zhanming,

Have you generated metamodel classes (<EntityName>_.java) for your
entities?  This section of the OpenJPA manual explains how to generate them.

http://openjpa.apache.org/builds/2.0.0/apache-openjpa-2.0.0/docs/manual/manual.html#d0e11088
-Jeremy
On Mon, May 24, 2010 at 1:37 PM, Kevin Sutter <kw...@gmail.com> wrote:

> Hi,
> Your example looks okay to me.  Your selection is singular.  There should
> be
> no reason for the multiselect.  Maybe Eclipse is complaining about a
> problem
> that doesn't exist.  Which Eclipse are you using?  And, did you
> re-configure
> your Eclipse environment to be using OpenJPA instead of EclipseLink?  Does
> your example compile cleanly outside of Eclipse?  Maybe somebody else can
> spot an error in your example, but it looks like it should work.
>
> Kevin
>
> On Mon, May 24, 2010 at 9:46 AM, Zhanming Qi <op...@gmail.com> wrote:
>
> >
> > Hi,
> >
> > I download OpenJPA2.0 and try to use Criteria Query, and I download
> JPA2.0
> > Spec final release document form jcp.org
> >
> > At page 263, it wrote this:
> >
> > CriteriaBuilder cb = ...
> > CriteriaQuery<String> q = cb.createQuery(String.class);
> > Root<Customer> customer = q.from(Customer.class);
> > Join<Customer, Order> order = customer.join(Customer_.orders);
> > Join<Order, Item> item = order.join(Order_.lineItems);
> > q.select(customer.get(Customer_.name))
> > .where(cb.equal(item.get(Item_.product).get(Product_.productType),
> > "printer"));
> >
> > and I try it like this
> > @Test
> > public void testCriteriaQueryJoins(){
> >        EntityManager em = entityManagerProvider.get();
> >        CriteriaBuilder cb = em.getCriteriaBuilder();
> >        CriteriaQuery<Corporation> q = cb.createQuery(Corporation.class);
> >        Root<Corporation> corporation = q.from(Corporation.class);
> >        Join<Corporation, Division> division =
> > corporation.join(Corporation_.divisions);
> >        Join<Division, Department> department =
> > division.join(Division_.departments);
> >        q.select(corporation.get(Corporation_.name))
> >
> > .where(cb.equal(department.get(Department_.division).get(Division_.name),
> > DIVISION_NAME));
> >        TypedQuery<Corporation> tq = em.createQuery(q);
> >        List<Corporation> result = tq.getResultList();
> >        assertThat(result, notNullValue());
> >        for (Corporation c : result){
> >                System.out.println(c.getName());
> >        }
> >        em.clear();
> > }
> >
> > But there is something wrong in this example.
> > At this line q.select(corporation.get(Corporation_.name)), and Eclipse
> tell
> > me that: the select method is wrong, and advise me to use
> multiselect(...)
> > instead
> >
> > Is my code wrong? or document wrong? or some other wrong?
> >
> > Thanks,
> > Zhanming.
> > --
> > View this message in context:
> >
> http://openjpa.208410.n2.nabble.com/JPA2-0-Criteria-API-question-select-tp5094119p5094119.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
>

Re: JPA2.0 Criteria API question select()

Posted by Kevin Sutter <kw...@gmail.com>.
Hi,
Your example looks okay to me.  Your selection is singular.  There should be
no reason for the multiselect.  Maybe Eclipse is complaining about a problem
that doesn't exist.  Which Eclipse are you using?  And, did you re-configure
your Eclipse environment to be using OpenJPA instead of EclipseLink?  Does
your example compile cleanly outside of Eclipse?  Maybe somebody else can
spot an error in your example, but it looks like it should work.

Kevin

On Mon, May 24, 2010 at 9:46 AM, Zhanming Qi <op...@gmail.com> wrote:

>
> Hi,
>
> I download OpenJPA2.0 and try to use Criteria Query, and I download JPA2.0
> Spec final release document form jcp.org
>
> At page 263, it wrote this:
>
> CriteriaBuilder cb = ...
> CriteriaQuery<String> q = cb.createQuery(String.class);
> Root<Customer> customer = q.from(Customer.class);
> Join<Customer, Order> order = customer.join(Customer_.orders);
> Join<Order, Item> item = order.join(Order_.lineItems);
> q.select(customer.get(Customer_.name))
> .where(cb.equal(item.get(Item_.product).get(Product_.productType),
> "printer"));
>
> and I try it like this
> @Test
> public void testCriteriaQueryJoins(){
>        EntityManager em = entityManagerProvider.get();
>        CriteriaBuilder cb = em.getCriteriaBuilder();
>        CriteriaQuery<Corporation> q = cb.createQuery(Corporation.class);
>        Root<Corporation> corporation = q.from(Corporation.class);
>        Join<Corporation, Division> division =
> corporation.join(Corporation_.divisions);
>        Join<Division, Department> department =
> division.join(Division_.departments);
>        q.select(corporation.get(Corporation_.name))
>
> .where(cb.equal(department.get(Department_.division).get(Division_.name),
> DIVISION_NAME));
>        TypedQuery<Corporation> tq = em.createQuery(q);
>        List<Corporation> result = tq.getResultList();
>        assertThat(result, notNullValue());
>        for (Corporation c : result){
>                System.out.println(c.getName());
>        }
>        em.clear();
> }
>
> But there is something wrong in this example.
> At this line q.select(corporation.get(Corporation_.name)), and Eclipse tell
> me that: the select method is wrong, and advise me to use multiselect(...)
> instead
>
> Is my code wrong? or document wrong? or some other wrong?
>
> Thanks,
> Zhanming.
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/JPA2-0-Criteria-API-question-select-tp5094119p5094119.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>