在 NHibernate 中不是持久性 M:N 与关联 class?
Not persister M:N with association class in NHibernate?
我想映射一个 M:N 产品和购物车之间的关联,以及它们之间的关联 class ProductsOfCart。但是数据不是持久性的,但我不知道你为什么能帮我?
Here is a code from CartControler
Create();
Product productD = new ProductDao().GetById(product);
ProductsOfCart productsOfCart = new ProductsOfCart();
User user = new UserDao().GetByLogin(User.Identity.Name);
Cart cart = new CartDao().GetByUser(user);
cart.Price += productD.Price;
cart.PriceDph += productD.PriceDph;
cart.NumberOfItems++;
ProductsOfCartDao productsOfCartDao = new ProductsOfCartDao();
CartDao cartDao = new CartDao();
CartDao.Update(cart);
productsOfCart.IdCart = cart;
productsOfCart.IdCart = productD;
productsOfCartDao.Create(productsOfCart);
Cart, Product and ProductsOfCart
public class Cart :IEntity
{
public virtual int Id { get; set; }
public virtual double Price { get; set; }
public virtual double PriceDph { get; set; }
public virtual int NumberOfItems { get; set; }
public virtual User IdUser { get; set; }
public virtual IList<Product> Products { get; set; }
}
public class Product : IEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int Price { get; set; }
public virtual string ProductState { get; set; }
public virtual string Maker { get; set; }
public virtual string Description { get; set; }
public virtual int ProductWaranty { get; set; }
public virtual ProductCategory Category { get; set; }
public virtual string ImageName { get; set; }
public virtual IList<Cart> Carts { get; set; }
}
public class ProductsOfCart : IEntity
{
public virtual int Id { get; set; }
public virtual Product IdProduct { get; set; }
public virtual Cart IdCart { get; set; }
}
And here are XML files of Product, Cart and ProductsOfCart
<class name="Product" table="Products">
<id name="Id" column="id_product">
<generator class="native" />
</id>
<property name="Name" column="name" />
<property name="Price" column="price" />
<property name="ProductState" column="productState" />
<property name="Maker" column="maker" />
<property name="Description" column="description" />
<property name="ProductWaranty" column="productWaranty" />
<many-to-one name="Category" column="id_category" foreign-key="id_category" />
<property name="ImageName" column="imageName" />
<bag name="Carts" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="Cart" table="Carts">
<id name="Id" column="id_cart">
<generator class="native" />
</id>
<property name="Price" column="price" />
<property name="PriceDph" column="priceDph" />
<property name="NumberOfItems" column="numberOfItems" />
<many-to-one name="IdUser" column="id_User" foreign-key="id_User" />
<bag name="Products" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_cart" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="ProductsOfCart" table="ProductsOfCart">
<id name="Id" column="id_ProductsOfCart">
<generator class="native" />
</id>
<many-to-one name="IdProduct" column="id_product" foreign-key="id_product"/>
<many-to-one name="IdCart" column="id_cart" foreign-key="id_cart" />
</class>
如果我们想将配对对象创建为实体。我们需要这样的映射:
<class name="Product" table="Products">
... // as is
<bag name="Categories" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="Cart" table="Carts">
... // as is
<bag name="Products" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_cart" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="ProductsOfCart" table="ProductsOfCart">
<many-to-one name="Product" column="id_product" />
<many-to-one name="Cart" column="id_cart" />
</class>
我们的 类 看起来像:
public class Cart :IEntity
{
...
public virtual ILIst<ProductCategory> Products { get; set; }
}
public class Product : IEntity
{
...
public virtual IList<ProductCategory> Categories { get; set; }
}
public class ProductCategory : IEntity
{
...
public virtual Product Product{ get; set; }
public virtual Category Category { get; set; }
}
我更喜欢这种结构。在它之上创建复杂的查询很容易...尝试在此处阅读更多示例 many-to-many with extra columns nhibernate
但我们也可以隐藏配对 table,并使用映射 many-to-many
,这将完全隐藏配对 table 和配对C# 对象
<class name="Product" table="Products">
... // as is
<bag name="Categories" lazy="true"
table="ProductsOfCart" // many-to-many requires table defintion
inverse="true" batch-size="25"
cascade="none"> // cascade would not effect the pair, but Category
// usually not wanted
<key column="id_product" />
<many-to-many class="Category" column="id_product" />
</bag>
</class>
<class name="Cart" table="Carts">
... // as is
<bag name="Products" lazy="true" table="ProductsOfCart"
inverse="false" // one side MIGHT NOT be inverse
batch-size="25"
cascade="none"> // cascade would not effect the pair, but Product
// usually not wanted
<key column="id_cart" />
<many-to-many class="Product" column="id_product" />
</bag>
</class>
我们的 类 看起来像:
public class Cart :IEntity
{
...
public virtual IList<Product> Products { get; set; }
}
public class Product : IEntity
{
...
public virtual IList<Category> Categories { get; set; }
}
在这里阅读关于多对多的内容
延长
如果您没有提供 persiter... 例外情况请确保:
- 所有 hbm 映射文件确实具有后缀
.hbm.xml
(检查任何拼写错误)
- 所有 hbm 映射文件必须是
Embedded resources
- 配置必须包含 (more here):
NH 会话工厂配置
...
<mapping assembly="Project.DomainModel"/> <!-- Here -->
</session-factory>
另外,问题中的最新映射是正确的——而 C# 的东西不是。要拥有此集合映射:
(Product.hbm.xml)
...
<bag name="Carts" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
C# POCO 必须是:
public class Product : IEntity
{
...
// instead of this
// public virtual IList<Cart> Carts { get; set; }
// we need this
public virtual IList<ProductsOfCart> Carts { get; set; }
}
这些信息应该足够 运行,我会说
我想映射一个 M:N 产品和购物车之间的关联,以及它们之间的关联 class ProductsOfCart。但是数据不是持久性的,但我不知道你为什么能帮我?
Here is a code from CartControler
Create();
Product productD = new ProductDao().GetById(product);
ProductsOfCart productsOfCart = new ProductsOfCart();
User user = new UserDao().GetByLogin(User.Identity.Name);
Cart cart = new CartDao().GetByUser(user);
cart.Price += productD.Price;
cart.PriceDph += productD.PriceDph;
cart.NumberOfItems++;
ProductsOfCartDao productsOfCartDao = new ProductsOfCartDao();
CartDao cartDao = new CartDao();
CartDao.Update(cart);
productsOfCart.IdCart = cart;
productsOfCart.IdCart = productD;
productsOfCartDao.Create(productsOfCart);
Cart, Product and ProductsOfCart
public class Cart :IEntity
{
public virtual int Id { get; set; }
public virtual double Price { get; set; }
public virtual double PriceDph { get; set; }
public virtual int NumberOfItems { get; set; }
public virtual User IdUser { get; set; }
public virtual IList<Product> Products { get; set; }
}
public class Product : IEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int Price { get; set; }
public virtual string ProductState { get; set; }
public virtual string Maker { get; set; }
public virtual string Description { get; set; }
public virtual int ProductWaranty { get; set; }
public virtual ProductCategory Category { get; set; }
public virtual string ImageName { get; set; }
public virtual IList<Cart> Carts { get; set; }
}
public class ProductsOfCart : IEntity
{
public virtual int Id { get; set; }
public virtual Product IdProduct { get; set; }
public virtual Cart IdCart { get; set; }
}
And here are XML files of Product, Cart and ProductsOfCart
<class name="Product" table="Products">
<id name="Id" column="id_product">
<generator class="native" />
</id>
<property name="Name" column="name" />
<property name="Price" column="price" />
<property name="ProductState" column="productState" />
<property name="Maker" column="maker" />
<property name="Description" column="description" />
<property name="ProductWaranty" column="productWaranty" />
<many-to-one name="Category" column="id_category" foreign-key="id_category" />
<property name="ImageName" column="imageName" />
<bag name="Carts" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="Cart" table="Carts">
<id name="Id" column="id_cart">
<generator class="native" />
</id>
<property name="Price" column="price" />
<property name="PriceDph" column="priceDph" />
<property name="NumberOfItems" column="numberOfItems" />
<many-to-one name="IdUser" column="id_User" foreign-key="id_User" />
<bag name="Products" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_cart" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="ProductsOfCart" table="ProductsOfCart">
<id name="Id" column="id_ProductsOfCart">
<generator class="native" />
</id>
<many-to-one name="IdProduct" column="id_product" foreign-key="id_product"/>
<many-to-one name="IdCart" column="id_cart" foreign-key="id_cart" />
</class>
如果我们想将配对对象创建为实体。我们需要这样的映射:
<class name="Product" table="Products">
... // as is
<bag name="Categories" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="Cart" table="Carts">
... // as is
<bag name="Products" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_cart" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="ProductsOfCart" table="ProductsOfCart">
<many-to-one name="Product" column="id_product" />
<many-to-one name="Cart" column="id_cart" />
</class>
我们的 类 看起来像:
public class Cart :IEntity
{
...
public virtual ILIst<ProductCategory> Products { get; set; }
}
public class Product : IEntity
{
...
public virtual IList<ProductCategory> Categories { get; set; }
}
public class ProductCategory : IEntity
{
...
public virtual Product Product{ get; set; }
public virtual Category Category { get; set; }
}
我更喜欢这种结构。在它之上创建复杂的查询很容易...尝试在此处阅读更多示例 many-to-many with extra columns nhibernate
但我们也可以隐藏配对 table,并使用映射 many-to-many
,这将完全隐藏配对 table 和配对C# 对象
<class name="Product" table="Products">
... // as is
<bag name="Categories" lazy="true"
table="ProductsOfCart" // many-to-many requires table defintion
inverse="true" batch-size="25"
cascade="none"> // cascade would not effect the pair, but Category
// usually not wanted
<key column="id_product" />
<many-to-many class="Category" column="id_product" />
</bag>
</class>
<class name="Cart" table="Carts">
... // as is
<bag name="Products" lazy="true" table="ProductsOfCart"
inverse="false" // one side MIGHT NOT be inverse
batch-size="25"
cascade="none"> // cascade would not effect the pair, but Product
// usually not wanted
<key column="id_cart" />
<many-to-many class="Product" column="id_product" />
</bag>
</class>
我们的 类 看起来像:
public class Cart :IEntity
{
...
public virtual IList<Product> Products { get; set; }
}
public class Product : IEntity
{
...
public virtual IList<Category> Categories { get; set; }
}
在这里阅读关于多对多的内容
延长
如果您没有提供 persiter... 例外情况请确保:
- 所有 hbm 映射文件确实具有后缀
.hbm.xml
(检查任何拼写错误) - 所有 hbm 映射文件必须是
Embedded resources
- 配置必须包含 (more here):
NH 会话工厂配置
...
<mapping assembly="Project.DomainModel"/> <!-- Here -->
</session-factory>
另外,问题中的最新映射是正确的——而 C# 的东西不是。要拥有此集合映射:
(Product.hbm.xml)
...
<bag name="Carts" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
C# POCO 必须是:
public class Product : IEntity
{
...
// instead of this
// public virtual IList<Cart> Carts { get; set; }
// we need this
public virtual IList<ProductsOfCart> Carts { get; set; }
}
这些信息应该足够 运行,我会说