I have problems with storing the foreign key.
I have two entities Order and OrderDetail. In OrderDetail I store type-value-pairs with an id and foreign key to an order. To each order I want to have a map with a mapkey-value pair type-value.
First problem was that when I tried to insert a new Order Hibernate made an INSERT on the order, but for the OrderDetails given in the Order Hibernate tries to make an UPDATE to the OrderDetails, although no OrderDetail was created before. So it fails.
I solved it by storing the OrderDetail before storing the Order (code and generated SQL below).
Code:
session.save(detail);
session.save(order);
Code:
Hibernate: insert into tbl_order_detail (type, value, order_fid) values (?, ?, ?)
Hibernate: insert into tbl_order (name) values (?)
But now the foreign key in table OrderDetail is not stored and I lose the connection between Order and OrderDetail.
Actually I want to store an Order like I did first.
I hope somebody can help me!!
Thanks!
--------Mapping from Order
Code:
<hibernate-mapping>
<class name="Order" table="tbl_order">
<id name="orderId" column="order_id" type="int" unsaved-value="null">
<generator class="native" />
</id>
<property name="name" column="name" type="java.lang.String" length="255" not-null="true" />
<map name="orderDetails" cascade="all" inverse="true" lazy="true">
<key column="order_fid" />
<map-key column="type" type="int" />
<one-to-many class="OrderDetail" />
</map>
</class>
</hibernate-mapping>
-------Mapping from Order Detail
Code:
<hibernate-mapping>
<class name="OrderDetail" table="tbl_order_item">
<id name="orderDetailId" column="order_detail_id" type="int" unsaved-value="null">
<generator class="native" />
</id>
<property name="type" column="type" type="int" not-null="true" />
<property name="value" column="value" type="java.lang.String" not-null="true" />
<many-to-one name="order" class="Order" column="order_fid" cascade="none" />
</class>
</hibernate-mapping>
Hibernate version: 3rc1
Name and version of the database you are using: MySQL 4.0.20