views:

558

answers:

1

I Have this Product Class i took from the examples of how to use nhibernate (but i did some modifications to it) I want to be able to map a class and all of it's references into the DB.

[NHibernate.Mapping.Attributes.Class(Lazy=true)]
    public class Product
    {             
        [NHibernate.Mapping.Attributes.Id(Name="Id",TypeType=typeof(Guid))]
        public virtual Guid Id { get; set; }

        [NHibernate.Mapping.Attributes.Property]
        public virtual string Name { get; set; }

        [NHibernate.Mapping.Attributes.Property]
        public virtual int Category { get; set; }

        [NHibernate.Mapping.Attributes.Property]
        public virtual bool Discontinued { get; set; }

        [NHibernate.Mapping.Attributes.Map(Name="Items")]
        public virtual Dictionary<Guid, SubItem> Products { get; set; }
    }

where subitem is defined as follows:

  [NHibernate.Mapping.Attributes.Class(Lazy = true)]
    public class SubItem
    {
        [NHibernate.Mapping.Attributes.Id(Name = "Id", TypeType = typeof(Guid))]
        public virtual Guid Id { get; set; }

        [NHibernate.Mapping.Attributes.Property]
        public virtual string Name { get; set; }              
    }

and i get the following exeption yielding that "The element 'map' in namespace 'urn:nhibernate-mapping-2.2' has incomplete content". probably something it missing overthere but i don't know what. and if it is the right way to do it. ( i want to be able to serialize the following object:

SubItem sub1 = new SubItem();
sub1.Id = Guid.NewGuid();
sub1.Name = "sub1";
SubItem sub2 = new SubItem();
sub2. Id  = Guid.NewGuid();
sub2.Name = "sub2";

Product p= new Product();
p.Name = "itay";
p.Category = 111;
p.Discontinued = false;
p.Id = Guid.NewGuid();

Product p1= new Product();
p1.Name = "Joe";
p1.Category = 222;
p1.Discontinued = true;
p1.Id = Guid.NewGuid();

p.Products = new System.Collections.Generic.Dictionary<Guid, SubItem>();
p.Products.Add(sub1.Id, sub1);
p.Products.Add(sub2.Id, sub2);
p1.Products = new System.Collections.Generic.Dictionary<Guid, SubItem>();
p1.Products.Add(sub1.Id,sub1);

i wonder how will NHIBERNATE present this situation in the DB ( will he create a diffrent table for SubItem objects? how will he treat the Products property with the Map Attribute on it....

Edit: i want to be able to create a ManyToMany mapping - which in the DB will become a different table with the relevant Id's (ProductID,SubItemId)

Help will be appreciated!

A: 

You need to provide the Key and Index information for the Map. Your attributes would look something like...

    [Map(0, Inverse = true)]
    [Key(1, Column = "ProductId")]
    [Index(2, Column = "SubItemId", TypeType = typeof(Guid))]
    [OneToMany(3, ClassType = typeof(SubItem))]
    public virtual Dictionary<Guid, SubItem> Products { get; set; }
g .
I wasn't sure what column names you were using so I made up reasonable ones to illustrate. It sounds like you want to replace ProductId with Id. You shouldn't need a ManyToMany unless a SubItem can belong to multiple products.
g .
If i use the ManyToMany Attribute - i need to also define a new class? in order to define the relevant ClassType? or it can be done automatically? The SubItem can belong to multiple products (the names of the classes i defined aren't very good because it is just an example...)
Also if this specific subitem belongs to multiple Products, i want that if i make a change in that item - all the products containing that item will notice that change.
Maybe it would be best to revise the question with the additional information about the ManyToMany scenario.
g .
10x for the advice. is the ManyToMany Attribute is the correct solution for my purpose?

related questions