-->
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.  [ 3 posts ] 
Author Message
 Post subject: Extran Update before Delete and after Insert
PostPosted: Sat Feb 10, 2007 2:47 am 
Newbie

Joined: Tue Feb 06, 2007 11:42 pm
Posts: 6
I have pretty simple model with User object having a map of IdentityAttributes. (see mapping below).

When I create a User and add an IdentityAttribute then save User.

I first see Insert for IdentityAttribute and then when transaction commited Update for the same IdentityAttribute. Update actually "establishes" relationship with User (updates foreign key for User in IdentityAttribute table).

When I delete an IdentityAttribute from map in User. I first see update which "breaks relationship" and then delete for Identity Attribute ( I have cascade="delete-orphan").

Is it normal ?

Thanks a lot for help, Alexi

Details.

1. Creation code
Code:
       Session sess = startTxAndSess();
       
        User user = new User("testUser");
       
        String attr1Name = "TestAttrName";
        IdentityAttribute attr = new IdentityAttribute(attr1Name);
        user.addAttribute(attr);
       
        System.out.println("About to save identity");
       
        sess.saveOrUpdate(user);
       
        Long userId = user.getId();
       
        System.out.println("About to commit");
       
       
        commitTx();



Debug log which shows extra update. Note that insert into IdentityAttribute does not insert foreign key column identityId. Update does that.

Code:
StartTX:org.springframework.transaction.support.DefaultTransactionStatus@48f675 Sess:2920707
About to save identity
2007-02-10 01:30:57,921 DEBUG [org.hibernate.SQL] - <insert into Identity (id, name, parentId, email, identityType) values (null, ?, ?, ?, 'USER')>
2007-02-10 01:30:57,937 DEBUG [org.hibernate.SQL] - <call identity()>
2007-02-10 01:30:57,953 DEBUG [org.hibernate.SQL] - <insert into IdentityAttributeName (id, name) values (null, ?)>
2007-02-10 01:30:57,953 DEBUG [org.hibernate.SQL] - <call identity()>
About to commit

2007-02-10 01:30:57,968 DEBUG [org.hibernate.SQL] - <update IdentityAttributeName set identityId=?, name=? where id=?>
CommitTX:org.springframework.transaction.support.DefaultTransactionStatus@48f675



2. Delete code

Code:
        sess = startTxAndSess();
       
        User u = (User) sess.load(User.class, userId);
       
        System.out.println("Found user");
       
        Map attrMap = u.getAttributeMap();
       
        System.out.println("About to remove");
       
        attrMap.remove(attr1Name);
       
        commitTx();


Debug output again shows first update to IdentityAttribute to set identityId =null to "break" connection to User. Then delete

Code:
   StartTX:org.springframework.transaction.support.DefaultTransactionStatus@9fe84e Sess:5009874
Found user
2007-02-10 01:38:07,421 DEBUG [org.hibernate.SQL] - <select user0_.id as id7_0_, user0_.name as name7_0_, user0_.parentId as parentId7_0_, user0_.email as email7_0_ from Identity user0_ where user0_.id=? and user0_.identityType='USER'>
About to remove
2007-02-10 01:38:07,421 DEBUG [org.hibernate.SQL] - <select attributem0_.identityId as identityId1_, attributem0_.id as id1_, attributem0_.name as name1_, attributem0_.id as id8_0_, attributem0_.name as name8_0_ from IdentityAttributeName attributem0_ where attributem0_.identityId=?>
2007-02-10 01:38:07,500 DEBUG [org.hibernate.SQL] - <delete from IdentityAttributeValue where identityAttributeNameId=?>
2007-02-10 01:38:07,500 DEBUG [org.hibernate.SQL] - <update IdentityAttributeName set identityId=null, name=null where identityId=?>
2007-02-10 01:38:07,500 DEBUG [org.hibernate.SQL] - <delete from IdentityAttributeName where id=?>
CommitTX:org.springframework.transaction.support.DefaultTransactionStatus@9fe84e



Don't know if it is related to the problem, but Select from IdentityAttribute above has all fields repeated twice in Select clause. Why is that ? Is it a problem

Mapping

Code:
  <class name="Identity" table="Identity" discriminator-value="IDENTITY">
       
        <id name="id"
            column="id">
            <generator class="native"/>
        </id>
       
        <discriminator  column="identityType"  type = "string" />
       
        <property name="name" column="name" />
       
        <map name="attributeMap" cascade="save-update,delete, delete-orphan" lazy="true" >
            <key column="identityId" />
            <map-key type="java.lang.String" column="name"  />
            <one-to-many  class="IdentityAttribute" />
        </map>
       
        <many-to-one
            name="parent"
            column="parentId"
            class="Identity"
        />   
       
        <subclass   name="User"  discriminator-value="USER">
            <property name="email" column="email"/>
        </subclass>
       
        <subclass   name="Organization"  discriminator-value="ORG">
       
        </subclass>
       
    </class>
   
    <class name="IdentityAttribute" table="IdentityAttributeName">
        <id name="id" column="id">
            <generator class="native"/>
        </id>     
       
        <property name="name" column="name" />
       
        <set name="values" table="IdentityAttributeValue" order-by="value" cascade="all">
            <key column = "identityAttributeNameId" />
            <element type="java.lang.String" column="value"/>
        </set>
       
    </class>
   


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 12:36 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi apolenur,


use inverse =true to solve your problem .I would recommend to use bidirectional mapping.

other wise you should have foreign key in DB and use insert false update false in for that column in child table

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 12:09 pm 
Newbie

Joined: Tue Feb 06, 2007 11:42 pm
Posts: 6
Hi Dharmendra,

Thanks a lot for your reply. Could you please clarify your suggestion a little more, because I still don't understand why I am having this issue.

You are suggesting to use inverse. My understanding that inverse only used with biderectional associations. So are you suggestion that I should add a many-to-one relationship from IdentityAttribute back to Identity and mark it inverse, right ? I am learning Hibernate and would really like to understand why I have to do it.

Regarding alternative solution. You are suggestion explicetly putting mapping for identityId property into IdentityAttribute mapping and marking this field as insert and update false, because this field of AttributeName table is a foregign key for Identity table, right ? If I do it, how then I going to be able to establish association between IdentityAttribute and Identity, since this is require modifing identityId field in IdentityName table. I can see that I can use foreign key constrain in DB to do cascade delete.

Sorry for to many questions, I just want not only solve the problem, but understand what the issue is, so I learn something and don't repeat my mistakes again.

Thanks again, Alexi


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