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.  [ 10 posts ] 
Author Message
 Post subject: Persisting 'new' objects
PostPosted: Wed Nov 26, 2008 4:14 pm 
Newbie

Joined: Wed Nov 26, 2008 3:37 pm
Posts: 5
Hi all, first post :)

I have found a particular problem with an NHibernate web app I've written. There are two classes, Product and Vehicle, and a many-to-many relationship between them.

This all works fine except for when I create a new Product, and populate it's Vehicle collection. I populate the Vehicle collection by going through a checkbox list on the web page, and retrieving the vehicle record for each checkbox, then add it to the collection on the product. Then I pass the product to NHibernate to save.

When I look in the db, the many-to-many table shows 0 in the Product_ID column.

Do I need to save the product first, then attach the Vehicle records and save again?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2008 3:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post your mappings ? Hard to say without that ...

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2008 2:44 pm 
Newbie

Joined: Wed Nov 26, 2008 3:37 pm
Posts: 5
Mapping for vehicle (simple class)

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">
   <class name="Binary.CMS.Models.Vehicle, App_Code" table="vehicles">
      <id name="Id">
        <generator class="identity" />
      </id>
      <property name="Name" length="45" not-null="true" column="name"/>
   </class>
</hibernate-mapping>



Mapping for product:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">
    <class name="Binary.CMS.Models.Product, App_Code" table="products">
      <id name="Id">
        <generator class="identity" />
      </id>
      <property name="PartNumber" column="partno" />
      <property name="ProductTitle" column="title" />
      <property name="Description" column="description" />
      <property name="Summary" column="summary" />
      <property name="InStock" column="instock" />
      <property name="AvailableToBuy" column="availabletobuy" />
      <property name="Price" column="price" />
      <property name="ImageFileName" column="imagefilename" />
      <property name="Keywords" column="keywords" />
      <property name="LastUpdated" column="timestamp" />
      <property name="CategoryId" column="categoryid" />
      <property name="Featured" column="featured" />
      <bag name="Vehicles" table="productmodel">
        <key column="product_id"/>
        <many-to-many column="model_id"
           class="Binary.CMS.Models.Vehicle, App_Code"/>
      </bag>
    </class>
</hibernate-mapping>



What I am trying to do is basically this:

Product x = new Product()
x.Vehicles.Add(vehicleA)
x.Vehicles.Add(vehicleB)

session.save(x)

The product row is written correctly, and rows in the productmodel table are written that manage the cross reference, but unfortunately the product_id on these rows is 0.

For existing product records it all works fine so I don't think the mapping is the problem?

Do I need to do this:

Product x = new Product()
session.save(x)

x.Vehicles.Add(vehicleA)
x.Vehicles.Add(vehicleB)
session.save(x)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2008 8:18 pm 
Newbie

Joined: Wed Nov 26, 2008 3:37 pm
Posts: 5
After running through the debugger my problem seems a bit more fundamental.

When I create a new Product object and call SaveOrUpdate, the ID property of the object is still 0 afterwards even though it's been inserted in the DB and has a proper value in the ID column.

So when NHibernate tries to persist the attached objects in the Vehicles collection, it's using 0 as the value of the product ID.

I'm using NHibernate with MySQL 4.1, and have tried changing the generator class to native instead of identity, but still the problem remains.

Does anyone have any ideas how to fix this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 2:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not familiar with MySQL. Do you have a sequence or an identity column defined in your table ?

In general these kind of generators aren't recommended because hibernate has to do the insert as soon as you Save() the object instead of doing this during Commit/Flush. Try to use GUID or something that's generated in your app and not in the db. There are plenty of discussions about that topic in http://groups.google.com/group/nhusers.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 4:21 am 
Newbie

Joined: Wed Nov 26, 2008 3:37 pm
Posts: 5
Yes that's right, the column is configured as INT type with the autogen flag set to true.

The ID is being set fine in the database, but NHibernate doesn't update the object with the new ID. This means my associations don't work when I create a new object.

If I create a product record first in my web app, then save and go back in, I can add associations and it works fine.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 4:32 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Strange .... can you enable "show_sql" and post the log with the inserts and updates ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 6:54 pm 
Newbie

Joined: Wed Nov 26, 2008 3:37 pm
Posts: 5
Wolli, sorry I've not updated the thread, but I've not had chance to look into this for a while (it's a home project rather than a work project :) )

Anyway, my app uses code to configure NHibernate, I added this line:


Code:
config.SetProperty(Environment.ShowSql, "true");


But there is nothing in the debug output window in VS. Do I need to do anything else to get the SQL logging to work?


Thanks,

Dorian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2008 2:17 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Afaik you have to enable/configure log4net. Have a look here:

http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate.aspx

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2008 7:33 am 
Newbie

Joined: Fri Aug 22, 2008 6:56 am
Posts: 5
i had some problems before because of the autogenerating indexes. be sure to set the default value to "0".

i also notice that you did not specify the cascade mode.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.