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