6 Replies Latest reply on Jan 24, 2011 3:20 PM by thegroove

    Weired JPA named query @NamedQuery behaviour

    thegroove

      Salut

       

      i recogniozed a strange behaviour, declaring anmed

      queries with a annotation. Here is some code:

       

       

       

      @NamedQueries(

          {

              @NamedQuery(

                  name=Dbean1.GET_COMPOSITION,

                  query=Dbean1.JPQL_COMPOSITION)

          }

      )

       

       

       

      where:

       

      @Entity

      public class Dbean1. implements Serializable

      {

         /* some code */

       

       

         final public static String GET_COMPOSITION = ".... composition " ;

         final public static String GET_COMPOSITION = "SELECT dbean FROM ..... " ;

       

       

      this looks fine. But imagine iff you a to implement a recurring part of the

      where expression and start thinking of a java code, that could help you to

      create that JPA-QL string.

      So when you write:

       

      @Entity

      public class Dbean1. implements Serializable

      {

         /* some code */

       

       

         final public static String GET_COMPOSITION      = ".... composition " ;


         final public static String GENERATED_WHERE_EXPR = generateWehreCompo() ;


         final public static String GET_COMPOSITION      =

                                "SELECT dbean FROM ..... +

                                WHERE" + GENERATED_WHERE_EXPR  ... ;

       

       

      JBoss5.1 will tell you "The value for annotation attribute NamedQuery.query must be a

      constant expression" So is final private static not constant enough ???

        • 1. Weired JPA named query @NamedQuery behaviour
          ilya40umov

          I guess that it's ok because when you are using annotations and named queries there is not need to generate something. Named queries in annotations are completely fixed and static and they are supposed to be read only once by Jboss. If you really want to generate where expression you can always use EntityManager.createQuery.

          see

          http://download.oracle.com/javaee/5/api/javax/persistence/EntityManager.html

          Actually I have never tried to generate where expressions for namedQueries because they should have been created before an application is deployed to an application server.

          • 2. Weired JPA named query @NamedQuery behaviour
            thegroove

            Salut Ilya,

             

            i understoot your argumentation, but i believe (definitivly not sure)

            this shall not be correct, because JAVA does not know anymodifier

            called "completly fixed and static".

             

            You may assign the annoation a final static String attribute, that is

            composed of other String using concenation using the +-operator.

            There is no reason, why +-operator works but a static method not!

            • 3. Weired JPA named query @NamedQuery behaviour
              ilya40umov

              IMHO Because a static field(like "a" + "b") is calculated when java code is compiled but a static function is a runtime thing. So that Jboss may read this annotations before this functions is run.

              • 4. Weired JPA named query @NamedQuery behaviour
                thegroove

                Salut Ilya,

                 

                iff your +-composition resides in another class, then you

                habe a class-loader job between. And then ???

                • 5. Weired JPA named query @NamedQuery behaviour
                  ilya40umov

                  I still think that "a class-loader job" does not mean executing any javacode.

                  http://www.objectdb.com/java/jpa/query/named

                  A named query is a statically defined query with a predefined unchangeable query string.

                  if you have a static function it means that you can change a query through changing this function. The logic of the function lies in runtime level and you can change it by changing some function parameters. I suppose that Jboss/Hibernate reads all the annotations before you static code is executed.

                   

                  P.S. I think that someone else can describe it more clearly. And what I'm saying is just my own opinion. And I'm sure that everyone can be mistaken.

                  • 6. Weired JPA named query @NamedQuery behaviour
                    thegroove

                    Salut Ilja,

                     

                    > A named query is a statically defined query with a predefined unchangeable query string.

                     

                    i agree to this, bit Dbean1.JPQL_COMPOSITION is definitivly static and

                    unachnageable. For that reason i use final-static modifier. But

                    Jboss still think, it is not.

                     

                    but:

                    @NamedQueries(

                        {

                            @NamedQuery(

                                name=Dbean1.GET_COMPOSITION,

                                query=Dbean1.JPQL_COMPOSITION)

                        }

                    )

                     

                     

                     

                    where:

                     

                    @Entity

                    public class Dbean1. implements Serializable

                    {

                       /* some code */

                     

                     

                       final public static String GET_COMPOSITION = ".... composition " ;

                       final public static String JPQL_COMPOSITION = "SELECT dbean FROM ..... " + Dbean1.dummyClass() ;