0

How to make many to many relationships in hibernate same table Example

I have a model name Product i want to add more Products

want to create extra table where there will two Fields producid and product2 M-to-Many

public class Product
{
    public virtual int Id { get; set; }
    public virtual IList<Product> ManyProduct { get; set; }
}

Mapping

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Id(x => x.Id);
        Map(x => x.ImageUrl);
        HasManyToMany(x => x.ManyProduct)
            .Cascade.All()
            .Table("ProductInProduct");
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94
smart boy
  • 671
  • 4
  • 11
  • 24

1 Answers1

0

specify the keycolumns manually

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Id(x => x.Id);
        Map(x => x.ImageUrl);
        HasManyToMany(x => x.ManyProduct)
            .ParentKeyColumn("product1_id")
            .ChildKeyColumn("product2_id")
            .Cascade.All()
            .Table("ProductInProduct");
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94