Hi,
I am not an expert on nhibernate, but I had your problem as well.
What I did was to redefine methods Equals and HashCode.
In Equal method tried to find a key which has a meaning for your model.
This allow you to identify an intemImage in the item.Images collection.

In a way nhibernate knows about about the id for itemImage cause when
you delete an itemImage from the collection
the sql generated by nhibernate include the id of the itemImage you
want to delete.

Reading what I just wrote here sound a bit confusing but, hope it helps.

William


On Wed, Jan 6, 2010 at 6:34 PM, Michael Vodep <[email protected]> wrote:
> Hi
>
> I have done some examples from "NHibernate in Action" and got a
> strange problem:
>
> * * * * * * * * * * * * * * * * * * * * * * * * * * *  ~~ CODE ~~ * *
> * * * * * * * * * * * * * * * * * * * * * * * * *
> namespace ConsoleApplication3
> {
>    public class Item
>    {
>        public virtual Int64 ItemId { get; set; }
>        public virtual string Name { get; set; }
>
>        private IList<ItemImage> images = new List<ItemImage>();
>        public virtual IList<ItemImage> Images
>        {
>            get { return this.images; }
>            set { this.images = value; }
>        }
>    }
> }
>
> <?xml version="1.0" encoding="utf-8" ?>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> assembly="ConsoleApplication3" namespace="ConsoleApplication3">
>  <class name="Item" table="item">
>    <id name="ItemId" type="Int64" column="itemid">
>      <generator class="native"/>
>    </id>
>    <property name="Name" column="name" />
>
>    <idbag name="Images"
>      lazy="true"
>      table="item_image">
>      <collection-id type="Int64" column="id">
>        <generator class="native"/>
>      </collection-id>
>      <key column="itemid"/>
>      <composite-element class="ConsoleApplication3.ItemImage,
> ConsoleApplication3">
>        <parent name="Item"/>
>        <property name="Filename" column="filename" not-null="true"/>
>        <property name="Zeitpunkt" column="zeitpunkt" not-null="true"/
>>
>        <property name="Menge" column="menge" not-null="true"/>
>      </composite-element>
>    </idbag>
>
>  </class>
> </hibernate-mapping>
>
> namespace ConsoleApplication3
> {
>    public class ItemImage
>    {
>        public virtual Int64 Id { get; set; }
>        public virtual Item Item { get; set; }
>        public virtual string Filename { get; set; }
>        public virtual DateTime Zeitpunkt { get; set; }
>        public virtual int Menge { get; set; }
>    }
> }
>
> <?xml version="1.0" encoding="utf-8" ?>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> assembly="ConsoleApplication3" namespace="ConsoleApplication3">
>  <class name="ItemImage" table="item_image">
>    <id name="Id" type="Int64" column="id">
>      <generator class="native"/>
>    </id>
>
>    <property name="Filename" column="filename" not-null="true"/>
>    <property name="Zeitpunkt" column="zeitpunkt" not-null="true"/>
>    <property name="Menge" column="menge" not-null="true"/>
>  </class>
> </hibernate-mapping>
>
> * * * * * * * * * * * * * * * * * * * * * * * * * * *  ~~ CODE END ~~
> * * * * * * * * * * * * * * * * * * * * * * * * * * *
>
> If i do a simple insert and retreive the data:
>
> * * * * * * * * * * * * * * * * * * * * * * * * * * *  ~~ CODE ~~ * *
> * * * * * * * * * * * * * * * * * * * * * * * * *
> Item item = new Item();
>
> item.Name = "TEST";
>
> ItemImage img1 = new ItemImage() { Filename = Guid.NewGuid().ToString
> (), Menge = 99, Zeitpunkt = DateTime.Now };
> ItemImage img2 = new ItemImage() { Filename = Guid.NewGuid().ToString
> (), Menge = 105, Zeitpunkt = DateTime.Now + TimeSpan.FromMinutes(10)};
>
> item.Images.Add(img1);
> item.Images.Add(img2);
>
> session.Save(item);
> trans.Commit();
>
> Item item2 = session.Get<Item>(item.ItemId);
>
> foreach (ItemImage i in item2.Images)
> {
>    Console.WriteLine(i.Id + " " + i.Menge + " " + i.Zeitpunkt + " " +
> i.Filename);
> }
>
> Console.ReadKey();
>
> * * * * * * * * * * * * * * * * * * * * * * * * * * *  ~~ CODE END ~~
> * * * * * * * * * * * * * * * * * * * * * * * * * * *
>
> In the foreach loop the Id of ItemImage is not set. So if i use it as
> a POCO i have no way to do a UPDATE because i lost the IDs. Does
> anybody know how to fix that?
>
> Thanks a lot
> Best regards
> Michael
>
> --
> You received this message because you are subscribed to the Google Groups 
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/nhusers?hl=en.
>
>
>
>
-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.


Reply via email to