Announcement Announcement Module
Collapse
No announcement yet.
Errors in named queries Spring 3.x + JPA + Hibernate 3.3.2.GA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Errors in named queries Spring 3.x + JPA + Hibernate 3.3.2.GA

    Hi,

    I've defined an entity with named query as follows
    Code:
    @Entity
    @Table(name="CUSTOMER")
    @NamedQuery(name="getAllCustomer",query="SELECT cust FROM CUSTOMER cust WHERE cust.STATUS=? AND cust.BILLING_CODE=?")
    public class Customer implements Serializable {
    
    	// entity properties and JPA annotated getters/setters
    }
    when I attempt to start the application, Context initialization fails with following error:
    Code:
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: travelRequestPersistenUnit] Unable to build EntityManagerFactory
    	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
    	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132)
    	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    	... 40 more
    Caused by: org.hibernate.HibernateException: Errors in named queries: getAllCustomer
    	at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:365)
    	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
    	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
    	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
    In Hibernate code, I found this:
    Code:
      480   			// this will throw an error if there's something wrong.
      481   			try {
      482   				log.debug("Checking named query: " + queryName);
      483   				//TODO: BUG! this currently fails for named queries for non-POJO entities
      484   				queryPlanCache.getHQLQueryPlan( qd.getQueryString(), false, CollectionHelper.EMPTY_MAP );
      485   			}
    Any suggestions???

  • #2
    Hello

    Code:
    @NamedQuery(name="getAllCustomer",
    query="SELECT cust FROM CUSTOMER cust 
    WHERE cust.STATUS=? AND cust.BILLING_CODE=?")
    Are you trying to get an object Customer, right?

    I think your HQL is wrong

    Try with (when you work with HQL You must think about Java classes not in the alias used in the DB (Customer, not CUSTOMER)

    Code:
    query="FROM Customer cust WHERE cust.STATUS=? AND cust.BILLING_CODE=?")
    The code above could not work, due I dont know the name properties for
    STATUS and BILLING_CODE in the Java class level

    HTH

    You mu

    Comment


    • #3
      Thank you for quick reply.

      Yes, I'm retrieving list of objects(customer) which matches the criteria.

      Here is what I did after reading your suggestion but still the same error (changes highlighted in red:

      Code:
      @Entity
      @Table(name="CUSTOMER")
      @NamedQuery(name="getAllCustomer",query="FROM Customer cust WHERE cust.status=? AND cust.billingCode=?")
      public class Customer implements Serializable {
      
      	// entity properties and JPA annotated getters/setters
      	private String status;
      	private String billingCode;
      
      //changed named query where clause parameters from table column names to entity attribute names.
      }
      Now, there are no errors however it doesn't return any results????

      Code:
      2011-05-26 16:20:18,514 [http-8080-Processor24] TRACE org.hibernate.engine.query.QueryPlanCache - located HQL query plan in cache (FROM Customer cust WHERE cust.status=? AND cust.billingCode=?)
      2011-05-26 16:20:18,514 [http-8080-Processor24] TRACE org.hibernate.engine.query.QueryPlanCache - located HQL query plan in cache (FROM Customer cust WHERE cust.status=? AND cust.billingCode=?)
      2011-05-26 16:20:18,514 [http-8080-Processor24] TRACE org.hibernate.engine.query.HQLQueryPlan - find: FROM Customer cust WHERE cust.status=? AND cust.billingCode=?
      2011-05-26 16:20:18,514 [http-8080-Processor24] TRACE org.hibernate.engine.QueryParameters - parameters: [APPROVED, AP0045D]
      2011-05-26 16:20:18,514 [http-8080-Processor24] TRACE org.hibernate.engine.QueryParameters - named parameters: {}
      2011-05-26 16:20:18,514 [http-8080-Processor24] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
      2011-05-26 16:20:18,514 [http-8080-Processor24] DEBUG org.hibernate.SQL - select customer0_.CUST_ID as CUST1_20_, customer0_.STATUS as CUST2_20_, customer0_.CUST_NAME as CUST3_20_, customer0_.BILLING_CODE as CUST4_20_, customer0_.CUST_VALUE as CUST5_20_ from CUSTOMER customer0_ where customer0_.STATUS=? and customer0_.BILLING_CODE=?
      2011-05-26 16:20:18,514 [http-8080-Processor24] TRACE org.hibernate.jdbc.AbstractBatcher - preparing statement
      2011-05-26 16:20:18,529 [http-8080-Processor24] TRACE org.hibernate.type.StringType - binding 'APPROVED' to parameter: 1
      2011-05-26 16:20:18,529 [http-8080-Processor24] TRACE org.hibernate.type.StringType - binding 'AP0045D' to parameter: 2
      2011-05-26 16:20:18,545 [http-8080-Processor24] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
      2011-05-26 16:20:18,561 [http-8080-Processor24] TRACE org.hibernate.loader.Loader - processing result set
      2011-05-26 16:20:18,561 [http-8080-Processor24] TRACE org.hibernate.loader.Loader - done processing result set (0 rows)
      2011-05-26 16:20:18,561 [http-8080-Processor24] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
      2011-05-26 16:20:18,561 [http-8080-Processor24] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
      2011-05-26 16:20:18,561 [http-8080-Processor24] TRACE org.hibernate.jdbc.AbstractBatcher - closing statement
      2011-05-26 16:20:18,561 [http-8080-Processor24] TRACE org.hibernate.loader.Loader - total objects hydrated: 0
      Last edited by tiger.spring; May 26th, 2011, 03:31 PM. Reason: answering question

      Comment


      • #4
        Hello

        Code:
        ERROR org.hibernate.impl.SessionFactoryImpl - Error in named query: getAllCustomer
        org.hibernate.hql.ast.QuerySyntaxException: CUSTOMER is not mapped
        You Must use

        Code:
        @NamedQuery(name="getAllCustomer",
        query="FROM Customer cust WHERE cust.status=? AND cust.billingCode=?
        The red color CUSTOMER is used on your DB side , but Customer is your Java Class and used by Hibernate, read Hibernate documentation

        Post your complete DAO method, yo have a better idea

        Comment


        • #5
          Thank you for your help. It is working now!

          Comment

          Working...
          X