I'm trying to get hibernate to handle using a lookup table for may keys in a transparent manner. I keep getting close, but I haven't been able to get the exact behavior I need/want. It might not be possible, but it sure seems like it could be (or so I hope!).
At a high level: I want map keys to be references to values in a lookup table, but, in my code look like they are simple Strings. That is, I want hibernate to persist references to the lookup table by id, but map the keys to their lookup values when hydrating the classes. I'll explain in more detail...
The tables directly involved look like this:
Code:
-- this is the lookup table (mapped to Report200LineItem)
create table report_200_line_items (
id int identity not null,
name varchar(48) not null unique,
primary key (id)
)
-- this is a map entry which references the lookup table via item_name (mapped to LineItemEntry)
create table line_item_entries (
id int identity not null,
code varchar(12) not null,
amount numeric(19,2) not null,
line_item_prefs_id int null,
item_name int null,
primary key (id)
)
-- this is the parent class which 'has' a map of line_item_entries (mapped to LineItemPreferences)
create table line_item_prefs (
id int identity not null,
min_charge numeric(19,2) null,
primary key (id)
)
alter table line_item_entries add constraint FK173229CF8AB576F0 foreign key (item_name) references report_200_line_items
alter table line_item_entries add constraint FK173229CF2BC26970 foreign key (line_item_prefs_id) references line_item_prefs
I
want the Java class that line_item_entries maps to to look like this:
Code:
public class LineItemPreferences {
// <getters/setters omitted for brevity>
private Integer id;
private BigDecimal minimumCharge;
private Map<String, LineItemEntry> lineItems; // the String is the textual name from the lookup table
// [A] private Map<Report200LineItem, LineItemEntry> lineItems;
}
Note that the line marked with the [A] works just fine, it's the line before it that I want though. Remember, in my code, when I'm using this class, I want the fact that my map-key is a reference to a lookup table to be transparent.
Here is the relevant mapping information (which works when I'm using the code marked by [A]):
Code:
<class name="Report200LineItem" table="report_200_line_items">
<id name="id" column="id" type="java.lang.Integer">
<generator class="identity" />
</id>
<property name="name" column="name" type="string" length="48" not-null="true" unique="true"/>
</class>
<class name="LineItemPreferences" table="line_item_prefs">
<id name="id" column="id" type="java.lang.Integer">
<generator class="identity" />
</id>
<property name="minimumCharge" column="min_charge" type="big_decimal" />
<map name="lineItems" table="line_item_entries" lazy="false" cascade="all,delete-orphan">
<key column="line_item_prefs_id" />
<map-key-many-to-many class="Report200LineItem" column="item_name" />
<one-to-many class="LineItemEntry" />
</map>
</class>
<class name="LineItemEntry" table="line_item_entries">
<id name="id" column="id" type="java.lang.Integer">
<generator class="identity" />
</id>
<property name="code" column="code" type="string" length="12" not-null="true" />
<property name="amount" column="amount" type="big_decimal" not-null="true" />
</class>
Is there a way to acheive the trasparency of the lookup table that I desire? If so, what must I do to my mapping to make it happen?
*Any* pointers or assistence would be a life-saver at this point.
Christian
FYI: I'm using Hibernate 3.0.5 against MS SQL Server 2000 on a Java 1.5 SDK.