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.  [ 3 posts ] 
Author Message
 Post subject: Unidirectional many-to-many in legacy schema
PostPosted: Tue Jul 26, 2005 1:01 pm 
Newbie

Joined: Tue May 24, 2005 6:31 pm
Posts: 7
i am somewhat new to ORM, so i apologize if this has an obvious solution. i dug around in the textbooks, on the forums, and on the web at large, and saw nothing that seemed to resemble this (not clearly enough for me to recognize, at least).

i will detail my situation, and you can respond directly, but pointers to tutorials that cover legacy cases like this would be appreciated.

at the outset, note that i am working with a legacy database, and the schema must be considered "immutable". i won't be allowed to touch it.

i have three tables:

Code:
create table A
(
   id integer not null primary key
   category_id integer not null
)

create table B
(
   id integer not null primary key
   category_id integer not null
)

create table Category
(
   id integer not null primary key
)


category_id is a foreign key to table Category, in both table A and table B.

this would seem, at least in some twisted form, to be a many-to-many through a "link table", but obviously in this case it is the primary key of the link table that is embedded in the other two tables, rather than the primary keys of the other two tables embedded in the link table.

so, i want to put together a mapping that associates A with B, unidirectionally, via category_id.

obviously, in SQL, i could just join A to B through category_id, and generate a result set out of B.

however, i want to map both A and B as persistent classes, and would like to construct a unidirectional association from A to B, with the mapped class for A containing a collection of instances of the mapped class for B. i would like this collection (fetching) to be lazy-evaluated at runtime.

as for mappings, i have gotten this far:

Code:
<class name="A" table="A">
   <id name="id" type="java.lang.Integer">
      <column name="ID" not-null="true" unique="true" sql-type="INTEGER" />
      <generator class="assigned" />
   </id>
   <property name="categoryId" type="java.lang.Integer">
      <column name="CATEGORY_ID" not-null="true" sql-type="INTEGER" />
   </property>
   <set name="setOfB" table="Category" lazy="true">
      <key column="ID" property-ref="categoryId" />
      <many-to-many column="ID" class="B" />
   </set>
</class>
<class name="B" table="B">
   <id name="id" type="java.lang.Integer">
      <column name="ID" not-null="true" unique="true" sql-type="INTEGER" />
      <generator class="assigned" />
   </id>
   <property name="categoryId" type="java.lang.Integer">
      <column name="CATEGORY_ID" not-null="true" sql-type="INTEGER" />
   </property>
</class>


however, category_id is a primary key in neither of tables A or B (it is only a primary key in table Category), so referencing B through the "link table" via its own category_id doesn't seem possible with a many-to-many association. i don't know how to identify that the column in B to use for constructing the association should be category_id (versus the primary key of B).

additional constraints: i do not want hibernate to manage table Category, i want it to be considered "reference data" (it is maintained by legacy applications). and again, i do not need any association from B to A (i would prefer this to be unidirectional from A to B). given there is no direct relationship between A and B, i probably won't need this to cascade.

can anyone suggest a direction to take? formally map Category as a persistent class, and map through it (many-to-one from A to Category, one-to-many from Category to B)? seems a bit inelegant....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 6:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yew!


You need to use property-ref on both <key> and <many-to-many> tags. You will need the very latest 3.1 beta 1 release for this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 6:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
BTW, if you ever need to update the association, you will need to do a couple more things, but using property-ref is a good first step.


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