Everything's fine after creating an bi-directional association with an IList<Property> in model.
Thanks, Stefan. Blog: http://www.cnblogs.com/JeffreyZhao/ Twitter: http://twitter.com/jeffz_cn -------------------------------------------------- From: "Jeffrey Zhao" <je...@live.com> Sent: Friday, August 14, 2009 10:27 PM To: <nhusers@googlegroups.com> Subject: [nhusers] Re: How can I JUST delete an entity using NHibernate > > Thanks for your reply, but it seems that I have to map Property as entitis > since I need to get a single Property, modify and submit the changes of > it. > > I tried the mapping: > > <class name="Product" table="Product"> > <id name="ProductID" column="ProductID"> > <generator class="identity"/> > </id> > <property name="Name"/> > <list name="Properties" cascade="all" lazy="true" inverse="true"> > <key column="ProductID" /> > <index column="SortOrder" /> > <one-to-many class="Property"/> > </list> > </class> > > <class name="Property"> > <id name="PropertyID"> > <generator class="identity"/> > </id> > <property name="Name"/> > </class> > > > but it seems that NH won't only provide a single Name column when insert a > Property instance - I have to map SortOrder as a property. > > I'm trying to build a bi-directional association between Product and > Property. Thanks for your help. > > > Blog: http://www.cnblogs.com/JeffreyZhao/ > Twitter: http://twitter.com/jeffz_cn > > -------------------------------------------------- > From: "Stefan Steinegger" <stefan.steineg...@bluewin.ch> > Sent: Friday, August 14, 2009 9:06 PM > To: "nhusers" <nhusers@googlegroups.com> > Subject: [nhusers] Re: How can I JUST delete an entity using NHibernate > >> >> The index of a list is no property. It is just a column to allow NH to >> read it in the same order back as it has been stored. >> >> Not every column is a property. Foreign keys aren't, list indexes are >> also no properties. >> >> With a list of composite-elements your Property would only look like >> this: >> >> public class Property >> { >> public virtual string Name { get; set; } >> } >> >> And the table would have this columns: >> >> ProductId (foreign key to product) >> SortOrder >> Name >> >> >> On 14 Aug., 14:44, "Jeffrey Zhao" <je...@live.com> wrote: >>> For the last question, the reason I use "Set" is that "List" in NH >>> mapping >>> has an "index". I tried to use the Property.SortOrder as the index but >>> met a >>> lot of problems. So I try to use a "sorted set" (as the doc mentioned) >>> as >>> a >>> workaround. >>> >>> Thanks for your reply, I'll have a try later. I used Linq to SQL before >>> and >>> now I'm going to use NH but it seems much more complicated than L2S. >>> >>> Blog:http://www.cnblogs.com/JeffreyZhao/ >>> Twitter:http://twitter.com/jeffz_cn >>> >>> -------------------------------------------------- >>> From: "Stefan Steinegger" <stefan.steineg...@bluewin.ch> >>> Sent: Friday, August 14, 2009 7:55 PM >>> To: "nhusers" <nhusers@googlegroups.com> >>> Subject: [nhusers] Re: How can I JUST delete an entity using NHibernate >>> >>> >>> >>> > You mapped the ProductID in the Property: >>> >>> > <class name="Property"> >>> > ... >>> > <property name="Name"/> >>> > <property name="SortOrder"/> >>> > <property name="ProductID" /> <<== evil >>> > </class> >>> >>> > This probably resets the product id with the property value, which is >>> > not initialized. >>> >>> > Consider to have a full reference to product (don't forget to make the >>> > set inverse="true") or just leave it away. >>> >>> > You are working with an ORM. This means: you don't have any relational >>> > stuff in your class model. So you shouldn't have "foreign keys" there. >>> >>> > BTW, you probably should map it like this: >>> >>> > <set name="Properties" cascade="all" table="Product_Details"> >>> > <key column="ProductID" on-delete="cascade"/> >>> > <composite-element type="Property"> >>> > <property name="Name"/> >>> > <property name="..."/> >>> > </composite-element> >>> > </set> >>> >>> > This makes it a list of value-type-alike instances. Property will not >>> > have an id anymore. >>> > Seehttp://nhforge.org/doc/nh/en/index.html#components >>> >>> > And - why is it a set if you need a sort order? It should probably be >>> > a list. >>> >>> > On 14 Aug., 12:38, "Jeffrey Zhao" <je...@live.com> wrote: >>> >> Thanks, I tried. As the post said, I changed the mapping sittings >>> >> like: >>> >>> >> <set name="Properties" cascade="all(or save-update)" lazy="true" >>> >> inverse="true"> >>> >> <key column="ProductID" on-delete="cascade" /> >>> >> <one-to-many class="Property"/> >>> >> </set> >>> >>> >> And now NH just execute an DELETE sql as I want. But, I cannot >>> >> correctly >>> >> insert the data now: >>> >>> >> var product = new Product { Name = "Product" }; >>> >> product.Properties.Add(new Property { Name = "P0", SortOrder = 0 }); >>> >> product.Properties.Add(new Property { Name = "P1", SortOrder = 1 }); >>> >> session.Save(product); >>> >> session.Flush(); >>> >>> >> But now the data in database is: >>> >>> >> table Product >>> >> ProductID Name >>> >> ----------- -------------------------------------------------- >>> >> 9 Product >>> >>> >> table Property >>> >> PropertyID ProductID Name >>> >> SortOrder >>> >> ----------- ----------- >>> >> -------------------------------------------------- >>> >> ----------- >>> >> 12 0 P0 >>> >> 0 >>> >> 13 0 P1 >>> >> 1 >>> >>> >> The ProductID column in Property should be 9 but 0. >>> >>> >> // PS: I cannot find the discription of on-delete attribute in the >>> >> doc:http://nhforge.org/doc/nh/en/index.html. >>> >>> >> Blog:http://www.cnblogs.com/JeffreyZhao/ >>> >> Twitter:http://twitter.com/jeffz_cn >>> >>> >> -------------------------------------------------- >>> >> From: "Stefan Steinegger" <stefan.steineg...@bluewin.ch> >>> >> Sent: Friday, August 14, 2009 5:59 PM >>> >> To: "nhusers" <nhusers@googlegroups.com> >>> >> Subject: [nhusers] Re: How can I JUST delete an entity using >>> >> NHibernate >>> >>> >> > Try >>> >>> >> > Product product = session.Load<Product>(1); >>> >> > session.Delete(product); >>> >>> >> > This should actually only create a proxy for the Product 8assuming >>> >> > you >>> >> > are using lazy loading). Then you can remove it. >>> >>> >> > then you can use on-delete in the mapping to let the database clean >>> >> > up >>> >> > referenced records: >>> >> >http://vanryswyckjan.blogspot.com/2008/04/nhibernate-20-and-cascading... >>> >>> >> > On 14 Aug., 11:12, "Jeffrey Zhao" <je...@live.com> wrote: >>> >> >> Hello, >>> >>> >> >> I'm a new for NH and I met the problem of "how can I delete an >>> >> >> entity". >>> >> >> For example, I've got two entity with an one-to-many association. >>> >> >> Here's the sql to build the table (omit the association): >>> >>> >> >> CREATE TABLE [dbo].[Property]( >>> >> >> [PropertyID] [int] IDENTITY(1,1) NOT NULL, >>> >> >> [ProductID] [int] NOT NULL, >>> >> >> [Name] [nvarchar](50) NOT NULL, >>> >> >> [SortOrder] [int] NOT NULL, >>> >> >> CONSTRAINT [PK_Property] PRIMARY KEY CLUSTERED >>> >> >> ( >>> >> >> [PropertyID] ASC >>> >> >> )) >>> >> >> GO >>> >>> >> >> CREATE TABLE [dbo].[Product]( >>> >> >> [ProductID] [int] IDENTITY(1,1) NOT NULL, >>> >> >> [Name] [nvarchar](50) NOT NULL, >>> >> >> CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED >>> >> >> ( >>> >> >> [ProductID] ASC >>> >> >> )) >>> >> >> GO >>> >>> >> >> Here comes the entities: >>> >>> >> >> public class Product >>> >> >> { >>> >> >> public virtual int ProductID { get; set; } >>> >> >> public virtual string Name { get; set; } >>> >> >> public virtual ISet<Property> Properties { get; set; }} >>> >>> >> >> public class Property >>> >> >> { >>> >> >> public virtual int PropertyID { get; set; } >>> >> >> public virtual int ProductID { get; set; } >>> >> >> public virtual string Name { get; set; } >>> >> >> public virtual int SortOrder { get; set; } >>> >>> >> >> } >>> >>> >> >> and my hbm.xml file: >>> >>> >> >> <?xml version="1.0" encoding="utf-8" ?> >>> >> >> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >>> >> >> assembly="NHTest" >>> >> >> namespace="NHTest"> >>> >> >> <class name="Product" table="Product"> >>> >> >> <id name="ProductID" column="ProductID"> >>> >> >> <generator class="identity"/> >>> >> >> </id> >>> >> >> <property name="Name"/> >>> >> >> <set name="Properties" cascade="all"> >>> >> >> <key column="ProductID"/> >>> >> >> <one-to-many class="Property"/> >>> >> >> </set> >>> >> >> </class> >>> >> >> <class name="Property"> >>> >> >> <id name="PropertyID"> >>> >> >> <generator class="identity"/> >>> >> >> </id> >>> >> >> <property name="Name"/> >>> >> >> <property name="SortOrder"/> >>> >> >> <property name="ProductID" /> >>> >> >> </class> >>> >> >> </hibernate-mapping> >>> >>> >> >> well, that's quite simple, and I can insert one Product with two >>> >> >> Properties without problem, but how can I delete the Product with >>> >> >> ID >>> >> >> equals 1? I tried: >>> >>> >> >> session.Delete(new Product { ProductID = 1 }); >>> >> >> session.Flush(); >>> >>> >> >> But NH always want to update the properties' ProductID to NULL, >>> >> >> but >>> >> >> my >>> >> >> schema won't accept the change (for NOT NULL). I tried every >>> >> >> cascade >>> >> >> settings in the <set /> element but always faild by updating. >>> >> >> I just want to execute the SQL like "DELETE FROM Product WHERE >>> >> >> ProductID >>> >> >> = 1" and everything else could be done in database. (e.g., cascade >>> >> >> deletion). >>> >> >> What should I do? >>> >>> >> >> Blog:http://www.cnblogs.com/JeffreyZhao/ >>> >> >> Twitter:http://twitter.com/jeffz_cn >> > >> > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to nhusers@googlegroups.com To unsubscribe from this group, send email to nhusers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---