5 Replies Latest reply on Feb 24, 2007 8:25 PM by Micho Edershvili

    Aliases in Queries

    Micho Edershvili Newbie

      Hi,
      I´m not sure if this or the "persistence" group is the right one.

      I created a Query like this

      erg = (List<User>) em.createQuery( "from User u where (u.peter = :name)").setParameter ("name", "pete").getResultList();
      
      and it wors as expected.
      If I change it an introduce an alias
      erg = (List<User>) em.createQuery( "from User where (peter = :name)").setParameter ("name", "pete").getResultList();
      and it works.
      

      I get an exception
      javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.QueryException: could not resolve property: peter of: com.entity.User [from com.entity.User as u where (u.peter = :name)]
      


      what´s wrong?


        • 1. Re: Aliases in Queries
          mazz Master

          This is saying your User object does not have a "peter" attribute.

          I think you want something like:

          "from User u where u.name = :name"

          and you setParameter("name", "pete").

          I assume your User object has an attribute "name" (i.e. User.getName()) and I assume your User doesn't have a User.getPeter() :)

          • 2. Re: Aliases in Queries
            Micho Edershvili Newbie

            I exchanged the two codeexamples.
            if there is no attribute "peter" why does the query

            "from User where (peter = :name)"

            work.

            If I add the alias u and write
            from User u where (u.peter = :name)

            I get the exception.

            I coded the attribute like this:
            @Column(name = "peter")
             public String getHugo()
             { return mHugo; }
            


            • 3. Re: Aliases in Queries
              mazz Master

              First, as I understand it, the @Column annotation defines the actual database column name but has no bearing on how the ORM tool identifies this column in the JPQL query language.

              @Column(name = "peter")
              public String getHugo()

              This entity's column is "Hugo" when you identify it in your JPQL queries. "peter" is the actual name of the column in the database, and is used when the JPA implementation (Hibernate in JBoss EJB's case) builds the actual SQL that it sends to the database. So, I definitely see why u.peter fails - it should be u.hugo.

              I don't know what that first query is doing... "peter = :name". Turn on Hibernate SQL debugging (show_sql=true) to look at the actual SQL its issuing - that usually tells you alot about what's happening.

              • 4. Re: Aliases in Queries
                Micho Edershvili Newbie

                Thanks for your answer.

                How do I have to name the property an a query in my example

                @Column(name = "peter")
                 public String getHugo()
                 { return mHugo; }
                

                peter, Hugo, mHugo
                the doku says
                The logical column name is defined by the Hibernate NamingStrategy implementation. The default EJB3 naming
                strategy use the physical column name as the logical column name. Note that this may be different than the
                property name (if the column name is explicit). Unless you override the NamingStrategy, you shouldn't worry
                about that.

                I understood that the pysical name is the name in the database, that´s wrong?

                from User as u where (u.Hugo = :name)

                throws the same exception
                from User where (Hugo = :name)

                throws a grammar exception.

                The query, thats ok
                "from User where (peter = :name)") .setParameter ("name", "y")

                retrieves all User Objects with paremater "peter" = "y"

                How can I turn on the SQL debugging.



                • 5. Re: Aliases in Queries
                  Micho Edershvili Newbie

                  thanks

                  from User u where (u.hugo = :name)

                  works