2 Replies Latest reply on Aug 25, 2009 10:31 PM by burnsjc

    Seam-gen generated EntityQueries not respecting RESTRICTIONS

    burnsjc

      Hello Seam community.  First let me say that if this is not a seam question/problem but more of a hibernate one, please let me know and I will post the question over there.


      So a simplified version of my scenario (I'll remove all the columns but one, etc...  ) -  I used seam-gen generate to initially create the structure of my project.  Let's say that that gave me a Users entity such:


      @Entity
      @Table(name = "users", uniqueConstraints = @UniqueConstraint(columnNames = "user_name"))
      public class Users implements java.io.Serializable {
      
           private Integer userId;
           private String userName;
      <snip>
      



      and a UsersList EntityQuery such:


      @Name("usersList")
      public class UsersList extends EntityQuery<Users> {
      
           private static final String EJBQL = "select users from Users users";
      
           private static final String[] RESTRICTIONS = {"lower(users.userName) like lower(concat(#{usersList.users.userName},'%'))",};
      
           private Users users = new Users();
      <snip>
      



      I now populate my DB with 10 users and run this code:


      UsersList ul = new UsersList();
      ul.getUsers().setUserName("John");
      List<Users> allResults = ul.getResultList();
      



      Now since the Javadoc for getResultList says it will update the restriction values, I expect this query to return all users that start with John.  This is what I see in the logs though:


      21:41:33,320 INFO  [STDOUT] Hibernate:
          select
              users0_.user_id as user1_89_,
              users0_.user_name as user2_89_
          from
              db.users users0_ limit ?
      



      First, I am confused why I don't see the where clause in the resulting hibernate call.  Second, iterating over the returned result set indeed shows that all 10 users have been returned.


      I have tried several different tests such as setting the restriction operator to and (and even or just to see), marked the EntityManager using @PersistentContext, making the bean Stateful, using unleaded gas, etc...


      I'm hoping someone can see it's just a piece of code I've left out or don't understand how to use from seam-gen's resulting structure.


      Any help is greatly appreciated.  Thanks!

        • 1. Re: Seam-gen generated EntityQueries not respecting RESTRICTIONS
          fup

          A restriction is ignored if the expression resolves to null (or empty string). Perhaps #{usersList.users.userName} is resolved to null ?

          • 2. Re: Seam-gen generated EntityQueries not respecting RESTRICTIONS
            burnsjc

            Good to know!  First I have to apologize that I forgot to include in my previous post that this was on JBoss 5.0.1 and Seam 2.1.2.


            I ran a test using:


            UsersList ul = new UsersList();
            ul.setEjbql("select users from Users users where users.userName='" + credentials.getUsername() + "'");
            



            And the return results worked as expected.  So it seems that changing the Users instance that's created as part of UsersList doesn't update the Restrictions string (once it's created during UsersList creation).  Odd, I would expect it update based on the JavaDoc, but that's okay.


            The manually setting the EJBQL solution works for me, just wanted to follow up in case this shows up in someone's Google search. :)