• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving Detached Object

 
Gopesh Desai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a parent-child relationship between ClientClass and ClientClassName with following declaration

ClientClass.java
<set name="clientClassNames" inverse="true" cascade="all-delete-orphan">
<key column="CLSSID"/>
<one-to-many class="com.hli.prism.classes.domain.ClientClassName"/>
</set>

ClientClassName.java
<many-to-one name="clientClass"
class="com.hli.prism.classes.domain.ClientClass"
column="CLSSID"
not-null="true"/>

Problem Faced
In my web layer, I fetch the data and display it on the screen. User unselects some of the ClientClassName rows and select some new ClientClassName rows. While saving, hibernate creates inserts rows for the new ClientClassName but does not delete the unselected ClientClassName dereferenced from the collection.

I am using session.saveOrUpdate(clientClass);
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just my two cents:
does your code remove the unselected items from your Set clientClassNames?
 
Gopesh Desai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it does not.
 
Gopesh Desai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry.I meant - Yes, it does.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If ClientClassName has a reference to its parent ClientClass, do you set it to null when its removed from the Set? I don't know that this is necessary, but I believe I'm doing that in my code. I'll verify that assumption when I next have access to my work machine.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gopesh,
i'm developing a home app (i'm no pro at all).
I've this class Entidade that has a Set of Contrato
I wrote a test and all went fine (contrato was indeed deleted from database)
code in Entidade:

ContratoConvencao inherits from Contrato and has a reference to Entidade

hbms:


the test:



please let me know if this helps
 
Gopesh Desai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I tried the following code

Lets take following example to understand this better
Database
1) I have 1 row in database for ClientClass with id = 1
2) I have 4 rows in database for ClientClassName with id 1,2,3 and 4 associated with ClientClass id 1.

In my detached ClientClass object, I have done following
1) Set Id as 1
2) Created a collection object containing ClientClassNames with Id 1,2, 4and 5.

Now in my data access layer I do the following
1) session.saveOrUpdate(clientClass); - This statement execute an update query for ClientClass, update queries for ClientClassName with id 1,2, and 4, and insert query for ClientClassName with id 5. NO DELETE QUERY for ClientClassName with Id 3.

My question is - What do I need to do to delete ClientClassName with Id 3 from the database. If I have to set ClientClassName.setClientClass(null) then where I be do'in that and How should I be passing it to DAO. Any code snippet will be be appreciated.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you never mention you removed ClientClassName_id3, like:
father.remove(child#3);

if you take a look at my test class, you'll see that only after i remove child from Set, i update father, and then child is deleted from database!

give it a try and let me know
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things.

Miguel, note that your test doesn't use detached objects. You need to close and open a new session before saving.
  • Load parent
  • Close Session
  • Remove child
  • Open Session
  • saveOrUpdate(parent)

  • Originally posted by Gopesh
    In my detached ClientClass object, I have done following
    1) Set Id as 1
    2) Created a collection object containing ClientClassNames with Id 1,2, 4and 5.
    Are you building a whole new object graph, or are you modifying the detached object? If that latter, you shouldn't need to set its ID (it already has one), nor create a Collection with CCNs. Instead, remove CCN #3 and set its CN to null. You can add a CN.removeCCN(CCN) method to do both operations as a unit.

    Then you can saveOrUpdate() the modified CN. Just to validate the basics, does CCN override both equals() and hashCode()? It must as part of the Set contract with Hibernate or you'll get strange behavior.
     
    miguel lisboa
    Ranch Hand
    Posts: 1281
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    David, thanks for pointing that out:


    TiA

    edit:
    i guess i just dont need to close open sessions as in this last case

    [ April 17, 2005: Message edited by: miguel lisboa ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic