3 Replies Latest reply on Oct 6, 2010 7:05 AM by Leo van den berg

    EntityQuery and named queries

    Karsten Wutzke Expert

      I'n new to Seam and I'm skimming through Seam in Action, chapter 10, section 10.4 Smarter queries with the Query component. I was wondering why EntityHome is implemented in Java code and EntityQuery is implemented in XML. I really don't want to mix Java and XML by putting my queries into XML files. Why was it done that way? It is a kind of idiomatic, inconsistent break IMHO.


      I was rather thinking about a solution to combine JPA/Hibernate named queries (annotations) with EntityQuery if that's all possible. Is there a way to do it in a straightforward manner, that is without using Reflection or similar hacks?


      Karsten

        • 1. Re: EntityQuery and named queries
          Leo van den berg Master

          Hi Karsten,


          You can use EntityHome and EntityQuery in java AND in xml. It all depends on how you want to use the component. For fast development without any additional stuff you can use the XML definition in components.xml, but if you need to override something you can extend EntityQuery and override the different methods.


          At the moment I tend to use external query strings instead of namedQueries because in one way or the other (entity or xml) you must adapt you domainmodel to change your namedQueries and that's an additional (maintence nd testing step).


          Leo   

          • 2. Re: EntityQuery and named queries
            Karsten Wutzke Expert

            Hello Leo, thanks for your reply.


            I was thinking about externalizing the JPQL/HQL strings, too. However, I don't see the benefits of doing so (right now). (Note, this is my first approach writing a web app in a professional way where I'm not just putting the logic whereever it just seems to be working.)


            As the entity classes are in code, I thought it would be straightforward to put the query strings into code as well. My EntityHome's will be Java code, at least that's what I'm planning to do. I might be wrong maintaining the QL strings in Java. It's probably, as stated above, that I don't see the benefits of externalizing non-SQL query strings, because you can't use them anywhere but from within code anyway, e.g. in an DBMS command prompt.


            The idea I liked about annotated named queries is that you can use the concatenation operator to make your statements more readable, but the concats are just executed at app startup once. Putting the statements in XML doesn't allow for proper formatting, at least not that I know of.


            So in essence, what are the benefits of externalizing non-SQL queries


            and


            what would be the right way to achieve good readability?


            Karsten


            PS: There's nothing I see what needed to be overridden in EntityQuery...

            • 3. Re: EntityQuery and named queries
              Leo van den berg Master

              Hi,


              - When you change from a single - monolytic - seam project , wehere everything is in the same project everything works great, but at the moment that you add extrenal modules (that's the way we do our domain model), then every change to the queries need an addtional testing/packaging step, which is quite frustrating.


              - The readibility issue is just a matter of taste. I addedthe next example to show how I have a namedQuery in my code:




              @NamedQueries(value = { 
                        @NamedQuery(
                                  name="findAllActiveIncidents", 
                                  query=
                                       "select distinct " +
                                       "  i " +
                                       "from Incident i " +
                                       "  left join fetch i.attributes a " +
                                       "  left join fetch i.location.geoPoint l " +
                                       "  left join fetch i.location.geoPoint.road r " +
                                       "  left join fetch i.eventType t " +
                                       "  left join fetch t.typeGroup g " +
                                       "  left join fetch i.eventSeverity s " +
                                       " where " +
                                       "   i.closureTimestamp is null " +
                                       " order by i.endTimestamp asc" +
                                       "      ,i.startTimestamp desc " +
                                       "      ,i.id desc ")
              })          




              - about the entityQery, you can override the getEjbQl metod to insert the string you want. Something like:




              @Override
                   public String getEjbql(){
                        return "select  e  from  MyOwnEntity e";
                   }
              
              



              It's all try and error....


              Leo