-->
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.  [ 9 posts ] 
Author Message
 Post subject: Updating database when no modification was done
PostPosted: Sat Nov 03, 2007 6:23 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Hibernate version: 3.2

Mapping documents:

Code:
   <class name="ManagedObject" table="managed_object" proxy="ManagedObject">
   
      <id name="name" type="string" unsaved-value="null">
         <column name="name" length="50"/>
         <generator class="assigned"/>
      </id>
      
      <version name="version" column="version" access="field"/>
      
      <property name="description" column="description" type="text"/>
      
      <set name="policies" table="managed_object_policies" lazy="true">
         <key column="managed_object_id" not-null="true"
            foreign-key="mobj_policies_mobj_fk"/>
         <many-to-many class="Policy" column="policy_id"
            foreign-key="mobj_policies_policy_fk"></many-to-many>
      </set>
      
      
   </class>


Code:
   <class name="Policy" table="policy" proxy="Policy" >
      <id name="name" type="string" access="field" unsaved-value="null">
         <column name="name" length="50"/>
         <generator class="assigned"/>
      </id>
      
      <version name="version" column="version" access="field"/>
      
      <property name="description" column="description" type="text"/>
      
      <set name="acceptableAttributes" table="policy_attributes" lazy="true">
         <key column="policy_id" not-null="true"
            foreign-key="policy_attributes_policy_fk"/>
         <many-to-many class="PolicySimpleValue" column="policy_simple_value_id"
            foreign-key="policy_attributes_value_fk"/>
      </set>
      
   </class>


Code:
   <class name="PolicySimpleValue" table="policy_simple_value" proxy="PolicySimpleValue">
      <id name="name" type="string" access="field"  unsaved-value="null" >
         <column name="name" length="50"/>
         <generator class="assigned"/>
      </id>
      
      <version name="version" column="version" access="field"/>
      
      <property name="description" column="description" type="text"/>
      <property name="type" column="value_type" type="string" length="10" access="field"/>
      

      <set name="policies" inverse="true" table="policy_attributes" lazy="true">
         <key column="policy_simple_value_id" />
         <many-to-many column="policy_id" class="Policy" />
      </set>

   </class>


b]Code between sessionFactory.openSession() and session.close():[/b]
Code:
  // The _* attribues are hash-maps where objects are cached.

                        session.beginTransaction();
         ManagedObject mobj=(ManagedObject)sess
            .get(ManagedObject.class, pMobjName);

         Set<Policy> pls=mobj.getPolicies();
         if ( pls != null )
            for ( Policy pol : pls) {
               _policies.put(pol.getName(), pol);
               
               if ( pol.getAcceptableAttributes() != null ) {
                  for ( PolicySimpleValue ps : pol.getAcceptableAttributes() ) {
                     _values.put(ps.getName(), ps);
                  }
               }
                     
            }

         _managedObjects.put(mobj.getName(), mobj);
                       session.getTransaction().commit();



Name and version of the database you are using: Postgres 8.2

The generated SQL (show_sql=true):
Code:

For each policy  and its acceptable atributes all these sqls
are called when commit is called.

Hibernate:
    update
        auth.policy
    set
        version=?,
        description=?
    where
        name=?
        and version=?

Hibernate:
    delete
    from
        auth.policy_attributes
    where
        policy_id=?

Hibernate:
    insert
    into
        auth.policy_attributes
        (policy_id, policy_simple_value_id)
    values
       (?, ?)


The problem
As you see no modification has been made to any of the objects
involved in the java-code, but when commit() is called a lot of
inserts/updates/deletes are done.

I'm not sure if this is related to my id's bean "assigned", but as I understand
from hibernate docs all the steps have been done for an assigned id to work properly, except for implementing Interceptor.isUnsaved(), this method is not part of Interceptor and really could not find how to implement/use a similar method.

Any ideas how to correct this problem, or how to avoid it ?

Thanks very much in advance
tonio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 10:05 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
it is feature of hibernate that what ever object state has been changed in transaction scope it would try to persist in DB so try to do coding accordingly.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 10:32 am 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Thanks dharmendra,

But the problem is that object state has not been changed (see the java code), and hibernate , I don't know why, is updating DB.

I assumed it has to do with assigned ids, but perhaps there is another
cause I'm not able to detect.

Thanks anyway


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 12:40 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Plz.. let us know ,what these method are doing

_managedObjects.put

_policies.put

_values.put

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 11:56 am 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Hi,

As it says in the code comment, all _* variables are instance HashMaps used to cache objects, they has nothing to do with persistence or hibernate.

Really I'm stuck around this problem, any help will be greatly welcomed

thanks
tonio


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 12:10 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Sorry, my explanation was not really good.

All put methods called in my java code are default HashMap methods, so
no change are made to the Beans, at least by my code.

Hope it's clear.
and thanks again for your response


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 13, 2007 3:40 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Well,

I simplified a lot my code, and here are the results, the only objects
involved in this code are Policy and PolicySimpleValue.

The only code I execute between begin and end transaction is:

Code:
          Policy p = (Policy)session.get(Policy.class, "KEY");


Now all the sql hibernate executes
Code:
Hibernate:  // This is the SQL for get
    select
        policy0_.name as name24_0_,
        policy0_.version as version24_0_,
        policy0_.description as descript3_24_0_
    from
        auth.policy policy0_
    where
        policy0_.name=?

// All these are executed when commit is called

Hibernate: // Fetch all policy childs
    select
        acceptable0_.policy_id as policy1_1_,
        acceptable0_.policy_simple_value_id as policy2_1_,
        policysimp1_.name as name26_0_,
        policysimp1_.version as version26_0_,
        policysimp1_.description as descript3_26_0_,
        policysimp1_.value_type as value4_26_0_
    from
        auth.policy_attributes acceptable0_
    left outer join
        auth.policy_simple_value policysimp1_
            on acceptable0_.policy_simple_value_id=policysimp1_.name
    where
        acceptable0_.policy_id=?

Hibernate:  // Update policy object ???
    update
        auth.policy
    set
        version=?,
        description=?
    where
        name=?
        and version=?

// Delete and re-insert existing childs ?????
Hibernate:
    delete
    from
        auth.policy_attributes
    where
        policy_id=?
Hibernate:
    insert
    into
        auth.policy_attributes
        (policy_id, policy_simple_value_id)
    values
        (?, ?)
Hibernate:
    insert
    into
        auth.policy_attributes
        (policy_id, policy_simple_value_id)
    values
        (?, ?)
Hibernate:
    insert
    into
        auth.policy_attributes
        (policy_id, policy_simple_value_id)
    values
        (?, ?)


Another strange clue (at least for me), if I execute
Code:
  Policy p = (Policy)session.get(Policy.class, "KEY");
  session.evict(p)

all p childs are deleted from the database, when commit is called.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 13, 2007 4:11 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Hi,

I just posted before ending my message, sorry.

If I do the same thing with ManagedObject all works OK

[code}
ManagedObject mo=(ManagedObject)session.get(ManagedObject.class, "KEY");
[/code]

The mapping is very similar ManagedObject/Policy Policy/PolicySimpleValue, really could no find differences, hibernate is not trying to update anything in the DB.

Thanks everybody just for reading
tonio


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 13, 2007 5:04 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Solution found.

It was my java Policy class the mehod getAcceptableAttributes() was
implemented like this:

Code:
    public Set<PolicySimpleValue> getAcceptableAttributes() {
       return Collections.umodifiableSet(acceptableAttributes);
    }


Still surprise that no exception was thrown, but at least I'm up and
working

Thanks everyone
tonio


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