3 Replies Latest reply on Oct 20, 2008 2:44 PM by torakuma

    FetchType.LAZY and number of records

    torakuma

      Hi-


      I'd like to be able to get the number of child records (that are loaded lazily) for display in a data table without actually loading the records.  The children are loaded later when editing for performance reasons.


      Here are the association and count properties in my parent entity



          @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
          public List<Child> getChildren() {
              return children;
          }
      
          @Transient
          public int getNumChildren() {
              // Calling getChildren() to get this is overkill
              // when all I want is the count!
              return numChildren;
          }



      My data table is using an EntityQuery object but I couldn't find a way to do it in there with JPQL.  It does, however, have a nice way to get the record count of the result list.  I just need the same thing for the association.


      I then thought I could use @Formula in the property itself to be something like...



          @Transient
          @Formula(value="select Child child from child where child.fk=#{parent.id}")
          public int getNumChildren() {
              return numChildren;
          }



      It seems as though my @Formula is being ignored.  I can put garbage in the value (i.e. value="lksajlksdfjksf") and I don't get an error in seam.  I'm new to @Formula so I may be messing this up.


      Could someone show me how to get the record count of an association (without calling the child getter) when using EntityQuery?


      Thanks!