1 Reply Latest reply on Dec 24, 2006 2:41 AM by murtuza52

    Hibernate loads all entities on manager.merge()

    murtuza52

      Current setup:
      JBoss 4.0.5, MySQL 5.0, Windows XP SP2

      The application we have developed has about 50 entities with inheritence. Each entity is mapped to a table and most OneToMany and ManyToMany relations are LAZY fetched except for few where application requires them to be EAGER fetched. Some of the entities have about 50,000 records and i have noticed in the logs that when manager.merge() is called on any entity, Hibernate load every entity from the database in memory before performing update. What surprises me is it even loads entities that are LAZY fected and even those that have absolutely no relation with current entity being merged. Can even help me to turn off this entities loading? I am sure hibernate is very flexible and there must be some tag or configuration where we can switch this off.

      Thanks
      Murtuza Vohra

        • 1. Re: Hibernate loads all entities on manager.merge()
          murtuza52

          I found the culprit. For those who have performance related issue, this might be one of the reasons. The culprit here was the use of FetchType.EAGER. This might be because of "load-before-update" strategy hibernate uses to check the columns have changed. Over use of EAGER means its going to load every related entities in memory and therefore performance is effected.

          Make all relations LAZY except for those that are absolutely required. You can easily debug and find out if the application server is working hard to synchronize with the database by using Use tags

           <property name="hibernate.show_sql" value="true"/>
           <property name="hibernate.use_sql_comments" value="true"/>
          

          in persistence.xml file. This will show you what is happening in background and you can find out what entities get loaded will persisting or merging.

          I hope hibernate or jboss comes up with better strategy to synchronize the data. This doesn't seems to be effecting the performance with server like GlassFish or JDO framework. They have different algorithm that performans more efficients.

          Feedback are welcomed to improve the performance further.

          Murtuza