-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem saving new object with set of new objects
PostPosted: Wed Sep 21, 2005 10:34 am 
Newbie

Joined: Tue May 31, 2005 8:57 am
Posts: 10
Hibernate version: 3.1 beta1 (Annotations 3.1 beta4)

I have a very simple problem and am quite sure I'm doing something wrong here. I have the following case (leaving out some unnecessary code):

Code:
@Entity
class A {
   @OneToMany(mappedBy="a", cascade = {CascadeType.ALL})
   public Set<B> getBs() {...};
}

@Entity
class B {
   @ManyToOne(optional=false, fetch=FetchType.EAGER)
   public A getA() {...};
}


In other words, a bi-directional one-to-many relation that cascades all operations.

Now I attempt to do the following:
Code:
A a = new A();
a.addB(new B());
session.save(a);


All very simple. But this fails. Due to the CascadeType.ALL, an attempt is made to save the B object related to A. However, since there is no A yet, if B is saved, the foreign key constraint on A can not be satisfied. This constraint exists because of the optional=false attribute on the getA() method.

So for saving this, I first need to save the B object, then add it to A and then save A.

Is it impossible to do it the way I want, or am I missing something?


Top
 Profile  
 
 Post subject: Re: Problem saving new object with set of new objects
PostPosted: Wed Sep 21, 2005 10:40 am 
Newbie

Joined: Tue May 31, 2005 8:57 am
Posts: 10
rwwilden wrote:
So for saving this, I first need to save the B object, then add it to A and then save A.


Small correction: for saving this, I need to create an A object, save it, add the new B object and save A again.


Top
 Profile  
 
 Post subject: solution?
PostPosted: Fri Sep 23, 2005 12:54 pm 
Newbie

Joined: Mon Nov 08, 2004 11:17 am
Posts: 3
Location: Buenos Aires, Argentina
Maybe this can help you.
I had the same problem, and I just perform the save inside a transaction, as follows:

Code:
A a = new A();
a.addB(new B());

Transaction tx = session.beginTransaction();
session.save(a);
tx.commit();


This works, though I don't know why... :p


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 3:29 pm 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
Maybe you're not supplying the Join column between A and B:

Code:
@Entity
class B {
    @ManyToOne
    @JoinColumn(name = "XXX")
    public A getA() { ...};
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:11 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you have to do b.setA(a); mappedBy is equivalent to inverse=true

_________________
Emmanuel


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