-->
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.  [ 6 posts ] 
Author Message
 Post subject: cascade insert?
PostPosted: Mon May 24, 2004 10:05 am 
Newbie

Joined: Mon May 24, 2004 9:36 am
Posts: 4
i`m trying to map a collection.
Code:
/**
* @hibernate.class
*/
public class BClass {
    private long id;
    private List test;
    /**
     * @hibernate.id generator-class="native"
     */
    public long getId() {
        return id;
    }
    /**
     * @hibernate.list lazy="false" cascade="all"
     * @hibernate.collection-key column="parent_id"
     * @hibernate.collection-one-to-many class="hibernate.test.AClass"
     * @hibernate.collection-index column="name"
     */
    public List getTest(){
        return test;
    }

and the test class
Code:
  AClass a = new AClass("testing");
  BClass b = new BClass();
  java.util.List s = new java.util.ArrayList(1);
  s.add(a);
  b.setTest(s);
  session.save(b);                  

the problem is that on session.save, hibernate first inserts the BClass (wich works just fine) but then tries to update the AClass (o pojo with only hibernate properties), wich shoud be also inserted and not updated (it doesn`t exist in the database).
if the hbm file are required i can provide them.
(I`m using hibernate 2.1 with xdoclet 1.2.1)
And one final thing: Is it possible to have a unique many-to-one association and have the cascade insert/update/delete? Like just say in AClass i would have
Code:
    /**
     * hibernate.many-to-one  outer-join="true" cascade="all" column="b_id" unique="true"
     */
    public BClass getB() {
            return b;
    }

and the when i do an update on a to have the asociated b class updated?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 24, 2004 10:47 am 
Senior
Senior

Joined: Fri Nov 21, 2003 5:55 am
Posts: 155
I had the same problem but now it's work with inverse="true" for BClass
and in my source code something like that

session = ServiceLocator.getInstance().getHibernateSession().openSession();
tx = session.beginTransaction();
session.save(B); //insert the parent POJO

Iterator it = BList.iterator();
while(it.hasNext()) {
session.save((A)it.next()); //insert each child POJO
}

session.flush();
tx.commit();

I don't know if it was the best solution but now it works after 1 week of errors:)


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 24, 2004 11:08 am 
Newbie

Joined: Mon May 24, 2004 9:36 am
Posts: 4
so in the end I'll have to manually insert every single object?
what need if for all that cascade markers in the hbm files. What is the diference of having cascade all / none ...... There is no way to say : this is may objects graph, now insert all of them to the db..


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 24, 2004 11:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
check your unsaved-value mapping. It's also in the FAQ


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 25, 2004 7:26 am 
Newbie

Joined: Mon May 24, 2004 9:36 am
Posts: 4
After the yesterday (and before your reply) I did a try with unsave-value="-1", but with the same results.
Anyway looking thrugh the doc i`m unable to find an example with cascade insert.
For example if I have a relation many-to-one: parent with id=-1 and children with id=-1 ,both new objects not stored in the database, children referencing the parent (children has a many-to-one relation) and i save the children, with cascade insert, wouldn`t the hibernate have to insert the parent also?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 25, 2004 11:03 am 
Newbie

Joined: Mon May 24, 2004 9:36 am
Posts: 4
scratch that. it actualy dows just that. i dont know where my mistake was. i`ve just rewriten the whole tests and works fine. sorry about that


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