Imagine the following ER Model:
Code:
------- --------------- ---------
|order| |order_product| |product|
--------(1..N)--(1)-----------------(1)--(1..N)----------
|id | |quantity | |id |
|... | | | |name |
------- --------------- ---------
This translates to the following tables in a relational database:
Code:
create table order(
id integer not null,
primary key(id)
)
create table product(
id integer not null,
name varchar(255) nut null,
primary key(id)
)
create table order_product(
order_id integer not null,
product_id integer not null,
quantity integer not null,
foreign key(order_id) references order(id),
foreign key(product_id) references product(id),
primary key(order_id,product_id)
)
I now want to access the products with their corresponding quantity directly from the Order class using a Map:
Code:
class Order {
int id;
Map<Product, Integer> products = new HashMap<Product, Integer>;
...
}
The products Map shall contain the products of this order as its keyset and the quantity shall by the corresponding value.
How do I realize this with Hibernate without having to explicitly map the order_product table and without having a Set of OrderProducts in Order? Is this possible at all?
Uli