2013-04-06 42 views
0

这里是我的映射ComplexDishNHibernate的不保存hasManyToMany数据

public class ComplexMapping:ClassMap<Complex> 
    { 
     public ComplexMapping() 
     { 
      Table("ComplexTable"); 

      Id(comp => comp.Id,"ComplexId").GeneratedBy.Identity(); 
      Map(comp => comp.Name,"Name").Not.Nullable(); 
      Map(comp => comp.Subscribe, "DescriptionComplex"); 

      HasManyToMany(comp => comp.ScrollOfDish) 
       .Table("ComplexDish") 
       .ParentKeyColumn("ComplexId") 
       .ChildKeyColumn("DishId").Cascade.All(); 

     } 
    } 

    public class DishMapping:ClassMap<Dish> 
    { 
     public DishMapping() 
     { 
      Table("DishTable"); 

      Id(dish => dish.Id, "DishId").GeneratedBy.Identity(); 

      Map(dish => dish.Name); 
      Map(dish => dish.Description); 
      Map(dish => dish.Price); 

      References(x => x.Category, "CategoryId").Cascade.None(); 

      HasManyToMany(comp => comp.Scroll) 
       .Table("ComplexDish") 
       .ParentKeyColumn("DishId") 
       .ChildKeyColumn("ComplexId").Inverse(); 

     } 
    } 

我使用DAO模式 - 当从前端数据来创建需要的对象

enter image description here

和对象保存但不是整个对象只有名称和描述已保存,但收集产品不保存。我想我忘了一些简单的事情,请帮助我。

回答

0

通常对我而言,多对多表示实体本身管理其生命周期的两个独立实体之间的关联。

例如。在您的情况

var firstDish = new Dish(); 
var secondDish = new Dish(); 

// 01 -- both dish objects are now attached to the session 
session.SaveOrUpdate(firstDish); 
session.SaveOrUpdate(secondDish); 


var firstComplexObject = new Complex(); 
firstComplexObject.ScrollOfDish.Add(firstDish); 
firstComplexObject.ScrollOfDish.Add(secondDish); 

// 02 -- first Complex Object now attached to the session 
session.SaveOrUpdate(firstComplextObject); 

var secondComplexObject = new Complex(); 
secondComplexObject.ScrollOfDish.Add(firstDish); 
secondComplexObject.ScrollOfDish.Add(secondDish); 

// 03 -- second Complex Object now attached to the session 
session.SaveOrUpdate(secondComplextObject); 

我会避免复杂的对象管理菜对象的生命周期一样

var firstDish = new Dish(); 
var secondDish = new Dish(); 

var firstComplexObject = new Complex(); 
firstComplexObject.ScrollOfDish.Add(firstDish); 
firstComplexObject.ScrollOfDish.Add(secondDish); 


// the dish object are not attached to session 
// hence the NHibernate has to save the entire object graph here!!!! 
// 01 -- first Complex Object now attached to the session 
session.SaveOrUpdate(firstComplextObject); 

var secondComplexObject = new Complex(); 
secondComplexObject.ScrollOfDish.Add(firstDish); 
secondComplexObject.ScrollOfDish.Add(secondDish); 

// 02 -- second Complex Object now attached to the session 
session.SaveOrUpdate(secondComplextObject); 

此外,由于菜肯定会两个复杂对象之间共享,这将使意义不级联从复杂项目删除到菜。

因此,我会确保您分开管理生命周期。 希望这会把你推向正确的方向。

+0

非常感谢。问题出在这方面,我已经用你的帮助解决了它) – 2013-04-08 05:02:45