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.  [ 1 post ] 
Author Message
 Post subject: one-to-many; several parent types with single child table?
PostPosted: Wed Nov 24, 2004 5:41 pm 
Newbie

Joined: Wed Nov 24, 2004 4:56 pm
Posts: 1
Hibernate version: 2.1.7

Name and version of the database you are using: Oracle 9i

I'm working with a database schema that was designed (but hasn't yet been implemented) before we decided to use Hibernate. It represents a system with several object types (e.g. Product, Contact, Distribution, Format, etc.), each of which we want to have Notes attached to.

I've been attempting to write my mapping documents and have come across a problem with this design. From reading the documentation, it is my understanding that using the collection mapping elements requires the use of the key element. Creating a map (or similar) that way implies a foreign key constraint on the child table.

In this case, however, the child table is represented in DDL as follows:

Code:
CREATE TABLE notes (
       NOTE_ID NUMBER(9) NOT NULL,
       REFERENCE_TYPE varchar2(50) NOT NULL,
       REFERENCE_ID NUMBER(9) NOT NULL,
       NOTE_TITLE varchar2(50) NULL,
       NOTE_TEXT varchar2(1000) NULL,
       DATE_CREATED timestamp NOT NULL,
       AUTHOR_ID NUMBER(9) NOT NULL
);

ALTER TABLE notes
       ADD  ( PRIMARY KEY (NOTE_ID) ) ;


The idea is to use the reference_type and reference_id columns to describe the parent object of a particular Note. reference_type will hold the (abbreviated) Java class name of the parent and reference_id the parent's ID. (All IDs in the system are unique and generated by a single sequence.)

Because of this design choice, it's not possible to create a foreign key constraint for reference_id, because it won't always have the same parent table.

I've explored briefly the where attribute for the map element, but that doesn't seem designed to permit dynamic values or define this kind of loosely-tied foreign key.

Is it possible (or even sane) to map this using Hibernate? We went this route to avoid the creation of a note table for each type of object...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.