-->
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: <many-to-one> simple question!
PostPosted: Fri Sep 24, 2004 4:34 am 
Newbie

Joined: Fri Sep 24, 2004 4:23 am
Posts: 5
Hibernate version: 2.1.6

---

I would like to introduce an simple problem: I have two POJOs:

public class A {
private long Code_A;
private B Test;
... gets and sets ...
}

public class B {
public long Code_B;
... gets and sets ...
}

And I want to map A using <many-to-one> of B :

<many-to-one name="Test" class="B"/>

---

Okey, then imagine the situation where the database field "Test" of the table "A" is NOT UNIQUE and CAN BE NULL. So, the A's dont really NEED an B in Test, and 10000 A's can point to the same B. In SQL we can simple use an "on delete set null" constraint.

Well. In Hibernate, if I do:
A A1 = new A();
B B1 = new B();
A1.setTest(B1);
"session".store(B1);
"session".store(A1);
"session".delete(B1);

It returns an "constraint violation error" because the A1 is using B1. But, I want to delete(B1) and set "Test" on A1 as null.

How do I do that??? How do I map that???

---> Thanx for your patience!!! :) Bernardo S. A. Silva


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 4:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
of course, all this is possible, you must read all about cascade and parent/child relationship.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Well, so I will read... :)
PostPosted: Fri Sep 24, 2004 11:15 pm 
Newbie

Joined: Fri Sep 24, 2004 4:23 am
Posts: 5
Okey,

Can you please send me the documentation references. I will read all, I promisse.

:)


Top
 Profile  
 
 Post subject: Hi Anthony!
PostPosted: Sat Sep 25, 2004 3:03 am 
Newbie

Joined: Fri Sep 24, 2004 4:23 am
Posts: 5
I am Bernardo S. A. Silva, from Brazil (bsas at hibernate forum) and I post the <many-to-one> question that you had answer. (sorry about my terrible english)
I already map that and read a lot of the "cascade" and the "parent/child" documentation, and I still can't solve my problem... :(
My real problem is: when I create a <many-to-one> relation, hibernate ALWAYS create a constraint on the database (doesn't matter the SGBD) at the "A" table that uses "on delete restrict".
But I need to create that relation with an "on delete set null" constraint.
I am really upset and tired, cause I expend more then 5hrs to solve that simple mapping problem and doesn't solve it.

Let me give you a simple example of my problem:

POJOs:

class A {
private long Code_A;
private B Relation;
.. gets and sets ...
}

class B {
private long Code_B;
... gets and sets ...
}

XMLs:

<hibernate-mapping>

<class name="A" table="A">
<id name="Code_A">
<generator class="native"/>
</id>
<many-to-one name="Relation" class="B"/>
</class>

<class name="B" table="B">
<id name="Code_B">
<generator class="native"/>
</id>
</class>

</hibernate-mapping>

MAIN:

...
A A1 = session.saveOrUpdateCopy(new A());
B B1 = session.saveOrUpdateCopy(new B());
A1.setRelation(B1);
A1 = session.saveOrUpdateCopy(A1);
session.delete(B1);
...

Exception:

Immediate foreign key constraint violation on delete
(FK7D70BC9726DD869) Columns = A(Relation) -> B(Code_B)

_____________________________________________________________________


Why??? What I did wrong??? Please help me!!!

Sorry 'cause I am boring you, really! I am desesperated!
Thanx a lot for you patience!

Bernardo S. A. Silva


Top
 Profile  
 
 Post subject: help please
PostPosted: Sun Sep 26, 2004 3:22 pm 
Newbie

Joined: Fri Sep 24, 2004 4:23 am
Posts: 5
plz, anyone can help me?


Top
 Profile  
 
 Post subject: Plz, help...
PostPosted: Mon Sep 27, 2004 7:19 pm 
Newbie

Joined: Fri Sep 24, 2004 4:23 am
Posts: 5
Help me plz... :)


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.