-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problems on deleting objects using many-to-one
PostPosted: Wed Jan 21, 2004 8:46 pm 
Newbie

Joined: Mon Dec 08, 2003 1:44 pm
Posts: 16
Location: Germany
Heres a brief explanation of the scenario:
I like to create a Question-Catalogue, therefore I need a "Question". So I modelled a Class QUESTION. A QUESTION can have a link to another QUESTION, so a Question-Object actually might hold a reference to another Question-Object.

Here is the Java-Code:

Code:
public class Question {

    private String id;
    private String text;
    private Question linkedQuestion;

    //Constructor
    ...

    //Methods: getters and setters
    ...

}


And this is what my mapping does look like:

Code:
<class name="Question"
    table="QUESTIONS"
   
    <id name="id" column="ID">
        <generator class="uuid.string" />
    </id>
    <property name="text" column="TEXT" />
    <many-to-one name="linkedQuestion" column="LINKED_QUESTION_ID"
        class="Question"
        cascade="save-update" />
</class>


Now I do the following:

Code:
Question q = new Question();
q.setText("I am doing something wrong?");
Question linkedQ = new (Question);
linkedQ.setText("Do I need help?");

q.setLinkedQuestion(linkedQ);


So far, so good. When I save "q", "linkedQ" is also persistet and the table QUESTIONS has two new rows, so everything seems to work quite nice.

But when I want to delete a question it only works if I first delete "q" and then "linkedQ". If I try to delete "linkedQ", an SQLException is thrown, saying I am violating an integrity constraint.

I somehow have the feeling, that the answer is way simple... but although I read the manual and searched the forum more than a dozen times, I haven't found a solution yet :-(

Best thanks in advance for any answers

Philipp Hinnah


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 5:11 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You linked Q -> linkedQ
When deleting LinkedQ, Hibernate cannot (efficiently) know there is a link (FK constraint) with Q.
To allow this, do a bidirectional relationship with appropriate cascade.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 6:31 am 
Newbie

Joined: Mon Dec 08, 2003 1:44 pm
Posts: 16
Location: Germany
Thanks!

I will try.

Philipp Hinnah


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