-->
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.  [ 7 posts ] 
Author Message
 Post subject: Object owned by to one of many possible parents
PostPosted: Mon Nov 10, 2003 1:47 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
I am trying to implement the following mapping:
A (1) ----- (*) C
B (1) ----- (*) C

An object of type C may belong either to A or C (not to both). I mapped the foreign key of A and B to the same column on C ("ParentId").

When I tried to export the schema to the database, I got the following exception:

INFO: echoing all SQL to stdout
net.sf.hibernate.MappingException: Repeated column in mapping for class test.C should be mapped with insert="false" update="false": ParentId
at net.sf.hibernate.persister.AbstractEntityPersister.checkColumnDuplication(AbstractEntityPersister.java:720)
at net.sf.hibernate.persister.NormalizedEntityPersister.<init>(NormalizedEntityPersister.java:824)
at net.sf.hibernate.persister.PersisterFactory.create(PersisterFactory.java:32)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:207)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:627)

My question is how to implement this scenario and still have cascade save-update working.

Thank you,

mota


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 1:54 pm 
Newbie

Joined: Tue Oct 21, 2003 6:14 pm
Posts: 11
I dont think Databases support that kind of relations
where the same Column is the foriegn key to different tables.

Easiest way is to have an abstract parent class which would implement
all the fucntionality and have two extensions C' and c" and may
a->c' and b->c". this will result in two tables but from the code standpoitn
of view its the same code.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 2:11 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
I am trying to implement a scenario similar to the following:

Work object stored in Works table.
Book subclass of Work stored in table Books.
Song subclass of Work stored in table Songs.

Two entities Singer and Author.

Author (1) ---- (*) Book
Singer (1) ---- (*) Song

Works table contains the common field to both Book and Song.

Any idea about the best design practice to handle this scenario.

Thank you,

mota


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 2:23 pm 
Newbie

Joined: Tue Oct 21, 2003 6:14 pm
Posts: 11
The other technique u can use is , many to many relation
though its 1->many

A(author)-->(middle table AxW)---> work
S(singer)-->(middle table SxW)-----> work.
Its little more inefficient


Top
 Profile  
 
 Post subject: Re: Object owned by to one of many possible parents
PostPosted: Mon Nov 10, 2003 3:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
mota wrote:
I am trying to implement the following mapping:
A (1) ----- (*) C
B (1) ----- (*) C

An object of type C may belong either to A or C (not to both). I mapped the foreign key of A and B to the same column on C ("ParentId").


Never played with that myself, but you can try the many-to-any tag

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 7:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually it looks like its just a basic <any> mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 10:42 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
I came up with the following design:

Author (1) ---------- (*) Book (extends Work)
Singer (1) ---------- (*) Song (extends Work)

I abandoned setting a common foreign key (for both Author and Singer) on the parent class (Work). Instead I moved the foreign keys to subclasses. I have the foreign key of Author in Book, and that of Singer in Song. This way I can keep the one-to-many associations.

Any feedback about this design?

Thank you,

mota

[/img]


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