Hello,
I could use some help making a mapping file for some unusual pre-existing Java code. In our system there is a DomainModel which keeps track of a bunch of ModelElements (ME). MEs basically just store information and don't really interact with each other. Any processing of the information in an ME happens elsewhere in the code. Each ME has a unique Identifier, which is essentially a wrapper around an int. When a ME needs to store a reference to another ME it does so by way of an Identifer rather than a normal reference to a Java object. To actually get access to the referenced object, any processing code has to go through the DomainModel. For example, Calendars and HolidaySets are both MEs, and a Calendar has a HolidaySet. Following is example code showing how some miscellaneous processing class would get the HolidaySet of a Calendar.
Calendar.java
Code:
public class Calendar extends ModelElement {
Identifier holidaySet;
public Identifier getHolidaySet() {
return holidaySet;
}
public void setHolidaySet(Identifier id) {
holidaySet = id;
}
}
DomainModel.java
Code:
public class DomainModel {
public ModelElement getObjectFromId(Identifier id) {
//do whatever needs to happen to return the appropriate ModelElement here
}
}
Processor.java
Code:
public class Processor {
DomainModel dm;
public Processor() {
dm = new DomainModel();
}
public HolidaySet getCalHolidaySet(Identifier id) {
Calendar cal = (Calendar) dm.getObjectFromId(id);
return (HolidaySet) dm.getObjectFromId(cal.getHolidaySet());
}
}
To model the associations between MEs in my hibernate mapping I'm using properties and collections with type "integer" and using a PropertyAccessor to convert between Identifiers and ints. (MEs don't have access to the DomainModel so they aren't able to convert an Identifier into a ME. So I can't use a PropertyAccessor on the MEs that translates between Identifiers and MEs to facilitate regular association mappings). But if I do this I don't know how to enforce foreign key constraints on those ints. For example, in the mapping and sql below (representing a bidirectional many-to-one association between Calendars and HolidaySets) I would like to say that the column HOLIDAY_SET_ID in the table CALENDARS is a foreign key referencing the table HOLIDAY_SETS (and vice versa for references from HolidaySet to Calendar).
Is there a way to do that? Or is there another way I should be modelling this situation?
Thank you.
Hibernate version:
3.0.5
Mapping documents:
<hibernate-mapping>
<!-- IdentiferAccessor implements PropertyAccessor and translates between Identifiers on the Java side and ints on the DB side-->
<class name="ModelElement" table="MODEL_ELEMENTS">
<id name="id" type="integer" column="ID" access="IdentifierAccessor"/>
<joined-subclass
name="Calendar"
table="CALENDARS">
<key column="CALENDAR_ID"/>
<property name="holidaySet" type="integer" column="HOLIDAY_SET_ID"
access="IdentifierAccessor"/>
</joined-subclass>
<joined-subclass
name="HolidaySet"
table="HOLIDAY_SETS">
<key column="HOLIDAY_SET_ID"/>
<!-- IdentiferListAccessor implements PropertyAccessor and translates between a list of Identifiers on the Java side and list of ints on the DB side-->
<bag name="calendars" table="HOLIDAY_SET_CALENDARS"
access="IdentifierListAccessor">
<key column="HOLIDAY_SET_ID"/>
<element type="integer" column="CALENDAR_ID"/>
</bag>
</joined-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
N/A
Full stack trace of any exception that occurs:
N/A
Name and version of the database you are using:
HSQL 1.8.0
The generated SQL (show_sql=true):
create table CALENDARS (
CALENDAR_ID integer not null,
HOLIDAY_SET_ID integer,
primary key (CALENDAR_ID)
)
create table HOLIDAY_SETS (
HOLIDAY_SET_ID integer not null,
primary key (HOLIDAY_SET_ID)
)
create table HOLIDAY_SET_CALENDARS (
HOLIDAY_SET_ID integer not null,
CALENDAR_ID integer
)
create table MODEL_ELEMENTS (
ID integer not null,
primary key (ID)
)
alter table CALENDARS
add constraint FK5C987CD5B4E03422
foreign key (CALENDAR_ID)
references MODEL_ELEMENTS
alter table HOLIDAY_SETS
add constraint FK8D60AFF839EF7245
foreign key (HOLIDAY_SET_ID)
references MODEL_ELEMENTS
alter table HOLIDAY_SET_CALENDARS
add constraint FK73F5EEA180957E68
foreign key (HOLIDAY_SET_ID)
references HOLIDAY_SETS
Debug level Hibernate log excerpt:
N/A