-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate firing a large number of select queries while savi
PostPosted: Wed Sep 17, 2014 9:23 pm 
Newbie

Joined: Wed Sep 17, 2014 9:20 pm
Posts: 2
Hibernate fires one select query for each of the associated object that it is trying to persist. If there are 5000 associated objects then 5000 selects followed by 5000 inserts!!!

This is leading to performance issues. Can any one please shed some light on why hibernate is doing so or what configuration below is leading to this. Thanks.

Below are the sqls being fired by hibernate while saving a FighterjetDO object, note in this sample there are 5 select queries for 5 inserts that hibernate is firing for the associated object set FighterjetMissionsDO:

Code:
Hibernate: select max(jet_id) from fighterjet
    Hibernate: select fighterjet_.mission_id, fighterjet_.JET_ID, fighterjet_.name as name1_ from fighterjetmissions fighterjet_ where fighterjet_.mission_id=? and fighterjet_.JET_ID=?
    Hibernate: select fighterjet_.mission_id, fighterjet_.JET_ID, fighterjet_.name as name1_ from fighterjetmissions fighterjet_ where fighterjet_.mission_id=? and fighterjet_.JET_ID=?
    Hibernate: select fighterjet_.mission_id, fighterjet_.JET_ID, fighterjet_.name as name1_ from fighterjetmissions fighterjet_ where fighterjet_.mission_id=? and fighterjet_.JET_ID=?
    Hibernate: select fighterjet_.mission_id, fighterjet_.JET_ID, fighterjet_.name as name1_ from fighterjetmissions fighterjet_ where fighterjet_.mission_id=? and fighterjet_.JET_ID=?
    Hibernate: select fighterjet_.mission_id, fighterjet_.JET_ID, fighterjet_.name as name1_ from fighterjetmissions fighterjet_ where fighterjet_.mission_id=? and fighterjet_.JET_ID=?
    Hibernate: insert into fighterjet (name, max_speed, country, jet_id) values (?, ?, ?, ?)
    Hibernate: insert into fighterjetmissions (name, mission_id, JET_ID) values (?, ?, ?)
    Hibernate: insert into fighterjetmissions (name, mission_id, JET_ID) values (?, ?, ?)
    Hibernate: insert into fighterjetmissions (name, mission_id, JET_ID) values (?, ?, ?)
    Hibernate: insert into fighterjetmissions (name, mission_id, JET_ID) values (?, ?, ?)
    Hibernate: insert into fighterjetmissions (name, mission_id, JET_ID) values (?, ?, ?)


Below are the domain objects and their hbms :

FighterjetDO :

Code:
public class FighterjetDO implements Serializable
    {
       private static final long serialVersionUID = 1L;
   
       private Integer jetId;
       
       private String name;
       
       private Integer maxSpeed;
       
       private String country;
       
       private Set<FighterjetMissionsDO> fighterjetMissionsDOSet;
   
       // Setters and getters.
   
   
    }

FighterjetDO HBM :

Code:
   <hibernate-mapping>
        <class name="com.man.fighterjet.FighterjetDO" table="fighterjet">
            <id name="jetId" type="int" column="jet_id">
                <generator class="increment" />
            </id>
            <property name="name">
                <column name="name" />
            </property>
            <property name="maxSpeed">
                <column name="max_speed" />
            </property>
            <property name="country">
                <column name="country" />
            </property>
           
            <set name="fighterjetMissionsDOSet" lazy="true" table="fighterjetmissions" fetch="join" cascade="save-update" sort="unsorted" inverse="true">
             <key column="JET_ID" />
             <one-to-many class="com.man.fighterjet.FighterjetMissionsDO" />
          </set>
        </class>
    </hibernate-mapping>

FighterjetMissionsDO :

Code:
public class FighterjetMissionsDO implements Serializable
    {
   
       private static final long serialVersionUID = 1L;
   
       private Integer missionId;
   
       private String name;
   
       private FighterjetDO fighterjetDO;
   
       // Setters and getters.
   
    }


FighterjetMissionsDO HBM :

Code:
<hibernate-mapping>
        <class name="com.man.fighterjet.FighterjetMissionsDO" table="fighterjetmissions">
            <composite-id>
             <key-property name="missionId" column="mission_id" type="integer" />
             
             <key-many-to-one name="fighterjetDO" class="com.man.fighterjet.FighterjetDO">
                   <column name="JET_ID"/>
                </key-many-to-one>
           </composite-id>
           
            <property name="name">
                <column name="name"/>
            </property>
        </class>
    </hibernate-mapping>



Test Code to populate and persist :

Code:
FighterjetDAO fighterjetDAO = new FighterjetDAO();
          
   
          Set<FighterjetMissionsDO> fighterjetMissionsDOSet = new HashSet<FighterjetMissionsDO>();
          
          FighterjetDO newFighterjetDO = new FighterjetDO();
          newFighterjetDO.setName("F22");
          newFighterjetDO.setMaxSpeed(1000);
          newFighterjetDO.setCountry("USA");
          
          FighterjetMissionsDO fighterjetMissionsDO1 = new FighterjetMissionsDO();
          fighterjetMissionsDO1.setMissionId(101);
          fighterjetMissionsDO1.setName("HIbernate");
          fighterjetMissionsDO1.setFighterjetDO(newFighterjetDO);
          fighterjetMissionsDOSet.add(fighterjetMissionsDO1);
   
          
          FighterjetMissionsDO fighterjetMissionsDO2 = new FighterjetMissionsDO();
          fighterjetMissionsDO2.setMissionId(102);
          fighterjetMissionsDO2.setName("HIbernate2");
          fighterjetMissionsDO2.setFighterjetDO(newFighterjetDO);
          fighterjetMissionsDOSet.add(fighterjetMissionsDO2);
          
          
          FighterjetMissionsDO fighterjetMissionsDO3 = new FighterjetMissionsDO();
          fighterjetMissionsDO3.setMissionId(103);
          fighterjetMissionsDO3.setName("HIbernate3");
          fighterjetMissionsDO3.setFighterjetDO(newFighterjetDO);
          fighterjetMissionsDOSet.add(fighterjetMissionsDO3);
          
          
          FighterjetMissionsDO fighterjetMissionsDO4 = new FighterjetMissionsDO();
          fighterjetMissionsDO4.setMissionId(104);
          fighterjetMissionsDO4.setName("HIbernate4");
          fighterjetMissionsDO4.setFighterjetDO(newFighterjetDO);
          fighterjetMissionsDOSet.add(fighterjetMissionsDO4);
          
          
          FighterjetMissionsDO fighterjetMissionsDO5 = new FighterjetMissionsDO();
          fighterjetMissionsDO5.setMissionId(105);
          fighterjetMissionsDO5.setName("HIbernate5");
          fighterjetMissionsDO5.setFighterjetDO(newFighterjetDO);
          fighterjetMissionsDOSet.add(fighterjetMissionsDO5);
   
          
          
          newFighterjetDO.setFighterjetMissionsDOSet(fighterjetMissionsDOSet);
          
          fighterjetDAO.createJet(newFighterjetDO);


FighterjetDAO :



Code:
   public void createJet(FighterjetDO fighterjetDO) {
            Transaction trns = null;
            Session session = HibernateUtil.getSessionFactory().openSession();
            try {
                trns = session.beginTransaction();
                session.save(fighterjetDO);
                session.getTransaction().commit();
            } catch (RuntimeException e) {
                if (trns != null) {
                    trns.rollback();
                }
                e.printStackTrace();
            } finally {
                session.flush();
                session.close();
            }
        }


Top
 Profile  
 
 Post subject: Re: Hibernate firing a large number of select queries while savi
PostPosted: Wed Sep 17, 2014 11:24 pm 
Newbie

Joined: Wed Sep 17, 2014 9:20 pm
Posts: 2
http://stackoverflow.com/questions/25902331/hibernate-firing-a-large-number-of-select-queries-while-saving-associated-object/25903056#25903056


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.