-->
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: Mapping Relationships
PostPosted: Thu Jul 14, 2005 5:06 pm 
Newbie

Joined: Wed Apr 27, 2005 1:30 pm
Posts: 7
Hi Guys,

I have some beginners issues with mapping associations, i have reference material but still would like to get peoples thoughts.

I have a table Customer_Orders, that contains order informtion
I have a table Products, that lists productions available to buy.

I then have a table called orders_products which contains, an order id, a product_id and product qty.

When i create a new order all works fine except populating the orders_products table. If i remove the product qty attribute of the table i can get the table to populate
Mapping Files below.

Code:
CustomerOrders.hbm

<set
         inverse="true"
         lazy="true"
         name="ordersProductsSet"
         table="orders_products"
      >
         <key column="ORDER_ID" />
         <one-to-many class="OrdersProducts" />
      </set>


Products.hbm

Code:
<set
         inverse="true"
         lazy="true"
         name="ordersProductsSet"
      >
         <key column="PRODUCT_ID" />
         <one-to-many class="OrdersProducts" />
      </set>


OrdersProducts.hbm
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.egalway.estore.vo">
   <class name="OrdersProducts" table="orders_products">
      <composite-id class="OrdersProductsPK" name="id">
         <key-many-to-one
            class="Products"
            column="PRODUCT_ID"
            name="product"
          />
         <key-many-to-one
            class="CustomerOrders"
            column="ORDER_ID"
            name="order"
          />
      </composite-id>
      <property
         column="PRODUCT_QTY"
         length="11"
         name="productQty"
         not-null="false"
         type="integer"
       />
   </class>
</hibernate-mapping>


If anyone could point out how i can populate the orders_products table that would be great.

The following is what i am trying from the java side.

Code:
        OrdersProducts op = new OrdersProducts();
        op.setId(new OrdersProductsPK(p,order));
        op.setProductQty(new Integer(99));
        storeConfigService.saveObject(order);


_________________
Tell me, I'll forget. Show me, I may remember. But involve me, and I'll understand.


Top
 Profile  
 
 Post subject: similar
PostPosted: Thu Jul 14, 2005 6:59 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
I have something similar involving users and groups. Users are in Groups, Groups have Users, and there is a Type association so I have a UserGroupRel object in the middle that contains both references and a Type. This should work similar for you with a quantity. (My associations are all bi-directional, not sure if you need that.) Here is my xml mapping:

Code:
<class   name="User"   table="MyUser">
   <id   column="id"   name="id"   type="long"   unsaved-value="0">
      <generator   class="native"   />
   </id>
   <set name="groups" cascade="save-update">
      <key column="USER_ID" />
      <one-to-many class="UserGroupRel"/>
   </set>
</class>
<class name="UserGroupRel" table="USER_GROUP_REL">
   <id   column="id"   name="id"   type="long"   unsaved-value="0">
      <generator   class="native"   />
   </id>
   <many-to-one name="user" cascade="save-update" column="USER_ID" class="User" lazy="false" />
   <many-to-one name="group" cascade="save-update" column="GROUP_ID" class="Group" lazy="false" />
   <property name="relType" column="rel_type" type="string"/>
</class>
<class   name="Group"   table="MyGroup">
   <id   column="id"   name="id"   type="long"   unsaved-value="0">
      <generator   class="native"   />
   </id>
   <set name="users" cascade="save-update">
      <key column="GROUP_ID" />
      <one-to-many class="UserGroupRel"/>
   </set>
</class>


the USER_GROUP_REL table has 4 columns: ID, USER_ID, GROUP_ID, REL_TYPE

Good luck!


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.