4 Replies Latest reply on Jul 22, 2009 9:34 AM by jayadevs

    Named query

    clxy

      I hit Named query not known error.


      The exceptiion is:
      javax.el.ELException: javax.persistence.PersistenceException: org.hibernate.MappingException: Named query not known: user.login


      My entitiy is


      @Entity
      @NamedQueries({
          @NamedQuery(
                  name = "user.login",
                  query = "SELECT u FROM User u WHERE u.email = :email and  u.password = :password")
      })
      public class User implements Serializable {
      ...
      }
      



      My service code is


      public class UserServiceJPAImpl implements UserService {
      
          private EntityManager em;
      
          public void setEntityManager(EntityManager em) {
              this.em = em;
          }
      
          public User login(String id, String password) {
      
              Query query = em.createNamedQuery("user.login");
              ...
          }
      }
      



      at components.xml, cofigured like :


              <component name="userService" auto-create="true"
                      class="foo.bar.UserServiceJPAImpl">
                      <property name="entityManager">#{em}</property>
              </component>
      




      My test code is:


      @Name("authenticator")
      public class AuthAction {
          @In
          Identity identity;
      
          @In
          UserService userService;
      
          @In(create = true)
          TestService testService;
      
          @Out(required = false, scope = SESSION)
          User user;
      
          public boolean authenticate() {
      
              testService.doSomething();// configured by annotation.can work!
      
              String userName = identity.getCredentials().getUsername();
              String password = identity.getCredentials().getPassword();
      
              User user = userService.login(userName, password);//can not work!
      
              return user != null;
          }
      }
      



      The exception occurred when


      em.createNamedQuery("user.login")




      and, i take another try


      Test entity


      @Entity
      @NamedQueries( { @NamedQuery(name = "test.sql", query = "select u from TestEntity u ") })
      public class TestEntity {
      ...
      }



      Test service code.


      @Name("testService")
      public class TestService {
      
          @In
          private EntityManager em;
      
          public void doSomething() {
      
              Query q = em.createNamedQuery("test.sql");
              q.getResultList();
          }
      }
      



      The different is EntityManager configured by annotation not components.xml as you can see.
      And test code can work well.


      so, how to make EntityManager configured by components.xml work?
      thanks

        • 1. Re: Named query
          clxy

          I am sorry, I forgot another difference.
          Entity User and service UserServiceJPAImpl belong to another project, and packaged as a jar file put in WEB-INF/lib.


          btw, my environment is


          Seam:2.1.1.GA
          JDK:1.6.0-13
          Tomcat:6.0.18


          • 2. Re: Named query
            clxy

            I figured this out.
            The entity in a jar file can not be loaded automatic.


            I add the orm.xml at META-INF,and configured the entity again.
            It can work now.


            I am a newbie of JPA.
            Please let me know if i am wrong.
            Thanks.

            • 3. Re: Named query
              clxy

              I found the same question at here.
              The right way is set jar-file in persistence.xml.


              =.=

              • 4. Re: Named query
                jayadevs
                Hi,



                Really the information posted above is uiseful.

                One query, I am trying to cache named queries in JPA, to cache the Named Query results for a particular time period of more than 20 minutes.

                I have written the ehcache.xml and defined the same reference in the persistence.xml, In the Entity option object, for the Named Query I have defined the Hints --> QueryHint = cacheable to true and the region.

                But the query is being cached for only 2 minutes(120seconds). I would like to cache the query about 20 minutes.


                Please find the below are the code in persistence.xml, ehcache.xml, POJO(Example: Option.java) and DAO(OptionDAO.java).



                POJO(Option.java):

                @NamedQuery(name = "findOptionsByLineOfBusinessAndStatus", query = "from Option where uniqueOptionKey.lineOfBusinessId= :lineOfBusiness AND optionStatus= :stateCode", hints = {       @QueryHint(name = "org.hibernate.cacheable", value = "true"),            @QueryHint(name = "org.hibernate.cacheRegion", value = "query.AdministrativeAreasPerCountry") })



                persistence.xml:

                <property name="hibernate.cache.use_second_level_cache" value="true"/>
                <property name="hibernate.cache.use_query_cache" value="true"/>
                <property name="hibernate.generate_statistics" value="true"/>
                <property name="hibernate.cache.provider_configuration_file_resource_path" value="ehcache.xml"/>  <!-- ehcache.xml is along with the persistence.xml in META-INF-->


                ehcache.xml

                <ehcache>
                    <diskStore path="c:\\web\\"/>
                    <defaultCache
                        maxElementsInMemory="10000"
                        eternal="false"
                        timeToIdleSeconds="360000"
                        timeToLiveSeconds="360000"
                        overflowToDisk="true"
                        diskPersistent="false"
                        diskExpiryThreadIntervalSeconds="360000"
                        memoryStoreEvictionPolicy="LRU"
                        />

                     <cache name="org.hibernate.cache.UpdateTimestampsCache"
                            maxElementsInMemory="1000"
                            eternal="false"
                            timeToIdleSeconds="360000"
                            timeToLiveSeconds="360000"
                            overflowToDisk="true"
                            diskPersistent="false"
                            diskExpiryThreadIntervalSeconds="360000"
                            memoryStoreEvictionPolicy="LRU"
                            />

                       <cache name="org.hibernate.cache.StandardQueryCache"
                            maxElementsInMemory="1000"
                            eternal="false"
                            timeToIdleSeconds="360000"
                            timeToLiveSeconds="360000"
                            overflowToDisk="true"           
                            diskPersistent="false"
                            diskExpiryThreadIntervalSeconds="360000"
                            memoryStoreEvictionPolicy="LRU"
                            />  

                         <cache name="query.AdministrativeAreasPerCountry"
                            maxElementsInMemory="500"
                            eternal="false"
                            timeToIdleSeconds="180000"
                            timeToLiveSeconds="864000"                  
                            overflowToDisk="true"/> 
                </ehcache>



                OptionDAO:

                Query query = getEntityManager().createNamedQuery("from Option where uniqueOptionKey.lineOfBusinessId= :lineOfBusiness AND optionStatus= :stateCode)";
                query.setParameter(LINE_OF_BUSINESS, lineOfBusiness);
                query.setParameter(STATE_CODE, stateCode);

                options = (List<Option>) query.getResultList();


                Could you please provide some information on the same. where exactly is missing the configuration properties being set are overridden to the default. The query being cached for 2 minutes is the default time to cache.


                Thanks and Regards,
                Jayadev S