-->
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.  [ 1 post ] 
Author Message
 Post subject: many-to-many : partly working
PostPosted: Mon Apr 24, 2006 4:01 pm 
Newbie

Joined: Mon Apr 24, 2006 3:33 pm
Posts: 1
the following happens with a many-to-many relation

when i add a new product to an order, a SaveOrUpdate works fine.
(although i have to save both product & order. Only when i save Product, the OrderRegel table is filled. When Order is saved, no insert/update statements hit the DB. Is this normal behaviour?)

The main problem:
when we add an existing product to an order, a SaveOrUpdate does not do anything. (not passing insert or update statements to the DB)

Any help would be really appreciated with this.

i have the following tables:

Order >- OrderRegel -< Product

With the following classes
Order >-< Products

Code sample 1: new Product (Works)

Code:
Order order = new OrdersDao().GetById(23);

Product newProduct = new Product();
newProduct.Name = "Test";
order.Products.Add(newProduct);
newProduct.Orders.Add(order);               
order.OrderDate = DateTime.Now;
   
new ProductDao().SaveOrUpdate(newProduct);
new OrderDao().SaveOrUpdate(order);


Code sample 2: existing product (does not work)

Code:
Order order = new OrdersDao().GetById(23);

Product existingProduct = new ProductDao().GetById(1);
order.Products.Add(existingProduct);
existingProduct.Orders.Add(order);               
order.OrderDate = DateTime.Now;
   
new ProductDao().SaveOrUpdate(existingProduct);
new OrderDao().SaveOrUpdate(order);

Our mapping:

Product:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="DomainModel.Product,DomainModel" table="[Product]">

      <id name="Id" column="ProductId" type="Int64" unsaved-value="0">
         <generator class="native"/>
      </id>
      
    <idbag name="Orders" table="OrderRegel" lazy="true" cascade="save-update" >
      <collection-id column="Id" type="Guid" >
        <generator class="guid"  />
      </collection-id>
      <key column="ProductId"/>
      <many-to-many class="DomainModel.Orders, DomainModel" column="OrdersId" />
    </idbag>
   
      <property column="[Name]" type="String" name="Name" length="50" />
      
   </class>
</hibernate-mapping>

Order:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="DomainModel.Orders,DomainModel" table="[Orders]" lazy="true">

      <id name="Id" column="OrdersId" type="Int64" unsaved-value="0">
         <generator class="native"/>
      </id>
   
    <idbag name="Producten" table="OrderRegel" lazy="true" cascade="save-update" >
      <collection-id column="Id" type="Guid" >
        <generator class="guid"  />
      </collection-id>
      <key column="OrdersId"/>
      <many-to-many class="DomainModel.Product, DomainModel" column="ProductId" outer-join="true"/>
    </idbag>
   
      <many-to-one name="Customer" column="[Customer]" class="DomainModel.Customer,DomainModel" cascade="all"  />
      <property column="[OrderDate]" type="DateTime" name="OrderDate" not-null="true" />
      
   </class>
</hibernate-mapping>


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

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.