-->
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.  [ 6 posts ] 
Author Message
 Post subject: composite-id with a foreign key to parent
PostPosted: Tue Aug 11, 2009 4:23 am 
Newbie

Joined: Sun Aug 09, 2009 1:15 pm
Posts: 3
hello Hibernate users!
I'm new to hibernate, and I'm hoping this forum can be as helpful to me as it was for others.

I'm in the design stage, and would like to know if my design is applicable.

This is what I want to achieve:
Relationship of grandpa, parent, child.
parent has a foreign key constraint on a property (not part of its primary key) to grandpa's PK.
child has a FK constraint on a property, which IS part of its composite-id, to parent's PK.

The application will do something like:
parent.addChild( new Child() );
grandpa.addParent( parent );


so, doable?
or do I need to get back to the drawing board?

thanks for any help.

_________________
thanks for any help.


Top
 Profile  
 
 Post subject: Re: composite-id with a foreign key to parent
PostPosted: Tue Aug 11, 2009 8:26 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I have a little tutorial that does almost the same mapping, demonstrating various forms of inheritance mapping using Hibernate:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=16mappinginheritancewithjpa

You'll notice I use the terms "Ancestor, Parent, Child" to do essentially the same thing.

The tutorial demonstrates three ways to do inheritance mapping. You can mix and match.
Image


-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: composite-id with a foreign key to parent
PostPosted: Tue Aug 11, 2009 9:00 am 
Newbie

Joined: Tue Aug 11, 2009 8:18 am
Posts: 1
Hi,

I have the similar problem, but no inheritance relation between classes, I have :
table LOT
LOT_ID PK
LOT_DESC

and table LOT_VALUES (with compisite id)
LOT_ID pk (The LOT table primary key)
VAL_NB pk
LOT_VALUE

My hibernate mapping is :

For LOT

<class name="Lot" table="LOT">
<id name="lotId" column="LOT_ID" type="java.lang.Long">
<generator class="increment" />
</id>
<property name="lotDesc" type="java.lang.String" column="LOT_DESC" />
<bag name="listLotValues" lazy="true">
<key column="LOT_ID" />
<one-to-many class="LotValues" />
</bag>
</class>

For LOT_VALUES

<class name="LotValues" table="LOT_VALUES">
<composite-id>
<key-property name="lotId" type="java.lang.Long" column="LOT_ID" />
<key-property name="VAlNb" type="java.lang.Long" column="VAL_NB"/>
</composite-id>
<property name="LotValue" type="java.lang.Long" column="LOT_VALUE" />

</class>

In my application I try to save a new LOT with new lot_values list, i have the error :

Hibernate: insert into LOT (LOT_ID, LOT_DESC) values (?, ?)
Hibernate: update LOT_VALUES set LOT_ID=? where LOT_ID=? and VAL_NB=?
2009/08/11 11:40:26,162 ERROR [main] org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.HibernateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:32)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:916)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:23)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
....

I dont know why Hibernate try to update the table LOT_VALUES ?

Thanks for help


Top
 Profile  
 
 Post subject: Re: composite-id with a foreign key to parent
PostPosted: Tue Aug 11, 2009 9:48 am 
Newbie

Joined: Sun Aug 09, 2009 1:15 pm
Posts: 3
Thanks Cameron McKenzie for the quick reply.

I guess I wasn't clear enough before. hbmic understood me better. There is no inheritance here. Maybe instead of grandpa, parent, child relationship I should have used the owner, owner_owned, owned terminology.

owner has a collection of owner_owned, in a one-to-many bidirectional mapping.
owner_owned has a collection of owned, in a one-to-many bidirectional mapping, with part of owned's composite-id as a FK to owner_owned's PK.

Table onwer:
id (PK)

Table owner_owned:
id (PK)
owner_Id (FK)

Table owned:
owner_Id (PK, FK)
2nd_key (PK)

_________________
thanks for any help.


Top
 Profile  
 
 Post subject: Re: composite-id with a foreign key to parent
PostPosted: Tue Aug 18, 2009 12:12 pm 
Newbie

Joined: Sun Aug 09, 2009 1:15 pm
Posts: 3
Looks like I managed to pull it off. Hurray!!

Follows is my implementation. Pink indicates the Owner-OwnerOwned relationship. Blue indicates the OwnerOwned-Owned relationship. Both are bidirectional and are controlled by the child. This is why we need to add/remove the parent from the child (e.g. calling from Owner.addOwnerOwned to OwnerOwned.setOwner(this)).
Note that Owned must be Serializable, as it is used as the key in the set. Hibernate presented a way implementing the composite-id with a class, but I failed with this approach.

Hope someone will benefit from this post one day. Here it goes...

The Owner entity and table owner:
<hibernate-mapping default-access="field">
<class name="Owner" table="owner">
<id name="id" column="id">
<generator class="native"/>
</id>
<set name="owner_owned_set" cascade="all-delete-orphan" inverse="true" >
<key column="owner_id" not-null="true"/>
<one-to-many class="OwnerOwned"/>
</set>

</class>
</hibernate-mapping>

class Owner{
private int id;
private Set<OwnerOwned> owner_owned_set;
// getters/setters

void addOwnerOwned(OwnerOwned ownerOwned){
owner_owned_set.add(ownerOwned);
ownerOwned.setOwner(this);
}

void removeOwnerOwned(OwnerOwned ownerOwned){
owner_owned_set.remove(ownerOwned);
ownerOwned.setOwner(null);
}

}


The OwnerOwned entity and owner_owned table:
<hibernate-mapping default-access="field">
<class name="OwnerOwned " table="owner_owned">
<id name="id" column="id">
<generator class="native"/>
</id>
<set name="owned_set" cascade="all-delete-orphan" inverse="true" lazy="false">
<key column="owner_owned_id" not-null="true" />
<one-to-many class="Owned" />
</set>

<many-to-one name="owner" column="owner_id" not-null="true"/>
</class>
</hibernate-mapping>

class OwnerOwned{
private int id;
private Set<Owned> owned_set;
private Owner owner;
// getters/setters

void addOwned(Owned owned){
owned_set.add(owned);
owned.setOwnerOwned(this);
}

void removeOwned(Owned owned){
owned_set.remove(owned);
owned.setOwnerOwned(null);
}

}

The Owned entity and owned table:
<hibernate-mapping default-access="field" >
<class name="Owned" table="owned">
<composite-id>
<key-many-to-one class="OwnerOwned" column="owner_owned_id" name="ownerOwned"/>
<key-property name="anotherProperty" column="another_property"/>
</composite-id>
<many-to-one name="ownerOwned" column="owner_owned_id" not-null="true" insert="false" update="false"/>
</class>
</hibernate-mapping>

class Owned implements Serializable{
private OwnerOwned ownerOwned;
private int anotherProperty;
// getters/setters
}

_________________
thanks for any help.


Top
 Profile  
 
 Post subject: Re: composite-id with a foreign key to parent
PostPosted: Wed Aug 19, 2009 9:59 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
Maybe instead of grandpa, parent, child relationship I should have used the owner, owner_owned, owned terminology.


Ugh...don't you hate it when we speak the same language and say different things? Indeed, from a database lingo, you described you problem properly. I interpreted it from an object oriented point of view. My mistake.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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