-->
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.  [ 8 posts ] 
Author Message
 Post subject: Random inserts/deletes on a select
PostPosted: Fri Sep 15, 2006 10:53 am 
Beginner
Beginner

Joined: Tue Apr 19, 2005 9:39 am
Posts: 45
I have 3 tables {Group, FeatureAccess, Feature}. The Group table and the Feature table have a many-to-many relationship, and per Database Design, I created an associative table that makes the relationships one-to-manies.

Okay without going into too much detail, here is the concept. An User can be part of MANY Groups and a Group has Many Features (this relationship is
Group (1) -----> (1..*) FeatureAccess (1..*) <----- (1) Feature

FeatureAccess has attributes Group, Feature, and access (int)

I want to be able to do group.getFeatureAccess() and feature.getFeatureAccess()

Here are my mapping files:

Code:
   Group.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="com.saic.ide.group.Group" table="idegroup">
      <id name="id" column="groupID" type="long">
         <generator class="native"/>
      </id>   
      <property name="groupName" column="groupName"    type="string"/>
      <property name="description" column="groupDescription" type="string"/>
      
      <set name="features" table="featureaccess" lazy="false">
          <key column="groupID"/>
          <composite-element class="com.saic.ide.menu.feature.FeatureAccess">
              <property name="access" type="int" not-null="true"/>
             <many-to-one name="feature" class="com.saic.ide.menu.feature.Feature" lazy="false" column="featureID"/>
          </composite-element>
      </set>
   </class>
   
</hibernate-mapping>



Code:
Feature.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="com.saic.ide.menu.feature.Feature" table="feature">
      <id name="id" column="featureID" type="long">
         <generator class="native"/>
      </id>   
      <property name="visible" column="visible"    type="boolean"/>
      <property name="iconPath" column="iconPath"    type="string"/>
      <property name="feature" column="name"   type="string"/>
      <property name="forward" column="forward"   type="string"/>   
      <many-to-one name="menu" column="menuID" class="com.saic.ide.menu.Menu" not-null="false" lazy="false"/>   
      <!--<set name="groups" table="featureaccess" lazy="false">
         <key column="featureID"/>
         <many-to-many class="com.saic.ide.group.Group" column="groupID"/>
      </set>
      -->
      <set name="accessGroups" table="featureaccess" lazy="false">
          <key column="featureID"/>
          <composite-element class="com.saic.ide.menu.feature.FeatureAccess">
              <property name="access" type="int" not-null="true"/>
             <many-to-one name="group" class="com.saic.ide.group.Group" column="groupID" lazy="false"/>
          </composite-element>
      </set>
      
   </class>



Code:

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="com.saic.ide.user.User" table="user">
      <id name="id" column="userID" type="long">
            <generator class="increment"/>
      </id>
      <property name="firstName" column="firstname"   type="string"/>
      <property name="lastName"  column="lastname"   type="string" />
      <property name="middleName"  column="middle"   type="string" />
      <property name="email"  column="email"   type="string" />
      <property name="title"  column="title"   type="string" />

      <set name="groups" lazy="false" table="usergroup">
         <key column="userID"/>
         <many-to-many class="com.saic.ide.group.Group" column="groupID"/>
      </set>
   
   </class>
   
</hibernate-mapping>



When hibernate retrieves a User there are several random inserts and deletes they look like:

Code:
Hibernate: select this_.groupID as groupID5_0_, this_.groupName as groupName5_0_, this_.groupDescription as groupDes3_5_0_ from idegroup this_
Hibernate: select features0_.groupID as groupID0_, features0_.access as access0_, features0_.featureID as featureID0_ from featureaccess features0_ where features0_.groupID=?
Hibernate: select feature0_.featureID as featureID3_0_, feature0_.visible as visible3_0_, feature0_.iconPath as iconPath3_0_, feature0_.name as name3_0_, feature0_.forward as forward3_0_, feature0_.menuID as menuID3_0_ from feature feature0_ where feature0_.featureID=?
Hibernate: select menu0_.menuID as menuID2_0_, menu0_.menuName as menuName2_0_, menu0_.visible as visible2_0_ from menu menu0_ where menu0_.menuID=?
Hibernate: select features0_.menuID as menuID1_, features0_.featureID as featureID1_, features0_.featureID as featureID3_0_, features0_.visible as visible3_0_, features0_.iconPath as iconPath3_0_, features0_.name as name3_0_, features0_.forward as forward3_0_, features0_.menuID as menuID3_0_ from feature features0_ where features0_.menuID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select feature0_.featureID as featureID3_0_, feature0_.visible as visible3_0_, feature0_.iconPath as iconPath3_0_, feature0_.name as name3_0_, feature0_.forward as forward3_0_, feature0_.menuID as menuID3_0_ from feature feature0_ where feature0_.featureID=?
Hibernate: select menu0_.menuID as menuID2_0_, menu0_.menuName as menuName2_0_, menu0_.visible as visible2_0_ from menu menu0_ where menu0_.menuID=?
Hibernate: select features0_.menuID as menuID1_, features0_.featureID as featureID1_, features0_.featureID as featureID3_0_, features0_.visible as visible3_0_, features0_.iconPath as iconPath3_0_, features0_.name as name3_0_, features0_.forward as forward3_0_, features0_.menuID as menuID3_0_ from feature features0_ where features0_.menuID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select feature0_.featureID as featureID3_0_, feature0_.visible as visible3_0_, feature0_.iconPath as iconPath3_0_, feature0_.name as name3_0_, feature0_.forward as forward3_0_, feature0_.menuID as menuID3_0_ from feature feature0_ where feature0_.featureID=?
Hibernate: select menu0_.menuID as menuID2_0_, menu0_.menuName as menuName2_0_, menu0_.visible as visible2_0_ from menu menu0_ where menu0_.menuID=?
Hibernate: select features0_.menuID as menuID1_, features0_.featureID as featureID1_, features0_.featureID as featureID3_0_, features0_.visible as visible3_0_, features0_.iconPath as iconPath3_0_, features0_.name as name3_0_, features0_.forward as forward3_0_, features0_.menuID as menuID3_0_ from feature features0_ where features0_.menuID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select feature0_.featureID as featureID3_0_, feature0_.visible as visible3_0_, feature0_.iconPath as iconPath3_0_, feature0_.name as name3_0_, feature0_.forward as forward3_0_, feature0_.menuID as menuID3_0_ from feature feature0_ where feature0_.featureID=?
Hibernate: select menu0_.menuID as menuID2_0_, menu0_.menuName as menuName2_0_, menu0_.visible as visible2_0_ from menu menu0_ where menu0_.menuID=?
Hibernate: select features0_.menuID as menuID1_, features0_.featureID as featureID1_, features0_.featureID as featureID3_0_, features0_.visible as visible3_0_, features0_.iconPath as iconPath3_0_, features0_.name as name3_0_, features0_.forward as forward3_0_, features0_.menuID as menuID3_0_ from feature features0_ where features0_.menuID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: select feature0_.featureID as featureID3_0_, feature0_.visible as visible3_0_, feature0_.iconPath as iconPath3_0_, feature0_.name as name3_0_, feature0_.forward as forward3_0_, feature0_.menuID as menuID3_0_ from feature feature0_ where feature0_.featureID=?
Hibernate: select menu0_.menuID as menuID2_0_, menu0_.menuName as menuName2_0_, menu0_.visible as visible2_0_ from menu menu0_ where menu0_.menuID=?
Hibernate: select features0_.menuID as menuID1_, features0_.featureID as featureID1_, features0_.featureID as featureID3_0_, features0_.visible as visible3_0_, features0_.iconPath as iconPath3_0_, features0_.name as name3_0_, features0_.forward as forward3_0_, features0_.menuID as menuID3_0_ from feature features0_ where features0_.menuID=?
Hibernate: select accessgrou0_.featureID as featureID0_, accessgrou0_.access as access0_, accessgrou0_.groupID as groupID0_ from featureaccess accessgrou0_ where accessgrou0_.featureID=?
Hibernate: delete from featureaccess where groupID=? and access=?
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: insert into featureaccess (groupID, access, featureID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)
Hibernate: delete from featureaccess where featureID=? and access=?
Hibernate: insert into featureaccess (featureID, access, groupID) values (?, ?, ?)


I did some research and it says Hibernate sometimes does brute force type retrieves but the blog was on bags and idbags. All these random inserts and deletes are killing performance. And as more elements are added to the FeatureAccess table these random inserts/deletes increase proportionally.

Is there something wrong with my mapping files? Should I use a different collection type?

Do you have any advice. I will be more than happy to give you more detail if you need it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 15, 2006 11:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
read about automatic flush and FlushMode then you will understand what is going on.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Flush
PostPosted: Fri Sep 15, 2006 11:38 am 
Beginner
Beginner

Joined: Tue Apr 19, 2005 9:39 am
Posts: 45
Thanks will do so!

Brandon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 15, 2006 12:11 pm 
Beginner
Beginner

Joined: Tue Apr 19, 2005 9:39 am
Posts: 45
Max,
I am reading up on Flush modes, but logically is still makes no sense. Why would hibernate need to synch the memory with the database on a select statement? I can understand it wanting to synch on transaction but not a basic retrieve.

Would us using detatchcritirias have anything to do with this? Or our bidirection relationship between Group --> FeatureAccess and
Feature -->Feature?

Also we have a bidirectional relationship with a composite element. I have read that this is not possible with Hibernate, is that true? If so then that stinks because alot of database schemas have that. Do you have any suggestions as to how "work around" this problem?

The big thing I want to be able to do is given a user (1) get the groups they are part of (2) get the access to the features that the users groups have.

Ugh, seems almost easier to just implement my own Hibernate ;-).

But anywho, thank you for taking time out of your day to help an idiot.

Here are our DAOs for Feature, FeatureAccess and Group.

Code:

FeatureAccessDAO

public class HibernateFeatureAccessDAO extends HibernateDaoSupport implements FeatureAccessDAO {

   //initialize the logger
   private Logger log = Logger.getLogger(HibernateFeatureDAO.class);

   /**
    * This will return the feature access for the given
    *    user (ID).  This is mapped to a database view,
    *    so there is no updating, deleting, or creating
    *    of this mapped object.
    */
   @SuppressWarnings("unchecked")
   public List<UserFeatureAccess> getFeatureAccess(long userID) {
      if (userID <= 0) {
         log.error("Invalid userID was received by the HibernateFeatureAccessDAO.");
         return null;
      }//end if
      
      DetachedCriteria criteria = DetachedCriteria.forClass(UserFeatureAccess.class)
      .add(Expression.eq("userID", new Long(userID)));
      criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
      
      return getHibernateTemplate().findByCriteria(criteria);
   }//end getFeatureAccess

}//end HibernateFeatureAccessDAO




Code:
**
* @author nicholassp
*/
public class HibernateFeatureDAO extends HibernateDaoSupport implements FeatureDAO{

   private Logger log = Logger.getLogger(HibernateFeatureDAO.class);
   
   /**
    * This will add a Feature to the database.
    */
   public Long add(ISelectableBean feature) {
      return (Long)getHibernateTemplate().save(feature);
   }//end add

   /**
    * This will add or update the Acronym, depending
    *    on whether the key is already in the DB.
    */
   public void addorupdate(ISelectableBean bean) {
      getHibernateTemplate().saveOrUpdate(bean);
   }//end addorupdate
   
   /**
    * This will delete a Feature from the
    *    database.
    */
   public void delete(ISelectableBean feature) {
      getHibernateTemplate().delete(feature);
   }//end delete

   /**
    * This will update a Feature that currently
    *    exists in the database.
    */
   public void update(ISelectableBean feature) {
      getHibernateTemplate().update(feature);
   }//end update

   /**
    * This will retrieve a Feature from the database
    *   that matches the given ID.
    */
   public ISelectableBean retrieve(long featureId) {
      DetachedCriteria criteria = DetachedCriteria.forClass(Feature.class,"feature")
      .add(Restrictions.eq("id", featureId));
      List features = getHibernateTemplate().findByCriteria(criteria);
      if (features.size() > 0) {
         return (ISelectableBean) features.get(0);
      } else {
         log.warn("There was no Feature with the ID = " + featureId);
         return null;
      }//end if
   }//end retrieve
   
   /**
    * This will return all Features from the database.
    */
   @SuppressWarnings("unchecked")
   public List<ISelectableBean> retrieveAll() {
      DetachedCriteria criteria = DetachedCriteria.forClass(Feature.class,"feature");
      return getHibernateTemplate().findByCriteria(criteria);
   }//end retrieveAll
   
   /**
    * This will retrieve the Features that are currently
    *    not assigned to a Menu.
    */
   @SuppressWarnings("unchecked")
   public List<Feature> retrieveAvailableFeatures() {
      DetachedCriteria criteria = DetachedCriteria.forClass(Feature.class, "feature")
      .add(Restrictions.isNull("menu"));
      return getHibernateTemplate().findByCriteria(criteria);
   }//end retrieveAvailabeFeatures

   /**
    * This will return all the features given a set of groups.
    */
   @SuppressWarnings("unchecked")
   public List<ISelectableBean> retrieveAllByGroups(Set<Group> groups) {
      
      if (groups == null) {
         log.error("Set of groups sent in to retrieve links is null");
         return null;
      }//end if
      
      Iterator it = groups.iterator();
      String groupIds = "(";
      for (int i = 0; i < groups.size(); i++) {
         groupIds += ((Group) it.next()).getId();
         if(it.hasNext())
            groupIds+=",";
      }//end loop
      groupIds+=")";
      /**
      DetachedCriteria criteria = DetachedCriteria.forClass(Feature.class, "feature")
      .createCriteria("accessGroups")
      .add(Restrictions.in("group", groupIds));
      criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
      return getHibernateTemplate().findByCriteria(criteria);
      **/
      //String hql = "from Feature feature join feature.accessGroups as access where access.group.id = 1";
      //String hql = "from Feature as feature where feature.accessGroups.access = 7";
      //SQLQuery query = session.createSQLQuery(sql);
      //String query="from Acronym as acronym where acronym.groups.groupId in (1,2)";
      
      HibernateCallback temp = new CallbackImpl(groupIds);
      return getHibernateTemplate().executeFind(temp);
      
//      return getHibernateTemplate().find(hql);
   }//end retrieveAllByGroups
   
   
   public class CallbackImpl implements HibernateCallback{

      String groups = null;
      
      public CallbackImpl(String groups){
         this.groups = groups+ ";";
      }
      
      @SuppressWarnings("unchecked")
      public Object doInHibernate(Session session) throws HibernateException, SQLException {
         String sql = "select distinct feature.featureID,feature.menuID,name,forward,iconPath,visible from feature,featureaccess where feature.featureID=featureaccess.featureID and featureaccess.groupID in ";
         sql += groups;
         System.out.println(sql);
         SQLQuery query = session.createSQLQuery(sql);
         query = query.addEntity(Feature.class);
         List<Feature> retVal = query.list();
         return retVal;
      }
      
   }
   
   
}//end HibernateFeatureDAO


Code:
public class HibernateGroupDAO extends HibernateDaoSupport implements GroupDAO {
   
   private Logger log = Logger.getLogger(HibernateGroupDAO.class);
   
   /**
    * This will add a Group to the database.
    */
   public Long add(ISelectableBean group) {
      return (Long)getHibernateTemplate().save(group);
   }//end add
   
   /**
    * This will add or update the Acronym, depending
    *    on whether the key is already in the DB.
    */
   public void addorupdate(ISelectableBean bean) {
      getHibernateTemplate().saveOrUpdate(bean);
   }//end addorupdate
   
   /**
    * This will update a Group that currently
    *    exists in the database.
    */
   public void update(ISelectableBean group) {
      getHibernateTemplate().update(group);
   }//end update
   
   /**
    * This will delete a Group from the database.
    */
   public void delete(ISelectableBean group) {
      getHibernateTemplate().delete(group);
   }//end delete

   /**
    * This will retrieve a Group from the database
    *   that has a matching ID.
    */
   public ISelectableBean retrieve(long groupID) {
      DetachedCriteria criteria = DetachedCriteria.forClass(Group.class, "group")
      .add(Restrictions.eq("id", groupID));
      List groups = getHibernateTemplate().findByCriteria(criteria);
      
      if (groups.size() > 0) {
         return (ISelectableBean) groups.get(0);
      } else {
         log.warn("No group was returned for group ID: " + groupID);
         return null;
      }//end if
   }//end retrieve
   
   /**
    * This will retrieveAll the groups from
    *    the database.
    */
   @SuppressWarnings("unchecked")
   public List<ISelectableBean> retrieveAll() {
      DetachedCriteria criteria = DetachedCriteria.forClass(Group.class, "group");
      return getHibernateTemplate().findByCriteria(criteria);
   }//en retrieveAll
   
   /**
    * RetrieveAllByGroups is not needed for the GroupDAO in the
    *    current scope.
    */
   public List<ISelectableBean> retrieveAllByGroups(Set<Group> groups) {
      log.error("RetrieveAllByGroups is currently not implemented for GroupDAO.");
      return null;
   }//end retrieveAllByGroups
   
}//end HibernateGroupDAO


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 15, 2006 12:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well, if you do something like this:

Code:
  User u = session.load(User.class, 42);
  assertEquals("Maximillian", u.getName());
  u.setName("Max");
  User u2 = session.creaetQuery("from User u where u.name = 'Maximillian').uniqueResult();


would you now expect that:

u2 is null

or

u2.getName() == "Max"

or

u2.getName() == "Maximillian" ?

...I (and hibernate and other ORM's with a persistencecontext) expect it to be null with the default behavior.

To do that automatic flush has to occur when you do queries against entities that has been changed inmeory.

if you don't want that behavior then set flushmode to NONE and the above example would now have u2.getName() == "Max" even though you searched for Maximillian.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 15, 2006 1:24 pm 
Beginner
Beginner

Joined: Tue Apr 19, 2005 9:39 am
Posts: 45
I would expect it to return a User with the name set to'Maximillian', since you did not tell User to save to the database. I guess the discrepancy is I like to have control of what communication is going on between the model layer and the control layer.

Your example still doesn't quite match what is going on. I am literally doing something like this "Select * from user where user_index = 3", that one statement spawns off a lot of inserts and deletes. I didn't change anything I just selected. Now if the reason this happens is because I have a set of Groups then I think the order of operation is off.

Does Hiberante do this on the above select statement?

Step "What happens"
----------------------------------
1) Select * from user where user_index = 3;
2) Gets the user, at this point there is nothing in
the Group set for the User object returned
3) Since there is a mapping between User and Groups, get all
the Groups associated with this User
4) *** Since the User object that was returned in Step on
changed Hibernate needs to update that object (this matches your example below) Update the User Object

[Another question, instead of just doing an update why does it delete all records and then reinsert all of them again?] This goes inline with how Hibernate handles bags...if I change 1 element of 20, hibernate deletes all 20 from the database and inserts 19. Why not just update the associating table?

So it looks as though I need to set my flushmode to NONE. I will dig around to find where I set that for all my DAOs, or do I have to do it before each DetachedCriteria statement?

Thanks again, I do give it up to Hibernate for their support networks.


Top
 Profile  
 
 Post subject: Figured a work around..
PostPosted: Fri Sep 15, 2006 4:33 pm 
Beginner
Beginner

Joined: Tue Apr 19, 2005 9:39 am
Posts: 45
Max,

Sorry to double post, but wanted to let you know I found a work around for my problem. I am just setting FLUSH.NEVER in the transactions where I really don't need it...thanks for your help on this.

Brandon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 15, 2006 4:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
spoons10 wrote:
I would expect it to return a User with the name set to'Maximillian', since you did not tell User to save to the database.


So would u != u2 ? or do you expect hibernate to overwrite your inmemory changes when you query for data that "accidentially" has the same identifier ?

Quote:
I guess the discrepancy is I like to have control of what communication is going on between the model layer and the control layer.


You can have that control (as you have found with FetchMode.NONE or if you don't want the statemanagement at all use StatelessSession), but you should really learn what Hibernate as an ORM actually does and don't assume it work like if you wrote pure jdbc. It does more and for a reason

Quote:
Your example still doesn't quite match what is going on. I am literally doing something like this "Select * from user where user_index = 3", that one statement spawns off a lot of inserts and deletes. I didn't change anything I just selected. Now if the reason this happens is because I have a set of Groups then I think the order of operation is off.


so you are saying that you did not change anything of the already in-memory/in-session objects? then something is wrong because hibernate apparently detected a change....

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.