-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to get correct insert order with classes in "triang
PostPosted: Fri Nov 28, 2003 4:57 am 
Beginner
Beginner

Joined: Wed Nov 26, 2003 11:53 am
Posts: 26
Location: Netherlands
Hello,

I'm trying to get the following structure to save correctly in the database:

Subscription 1<--->* Book 1<----* Authorisation

and

Subscription 1<--->* Authorisation

which is in effect a triangle. Subscription is the "root" so it uses cascade="all" for both the Book and Authorisation relations.

The problem is that Authorisation holds a reference to Book: when I saved a triplet, I got a not-null error on the insert of Authorisation: Book wasn't inserted yet so it did not have a primary key which meant that the foreign key was null as well. Book has to be inserted before Authorisation. So I added a cascade="save-update" on the Book.authorisation property. Now I get a duplicate key error because apparently the cascade from Subscription and Authorisation both try to insert the Book. How do I get around this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 8:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Thzre is probably something wrong with your unsaved-value in Book. Hard to say wo your mapping

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Here are the mapping files
PostPosted: Mon Dec 01, 2003 5:13 am 
Beginner
Beginner

Joined: Wed Nov 26, 2003 11:53 am
Posts: 26
Location: Netherlands
OK, here they are:

// ------------------------------------------- SUBSCRIPTION

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="nl.sogeti.piloot.server.admin.model.Subscription"
table="Subscription"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="long"
unsaved-value="0"
>
<generator class="identity">
</generator>
</id>

<version name="version" type="long" column="version"/>

<property name="number" type="long" update="true" insert="true" column="number"/>
<property name="address" type="java.lang.String" update="true" insert="true" column="address"/>
<property name="city" type="java.lang.String" update="true" insert="true" column="city"/>
<property name="endDate" type="java.util.Date" update="true" insert="true" column="endDate"/>
<property name="name" type="java.lang.String" update="true" insert="true" column="name"/>
<property name="standalone" type="boolean" update="true" insert="true" column="standalone"/>
<property name="startDate" type="java.util.Date" update="true" insert="true" column="startDate"/>
<property name="zipCode" type="java.lang.String" update="true"insert="true" column="zipCode"/>

<bag
name="authorisations"
lazy="false"
inverse="true"
cascade="all"
>

<key
column="id"
/>

<one-to-many
class="nl.sogeti.piloot.server.admin.model.Authorisation"
/>
</bag>

<bag
name="books"
lazy="false"
inverse="true"
cascade="all"
>

<key
column="id"
/>

<one-to-many
class="nl.sogeti.piloot.server.admin.model.Book"
/>
</bag>

<property
name="maxNrIButtons"
type="int"
update="true"
insert="true"
column="numberOfIButtons"
/>

<property
name="lastversion"
type="long"
update="true"
insert="true"
column="lastversion"
/>

<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="description"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Subscription.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


// ----------------------------------- BOOK

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="nl.sogeti.piloot.server.admin.model.Book"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="long"
unsaved-value="0"
>
<generator class="identity">
</generator>
</id>

<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="name"
/>

<many-to-one
name="subscription"
class="nl.sogeti.piloot.server.admin.model.Subscription"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="fk_subscription"
not-null="true"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Book.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>

// ----------------------- AUTHORIZATION

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="nl.sogeti.piloot.server.admin.model.Authorisation"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="long"
unsaved-value="0"
>
<generator class="identity">
</generator>
</id>

<property
name="IButton"
type="java.lang.String"
update="true"
insert="true"
column="ibutton"
/>

<many-to-one
name="subscription"
class="nl.sogeti.piloot.server.admin.model.Subscription"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="fk_subscription"
not-null="true"
/>

<many-to-one
name="book"
class="nl.sogeti.piloot.server.admin.model.Book"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="fk_book"
not-null="true"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Authorisation.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 10:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I Assume that you use primitive type for ids.

You don't have one-to-many in Book to Authorization (but this is not the pb)

In the bag / one-to-many
I think you map key to the wrong column. This must be mapped in the fk column of the other side table (eg a subscription_fk in Authorization). The same column must be mapped in the many-to-one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 10:54 am 
Beginner
Beginner

Joined: Wed Nov 26, 2003 11:53 am
Posts: 26
Location: Netherlands
Just an update to help other ppl. What I needed to do was the same as what was said in posting http://forum.hibernate.org/viewtopic.ph ... highlight=

No unidirectional relationships with NOT NULL foreign key columns...


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