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....