-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Foreign Key is not stored
PostPosted: Mon Mar 21, 2005 5:48 am 
Newbie

Joined: Mon Mar 21, 2005 5:13 am
Posts: 1
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


Top
 Profile  
 
 Post subject: not tried to closley but
PostPosted: Wed Mar 23, 2005 7:23 pm 
Newbie

Joined: Fri Mar 04, 2005 7:10 am
Posts: 12
bit of a newbie still here. Havnt bottomoed out how this all seems to work, however i think i tried something similar.

becasue the order has inverse = true you have to make the save from the order detail end.

hence you need a outine line addOrderDetail in the order that takes on orderDetail obj as param. calls the setOrder func on the detail (sets the non inverse end) and adds the ord detail to the java collection in the order record (but no update occus here as its the inverse).

What I then think i had to do was set a casce =all in the order detail many to one declariotn and then call

sessio.saveAndUpdate (order) this seemed to trigger the update without having to same the orderDetail at all as the cascade action takes care of it.

Will


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.