-->
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: Problem with saving entites with non optional associations
PostPosted: Tue May 29, 2007 10:19 am 
Newbie

Joined: Fri Dec 29, 2006 12:20 pm
Posts: 8
Hi all

I'm using Hibernate 3.02 with annotations. I'm trying to map an association between two entities. The Problem arises when both sides of the association are mandatory. So each entity needs a constraint not to let the other side to be null.

So I cannot save either of the entities, cause each entity needs the otherone.

Help please.

_________________
--
REZA++


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 11:34 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Hibernate Validator will check if a field is null and throw an exception if it is.

But, I think hibernate will do that automatically (without Hibernate Validator) by setting not-null="true" on that field.



orr, you could do it manually like this

Code:
ClassA {

private ClassB classb;

public void setClassb(ClassB classb) {
this.classb = classb;
if classb.getClassa == null)
throw Exception

}

}



or write your own Listener to check the classes before save and update

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: Thanks, But I did not get my answer
PostPosted: Tue May 29, 2007 12:22 pm 
Newbie

Joined: Fri Dec 29, 2006 12:20 pm
Posts: 8
Thanks for help, but that was not actually the problem. I'm using MySQL database and should tell the database not to check the foreign key constraints for saving objects just for some time.

suppose I have A and B. B should have an A and A also should have a B. Suppose having this code:

Code:
sf.getCurrentSession().beginTransaction();
A a = new A();
B b = new B();
a.setB(b);
b.setA(a);

sf.getCurrentSession().save(a);
sf.getCurrentSession().save(b);

sf.getCurrentSession().commit();


This code will throw exception, because saving a will need a valid b, but b itself needs an a to be saved. Database will not allow me to save a without any b. Because of foreign key constraints.

This loop will make the problem.
I think this is not hibernate's problem, I should tell the database to do this, but by hibernate's help.

any idea?

_________________
--
REZA++


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 12:32 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Set up your mapping files correctly then.

Hibernate can insert both of those classes into their tables and use any auto-generated values from the database in any foreign key fields with only one call to save.

If your database has must exist on both ends then you should probably fix that first.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: But How?
PostPosted: Tue May 29, 2007 12:40 pm 
Newbie

Joined: Fri Dec 29, 2006 12:20 pm
Posts: 8
I have a constraint of type "must exist" on both ends and that is my problem. I want mysql to forget the foreign key constraints just for a moment and let me save my objects.

How can I do that? You mean Hibernate will automatically do this for me and there is no need to tell anything to my MySQL?

_________________
--
REZA++


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 12:47 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
you should have it mapped that:

Code:

<class name="classA"
>

...

<name="many-to-one"
class="com.package.ClassB"
not-null="true"
>
<column name ="foreignkey" />

</many-to-one>

</class>


on both ends. (or one-to-one) And if they reference auto-generated values hibernate will pick those up and use them for the inserts.

if one-to-one there is even a foreign-key attribute you can set

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: OK
PostPosted: Tue May 29, 2007 1:19 pm 
Newbie

Joined: Fri Dec 29, 2006 12:20 pm
Posts: 8
I made making a mistake. Thanks again. It's correct now.

_________________
--
REZA++


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.