-->
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.  [ 7 posts ] 
Author Message
 Post subject: one-to-one -missing update in one case- misunderstanding?
PostPosted: Wed Nov 03, 2004 10:40 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
Hibernate version: 2.1.6

Hi,

i know, that i should show my mapping-documents etc. but i first want to ask if i have a mistake in thinking ... could be ;)

I've got a one-to-one-relationship (cascading=all;outer-join=true) between ClassA and ClassB (using primary-key - composite-ids on both side and version-columns).
Everything works fine ... insert, update and delete works perfectly.

Only one thing i wasn't able to get running ...

1) Load ClassA from Database (incl. ClassB)
2) Set reference to ClassB to 'null' (classA.setClassB(null))
3) saveOrUpdate(classA)

When i set classA.setClassB(null) the entry for ClassB in Database isn't deleted ... it doesn't matter if it's the same session or a new one ...

First of all it would be a great help to know if this is possible with hibernate? Or should i use a "many-to-one"-relationship in those cases?

thx!
curio


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 10:58 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
try
Code:
cascade=all-delete-orphan


if you have a bidirectionl association you have to add
Code:
classB.setClassA(null)


HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 11:33 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
hi ernst,

thanx for the hints ...
Code:
cascade=all-delete-orphan
is not allowed by dtd, i've tried this.
I'm using: http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd

in both cases (bidirectional) association or not, there's no update to ClassA and no delete to ClassB.

The strange thing everything works fine ... only deleting the entry for ClassB from DB doesn't work ...

But i think that it must be possible ... otherwise "outer-join=true" wouldn't make sense ... well, perhaps i'll have to build a small example and add it here ... perhaps i really have some small things wrong (but what if everything else works? ) :)

thx!
curio


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 3:41 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Hi curio

I'm sorry for posting rubish. I should have checked the DTD first myself :-(.

From the DTD I guess that a parent/child relation with all-delete-orphan is supported only for Collections.
Can one of the experts confirm this?

As a work around you can
- use a list/bag/set mapping
- or check in the onFlushDirty method of an Interceptor whether there was a B and delete it.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 6:02 pm 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
Hi ernst,

no problem! i've thought that "all-delete-orphan" might work, too, because i've read a comment from gavin somewhere in this forum that this might be possible ...

hmm ... using an interceptor is a possibility, too ... yes ... never thought about this ... but sure ... i must try this ...

using a list was one of the possibilities i've thought about, too ... but at the moment i don't like it, because it's then possible to add more than only one instance of classB .... ok. i could hide the list and only support setting one element ...

i'll try both possibilites tomorrow when i'm back at work ...

thx for your help!
curio

P.S.: But if it's not possible to set a one-to-one-association to "null", i misinterpretated this kind of association the whole time ... i think i will debug a little bit tomorrow :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 04, 2004 3:56 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Quote:
P.S.: But if it's not possible to set a one-to-one-association to "null", i misinterpretated this kind of association the whole time ...

I'd expect that the foreign key will be set to NULL.

Hapy debugging
Ernst


Top
 Profile  
 
 Post subject: possible bug?
PostPosted: Fri Nov 05, 2004 9:34 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
i think i've got it right ... and found a bug ... but i can be wrong ...

Mapping files:
Code:
<hibernate-mapping>
   <class
    name="ClassA"
    table="testClassA">
   
    <id name="idClassA"
        column="id"
        type="integer"
        unsaved-value="0">
      <generator class="increment"/>
    </id>
   
    <property
       name="dummy"
       column="dummy"
       type="string"/>
   
    <one-to-one
      name="classBRef"
      class="ClassB"
      outer-join="true"
      cascade="all"/>
  </class>
</hibernate-mapping>

<hibernate-mapping>
   <class
    name="ClassB"
    table="testClassB">
   
    <id name="idClassB"
        column="id"
        type="integer"
        unsaved-value="0">
      <generator class="foreign">
         <param name="property">classARef</param>
      </generator>
    </id>
   
    <property
       name="dummy"
       column="dummy"
       type="string"/>
   
    <one-to-one
      name="classARef"
      class="ClassA"
      outer-join="true"
      constrained="true"/>
  </class>
</hibernate-mapping>

Two Classes with a one-to-one-Relationship ...
The code i use (quick and dirty):
Code:
    Session s = super.createSession();
    Transaction t = s.beginTransaction();
    ClassA classA = (ClassA)s.load(ClassA.class, new Integer(1));
    boolean aHasB = classA.getClassBRef() != null;
    boolean bHasA = classA.getClassBRef().getClassARef() != null;
    System.err.println("--------[classA has classB?]--->"+aHasB);
    System.err.println("--------[classB has classA?]--->"+bHasA);

    // remove classB-Ref
    classA.getClassBRef().setClassARef(null);
    classA.setClassBRef(null);
   
    s.saveOrUpdate(classA);
    t.commit();
   
    // reload classA from first-level-cache!
    classA = (ClassA)s.load(ClassA.class, new Integer(1));

    aHasB = classA.getClassBRef() != null;
    if (aHasB)
      bHasA = classA.getClassBRef().getClassARef() != null;
    else
      bHasA = false;
    System.err.println("--------[classA has classB?]--->"+aHasB);
    System.err.println("--------[classB has classA?]--->"+bHasA);
       
    s.close();

And the log:
Code:
1828 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - initializing non-lazy collections
--------[classA has classB?]--->true
--------[classB has classA?]--->true
1828 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - saveOrUpdate() persistent instance
1828 [main] DEBUG net.sf.hibernate.transaction.JDBCTransaction  - commit
1828 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - flushing session
1828 [main] DEBUG net.sf.hibernate.engine.Cascades  - processing cascades for: ClassA
1828 [main] DEBUG net.sf.hibernate.engine.Cascades  - done processing cascades for: ClassA
1828 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - Flushing entities and processing referenced collections
1828 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - Processing unreferenced collections
1828 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - Scheduling collection removes/(re)creates/updates
1828 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
1828 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
1843 [main] DEBUG net.sf.hibernate.impl.Printer  - listing entities:
1843 [main] DEBUG net.sf.hibernate.impl.Printer  - ClassB{classARef=null, dummy=testDummyB, idClassB=1}
1843 [main] DEBUG net.sf.hibernate.impl.Printer  - ClassA{idClassA=1, dummy=testDummyA, classBRef=null}
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - executing flush
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - post flush
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - transaction completion
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - loading [ClassA#1]
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - attempting to resolve [ClassA#1]
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - resolved object in session cache [ClassA#1]
--------[classA has classB?]--->false
--------[classB has classA?]--->false
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - closing session
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - disconnecting session
1843 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider  - returning connection to pool, pool size: 1
1843 [main] DEBUG net.sf.hibernate.impl.SessionImpl  - transaction completion


Nothing will be deleted ...

BUT ... like you can se in the log, the reference is deleted at object side, and even in the first-level-cache of the session ...

This can be a bug ... or? What are the hibernate-experts saying?
I'm using the current cvs_snapshot ...

thx!
curio


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