Skip to main content

[jpa-spec users] [jsr338-experts] EntityManagerFactory.addNamedQuery() stores a template?

  • From: Pinaki Poddar <ppoddar@...>
  • To: jsr338-experts@...
  • Subject: [jpa-spec users] [jsr338-experts] EntityManagerFactory.addNamedQuery() stores a template?
  • Date: Mon, 15 Apr 2013 15:22:15 -0700
  • List-id: <>

Hello Linda,
    From the JavaDoc of the newly introduced EntityManagerFactory.addNamedQuery(String name, Query q), I am assuming that the supplied query q is treated as a template as opposed to an instance as far as binding parameters are concerned.
Let us consider a simple case

 // we create a query
     EntityManager em = ...;
     Query query = em.createQuery("select p from PObject p where = :name");
     // and configure it
    // and bind its parameter to a specific value
     query.setParameter("name", "XYZ");

     // let us declare this query to the persistence unit
     emf.addNamedQuery("x", query);

    // after a while we recall it
        EntityManager em2 = ...;  
        Query query2 = em2.cretaeNamedQuery("x");

   // Then according to the JavaDoc the following assertions should succeed
       assertTrue(query != query2); // the query we declared is a template and hence what is recalled is essentially a new one
       assertEquals(51, query2.getFirstResult());    // but the new one  retains the first result position and other configuration of the template
  // but the binding parameters are not memorized i.e

      try {
           fail("Expected the query execution to fail because the declared parameter [name] is not bound");
      } catch (Exception ex) {

  Will you please confirm if my reading of the spec in this regard is correct or not?

Regards --

Pinaki Poddar                          
Chair, Apache OpenJPA Project 
JPA Expert Group Member
Application & Integration Middleware

[jpa-spec users] [jsr338-experts] EntityManagerFactory.addNamedQuery() stores a template?

Pinaki Poddar 04/15/2013

[jpa-spec users] [jsr338-experts] Re: EntityManagerFactory.addNamedQuery() stores a template?

Linda DeMichiel 04/15/2013
Please Confirm