3 Replies Latest reply on Sep 30, 2008 8:53 PM by gjeudy

    Hibernate and seam performance

    bashan
      Hi,
      I was wondering about hibernate performance issues:
      1. If I have one to many relationship and I do: parent.childs.size()
      Will hibernate will be smart enough to fetch the size only without populating all the childs?
      If hibernate is not that smart, is there a better way of doing it?
      2. Similar issue: if I have many to many relation between article and users, and I would like to know if article is related with a user. Is  it efficient doing: article.contains(user)?
      If I do something like:
      Rendered="{mybean.isUserInArticle}"
      The code is usually activated several times. Is there a good solution for this issue in Seam?

      Thanks
        • 1. Re: Hibernate and seam performance
          amitev

          1. If I have one to many relationship and I do: parent.childs.size()
          Will hibernate will be smart enough to fetch the size only without populating all the childs?
          If hibernate is not that smart, is there a better way of doing it?

          Hibernate will fetch only the size if you mark the collection as extra-lazy. Look here.




          If I do something like: ...
          The code is usually activated several times. Is there a good solution for this issue in Seam?

          You can fetch the value only once and cache it for later use. Take a look at @Factory.


          • 2. Re: Hibernate and seam performance
            bashan

            Thanks,


            1) Will @Factory work with a calling to a parametrized function?
            How can I update the value if it is changed in an action?
            For example, here is a code I have for adding/removing data from favorites:


            @Name("mediaFavorite")
            public class MediaFavorite
            {
              @In
              private MediaDAO mediaDAO;
            
              @In
              Session nikoniansDatabase;
            
              @In(required = false)
              private User user;
            
              private Media media;
            
              private Boolean isInFavorites;
            
              public void favorite(int mediaId)
              {
                media = mediaDAO.getMedia(mediaId);
                user = (User)nikoniansDatabase.merge(user);
                if (!isInFavorites(media))
                {
                  media.getFavoriteUsers().add(user);
                  isInFavorites = true;
                }
                else
                {
                  media.getFavoriteUsers().remove(user);
                  isInFavorites = false;
                }
            
                nikoniansDatabase.persist(media);
              }
            
              public boolean isInFavorites(Media media)
              {
                if (isInFavorites == null)
                {
                  user = (User)nikoniansDatabase.merge(user);
                  isInFavorites = user != null && media.getFavoriteUsers().contains(user);
                }
            
                return isInFavorites; 
              }
            }
            


            2) Regarding the size() and contains() method, the Extra lazy is nice, but what would happen if I want to get the data from a list of objects?
            For example: if I show forum posts in a dataTable and one of the columns is: replies. So there will be a list called posts and in replies column I would do: post.replies.size(). Will it cause an additional query for each row shown in the table? If it does, is there a nice way of dealing with it in Hibernate?

            • 3. Re: Hibernate and seam performance
              gjeudy

              1) Going back to your initial question, where do you put the

              rendered="{mybean.isUserInArticle}"

              condition ? Inside a dataTable column? If so you can't avoid that it will be called repeatdly for each row. What you want to do is to ensure the computation is cheap.


              I'm not sure why @Factory was mentioned, this is to create components when referring through an EL expression which is not your usecase here I believe.


              2) I would be very surprised that hibernate caches the size() contains() results when used with extra lazy which would result in a db call each time you call it. You should verify this for yourself.


              Maybe you want to eagerly load all the posts and corresponding replies using HSQL with

              fetch join

              prior to rendering your view. It would make sense if you are going to display replies if present anyways.