3 Replies Latest reply on Nov 28, 2007 1:42 AM by mazz

    EJB3 query language

      is there a specific way to write sql queries in entity beans in ejb spec 3 rather than normal sql query?

      i have defined namedquery as follow.
      @NamedQueries( {@NamedQuery(name = "Language.findByAgentCode",
      query = "SELECT l FROM Language l WHERE l.AgentCode = :agentcode")})

      but when deploying following exception is thrown

      2007-11-28 10:03:31,237 WARN [org.jboss.system.ServiceController] Problem starting service persistence.units:ear=sdb.ear,jar=sdb3.jar,unitName=Language
      javax.persistence.PersistenceException: org.hibernate.HibernateException: Errors in named queries: Language.findByAgentCode
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:720)
      at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:127)
      at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:246)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

      pls help me to solve this problem

        • 1. Re: EJB3 query language
          mazz

          is there additional stack traces (Caused By?) that you didn't attach? usually it'll tell you a bit more about what's wrong with the query.

          The JPQL looks good, make sure:

          a) You have an actual @Entity named Language
          b) Language has a mapped @Column "AgentCode"
          c) if you mapped on the getter, it must be "getAgentCode", if you mapped a field, it must be named AgentCode. In fact, I'm not sure if that capital 'A' in your JPQL will cause a problem or not - I've always used lower case and it always matched my lower-case field names, so I don't know how case-sensitivity works

          Perhaps its best for you to post your Language entity and its mappings.

          • 2. Re: EJB3 query language

            this is my entity bean
            package com.genesiis.sdb3.master3;

            import java.io.Serializable;
            import javax.persistence.Column;
            import javax.persistence.Entity;
            import javax.persistence.Id;
            import javax.persistence.NamedQueries;
            import javax.persistence.NamedQuery;
            import javax.persistence.Table;

            @Entity
            @Table(name = "Language")

            @NamedQuery(name = "Language.findByAgentCode", query = "SELECT l FROM Language L WHERE L.AgentCode = :agentCode")
            public class Language3 implements Serializable {
            @Id
            @Column(name = "LanguageCode", nullable = false)
            private String languageCode;

            @Column(name = "AgentCode")
            private String agentCode;

            @Column(name = "Description")
            private String description;

            @Column(name = "SortKey")
            private int sortKey;

            @Column(name = "LastUpdatedUser")
            private String lastUpdatedUser;

            public Language3() {
            }

            public Language3(String languageCode, String agentCode, String description,
            int sortkey, String lastUpdatedUser) {
            this.languageCode = languageCode;
            this.agentCode = agentCode;
            this.description = description;
            this.sortKey = sortkey;
            this.lastUpdatedUser = lastUpdatedUser;
            }

            public void setLanguageCode(String languageCode){
            this.languageCode = languageCode;
            }

            public void setAgentCode(String agentCode){
            this.agentCode = agentCode;
            }

            public void setDescription(String description){
            this.description = description;
            }

            public void setSortKey(int sortKey){
            this.sortKey = sortKey;
            }

            public void setLastUpdatedUser(String user){
            this.lastUpdatedUser = user;
            }

            /***getters*************************/
            public String getLanguageCode(){
            return this.languageCode;
            }

            public String getAgentCode(){
            return this.agentCode;
            }

            public String getDescription(){
            return this.description;
            }

            public int getSortKey(){
            return this.sortKey;
            }

            public String getLastUpdatedUser(){
            return lastUpdatedUser ;
            }

            public int hashCode() {
            int hash = 0;
            hash += (this.languageCode != null ?
            this.languageCode.hashCode() : 0);
            return hash;
            }

            public boolean equals(Object object) {
            if (object == null || !this.getClass().equals
            (object.getClass())) {
            return false;
            }
            Language3 other = (Language3)object;
            if (this.languageCode != other.languageCode &
            (this.languageCode == null || !this.languageCode.
            equals(other.languageCode))) {
            return false;
            }
            return true;
            }



            public String toString() {
            return "" + this.languageCode;
            }

            }

            And the persistemce.xml is as follow


            <persistence-unit name="Language">
            This unit manages orders and customers.
            It does not rely on any vendor-specific features and can
            therefore be deployed to any persistence provider.

            <jta-data-source>java:jdbc/sdbDatabase</jta-data-source>
            <jar-file>../sdb3.jar</jar-file>
            com.genesiis.sdb3.master3.Language3
            </persistence-unit>


            what would be the error??

            • 3. Re: EJB3 query language
              mazz

              I see. You are thinking too much in the SQL world - you're in the land of JPA now :) Must think in terms of the JPQL query language. @NamedQueries are written in JPQL, not SQL.

              Your table name is Language but your entity is "Language3". Your column is "AgentCode" but you mapped the field "agentCode" (again, on this point I am not sure - does case matter? I am not sure but I would assume case-sensitivity just to be sure and use agentCode in your query).

              Your named query should be:

              SELECT L FROM Language3 L WHERE L.agentCode = :agentCode


              I suggest you go through some EJB3/JPA tutorials, specifically those that introduce the JPA querying concepts and JPQL.