-->
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 relationship problems
PostPosted: Tue Feb 22, 2011 9:03 am 
Newbie

Joined: Thu Feb 17, 2011 5:08 pm
Posts: 6
Hello people... I have some problems with a one-to-one relationship...

I need map tables for to do cascade actions... More exactly when I set null the reference attribute Hibernate remove data from the reference table....

a simplified POJO is..

Code:
public class Persona{

public int id;
public String nombre;
public Cliente cliente;

gets and sets methods

}

public class Cliente{

public int id;
public String descripcion;
public Persona persona;

gets and sets methods

}

the behaviour that i want is:

On inserts... (new objects to persist)

When I set a cliente in persona (persona.setCliente(cliente)) Hibernates insert a row in Clientes table... is i don't set cliente... Hibernate inserts nothing..

On Deletes

Hiberte deletes the cliente asociated to persona... In other word when I remove a persona hibernate removes the cliente asociated in cascade... (if cliente exists)

On Edit an existent persona...

If I edit a existent persona and I Change cliente's attribute descripcion (persona.getCliente().setDescripcion("hello");) i want that Hibernate persist this change on my database... (sql update sentence)

but... when I set null the cliente on an existent persona (persona.setCliente(null);) I WANT HIBERNATE REMOVE THE CLIENTE OF MY DATABASE. HERE IS MY PROBLEM!!!!!!!!!!!

can you help me please??

My database schema is

Code:
Table Persona
int id (PK)
vachar nombre;
cliente_id (FK table cliente)

Table Clientes
int id (PK)
varchar descripcion
persona_id (FK table personas)



Thanks in advance

Regards

Nicolas


Top
 Profile  
 
 Post subject: Re: one-to-one relationship problems
PostPosted: Tue Feb 22, 2011 9:39 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

You need to add the cascade-style "delete-orphan" to your one-to-one mapping.
But this functionnality was implemented very recently by the hibernate team and only exists since version 3.5.0


Top
 Profile  
 
 Post subject: Re: one-to-one relationship problems
PostPosted: Sun Feb 27, 2011 1:31 pm 
Newbie

Joined: Thu Feb 17, 2011 5:08 pm
Posts: 6
Hi!!

thanks for ask!!

I tried this.. delete-orphan works!! but I found new some problems...

When I edit a Persona Hibernate tries to insert a new Cliente causing PK constraint violation...

How I can do for Hibernate update the current Cliente and don't try to insert a new Cliente??

part of mapping code is
in Persona.hbm
Code:

<class name="model.Persona" table="persona" schema="public">
        <id name="id" type="long">
            <column name="id" />
            <generator class="sequence">
                <param name="sequence">persona_id_seq</param>
            </generator>
        </id>


in Cliente.hbm
Code:
<class name="model.Cliente" table="cliente" schema="public">
        <id name="id" type="long">
            <column name="id" />
            <generator class="foreign">
                <param name="property">persona</param>
            </generator>

        </id>
         <one-to-one name="persona" constrained="true" />


the behaviour that i want is:

On inserts... (new objects to persist)

When I set a cliente in persona (persona.setCliente(cliente)) Hibernates insert a row in Clientes table... is i don't set cliente... Hibernate inserts nothing..

On Deletes

Hiberte deletes the cliente asociated to persona... In other word when I remove a persona hibernate removes the cliente asociated in cascade... (if cliente exists)

On Edit an existent persona...

If I edit a existent persona and I Change cliente's attribute descripcion (persona.getCliente().setDescripcion("hello");) i want that Hibernate persist this change on my database... (sql update sentence)

but... when I set null the cliente on an existent persona (persona.setCliente(null);) I WANT HIBERNATE REMOVE THE CLIENTE OF MY DATABASE.


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.