-->
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.  [ 1 post ] 
Author Message
 Post subject: Transientes Parent-Objekt und Children-Collection
PostPosted: Mon Oct 24, 2005 10:55 am 
Beginner
Beginner

Joined: Mon Oct 24, 2005 9:46 am
Posts: 22
Location: Germany
Ich bin dabei mich in Hibernate einzuarbeiten und nachdem ich die Referenzdoku und "Hibernate quickly" durchgewälzt habe, bleibt mir folgendes (evtl. auch relativ triviales) doch noch leicht verschlossen:


Hibernate version: 3.0.5

Mapping documents:
Code:
<class name="Parent" table="PARENT">
    <id name="id" column="ID" type="java.lang.Integer">
        <generator class="native"/>
    </id>
   
    <many-to-one name="address" column="ID_ADDRESS" class="Address" not-null="true" cascade="all-delete-orphan"/>
   
    <set name="childrenSet" cascade="all,delete-orphan" inverse="true">
        <key column="ID_PARENT"/>
        <one-to-many class="Children"/>
    </set>
</class>

<class name="Address" table="ADDRESS">
    <id name="id" column="ID" type="java.lang.Integer">
        <generator class="native"/>
    </id>
    <set name="parentSet" inverse="true">
        <key column="ID_ADDRESS"/>
        <one-to-many class="Parent"/>
    </set>
</class>

<class name="Children" table="CHILDREN">
    <id name="id" column="ID" type="java.lang.Integer">
        <generator class="native"/>
    </id>
    <many-to-one name="parent" column="ID_PARENT" class="Parent"  not-null="true" />
</class>



Code between sessionFactory.openSession() and session.close():
Code:
Parent p = new Parent();
Address a = new Address();

p.setAddress(a);

//session.saveOrUpdate(p);   //siehe text

Child c = new Child();
c.setParent(p);

p.getChildrenSet().add(c);

session.saveOrUpdate(p);


-----------------------------------------------------
(Vereinfachte Darstellung der Mappings/des Codes).

Folgendes passiert:

- Parent wird in die DB inserted.
- auch die Address wird in die DB inserted
- mit dem Children-Set passiert allerdings auf diese Weise gar nichts
(Cascades.cascadeCollection: auf "cascade ACTION_SAVE_UPDATE..." folgt direkt wieder "done cascade ACTION_SAVE_UPDATE...")


Wenn ich vor dem Hinzufügen des Childs zum ChildrenSet des Parents eben jenes explizit speichere(sprich: die Zeile des Codebeispiels einkommentiere), klappt alles wunderbar und das Child wird in der DB gespeichert.

Die Frage ist nun:
Was habe ich wo falsch gemacht oder muss ich wirklich erst das Parent-Objekt persistent machen, bevor ich mit der Childrencollection hantiere?


gruss,
Torsten


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.