-->
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: One-To-One associations
PostPosted: Tue Aug 09, 2005 10:59 am 
Newbie

Joined: Tue Aug 09, 2005 10:26 am
Posts: 1
Hi,

i am new to hibernate and want to config the following ont-to-one relation (it's an extract, not the real model):

Code:
-----------------                    ----------------
| TASK          |                    | TASKRESULT   |
-----------------   1         0..1   ----------------
| id            | <----------------> | id           |
-----------------                    ----------------
| action        |                    | result       |
-----------------                    ----------------


- I like to navigate the relation from the TASK to the TASKRESULT. The reverse direction is not required.
- I like to get TASKRESULT.id assigned the same value as TASK.id.
- I like to get TASKRESULT deleted automatically when TASK is deleted.

i tried the following mapping:

Code:
-  TASK  -
==========

   <class name="Task" table="task">

      <id name="id" column="id">
         <generator class="native"/>
      </id>
      <property name="action" column="action"/>
      <one-to-one name="result" type="TaskResult" cascade="save-update,delete"/>

   </class>

- TASKRESULT -
==============

   <class name="TaskResult" table="taskresult">

      <id name="id" column="id">
         <generator class="foreign">
            <param name="property">task</param>
         </generator>
      </id>
      <one-to-one name="task" type="Task" constrained="true"/>

   </class>


Now i observe the following problems:

1.) When i try to delete a TASK, associated with a TASKRESULT, i get a integrity constraint violation, because TASKRESULT.id is a FOREIGN KEY of TASK.id. How do i tell hibernate to delete the TASKRESULT before deleting TASK?

2.) I do not want to navigate from TASKRESULT to TASK. So how do i have to config TASKRESULT to get the TASKRESULT.id assigned with the same value as TASK.id but without the need to declare a property task in the class TaskResult?

Thx for any suggestions, i am a bit clueless. I did study the documentation and the web for more than 4 hours now.

Thx and best regards,

JDon


Top
 Profile  
 
 Post subject: Re: One-To-One associations
PostPosted: Tue Aug 09, 2005 11:35 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Jdon wrote:
Hi,

i am new to hibernate and want to config the following ont-to-one relation (it's an extract, not the real model):

Code:
-----------------                    ----------------
| TASK          |                    | TASKRESULT   |
-----------------   1         0..1   ----------------
| id            | <----------------> | id           |
-----------------                    ----------------
| action        |                    | result       |
-----------------                    ----------------


- I like to navigate the relation from the TASK to the TASKRESULT. The reverse direction is not required.
- I like to get TASKRESULT.id assigned the same value as TASK.id.
- I like to get TASKRESULT deleted automatically when TASK is deleted.

i tried the following mapping:

Code:
-  TASK  -
==========

   <class name="Task" table="task">

      <id name="id" column="id">
         <generator class="native"/>
      </id>
      <property name="action" column="action"/>
      <one-to-one name="result" type="TaskResult" cascade="save-update,delete"/>

   </class>

- TASKRESULT -
==============

   <class name="TaskResult" table="taskresult">

      <id name="id" column="id">
         <generator class="foreign">
            <param name="property">task</param>
         </generator>
      </id>
      <one-to-one name="task" type="Task" constrained="true"/>

   </class>


Now i observe the following problems:

1.) When i try to delete a TASK, associated with a TASKRESULT, i get a integrity constraint violation, because TASKRESULT.id is a FOREIGN KEY of TASK.id. How do i tell hibernate to delete the TASKRESULT before deleting TASK?

2.) I do not want to navigate from TASKRESULT to TASK. So how do i have to config TASKRESULT to get the TASKRESULT.id assigned with the same value as TASK.id but without the need to declare a property task in the class TaskResult?

Thx for any suggestions, i am a bit clueless. I did study the documentation and the web for more than 4 hours now.

Thx and best regards,

JDon


Have you tried moving the constrained="true" to the Task one-to-one mapping?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 4:05 pm 
Newbie

Joined: Fri Jan 07, 2005 5:46 pm
Posts: 19
There is information in the reference document (6.1.11. one-to-one):
...
Now we must ensure that the primary keys of related rows in the PERSON and EMPLOYEE tables are equal. We use a special Hibernate identifier generation strategy called foreign.
...

and (22.3. Cascading lifecycle):
...
Now, in our case, a Child can't really exist without its parent. So if we remove a Child from the collection, we really do want it to be deleted. For this, we must use cascade="all-delete-orphan".
...


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.