-->
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.  [ 2 posts ] 
Author Message
 Post subject: one-to-one problems
PostPosted: Mon Mar 15, 2004 5:02 pm 
Newbie

Joined: Mon Mar 15, 2004 4:59 pm
Posts: 3
Hello all,

Using Hibernate version 2.1.4, Postgres 7.3.4.

I have two hibernate objects:

First, Channel which has a native generator for its ID (named channelId) and a one-to-one relationship with Vouch, cascade all. Vouch has a foreign generator for its id (named vouchId) with generator-param property set to channel. Within Vouch I have a one-to-one relationship with Channel, cascade save-update.

Vouch.hbm.xml:
Code:
<id
            name="vouchId"
            column="vouchId"
            type="java.lang.Long"
        >
            <generator class="foreign">
                <param name="property">channel</param>
            </generator>
        </id>

        <one-to-one
            name="channel"
            class="Channel"
            cascade="save-update"
            outer-join="auto"
            constrained="true"
        />

Channel.hbm.xml:
Code:
<id
            name="channelId"
            column="channelId"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <one-to-one
            name="vouch"
            class="Vouch"
            cascade="all"
            outer-join="auto"
            constrained="false"
        />



Here is the test I am running:
Code:
Channel channel = new Channel();
channel = createChannel(channel);
   
Vouch vouch = new Vouch();
vouch.setChannel(channel);
vouch = createVouch(vouch);
   
deleteChannel(channel);


The deleteChannel fails b/c of a referential integrity error (the vouch never gets deleted first).

methods that are called (open and closing sesion removed within each method, also error catching removed):

Code:
public Channel createChannel(Channel channel) {
      session.save(channel);
      session.flush();
      session.refresh(channel);
      return channel;
}

public void deleteChannel(Channel channel) {
      session.delete(channel);
      session.flush();
}
   


The createVouch method is functionally identical to createChannel.

I tried using constrained, but that didn't work. What is actually happening is the channel's vouch reference isn't getting set when I do a createVouch with the vouch's channel reference set appropriately (vouch.channel ok, but channel.vouch null, vouch.getChannel.vouch is also null).

It doesn't appear to be setting up the reverse relationship appropriately. When I try to do a deleteChannel on the channel, it will orphan the vouch. If constrained is set and I try to deleteChannel, I get a referential integrity violation since it isn't deleting the vouch before the channel (no reference is set in channel to the appropriate vouch).

Any help would be greatly appreciated!

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 10:07 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hibernate does not manage association in you java graph. It is your responsability to setOnetoOne both side when updating an already loaded object.

_________________
Emmanuel


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