具有通用基类的 NHibernate 映射
我正在使用 Fluent NHibernate 来映射我的实体,并且我已经找到了一组正在映射的探针。我尝试了 SubclassMap 的不同变体,但似乎无法正确映射购物车、项目或订单。 Item、CartItem、OrderItem 和 ProjectItem 映射良好。我对 NHibernate 相当陌生,我只是不知道如何告诉它做我想做的事。
编辑:这是我要使用此继承结构的架构:
public abstract class Item
{
public virtual int Id {get;set;}
public virtual decimal Price {get;set;}
}
public class CartItem : Item
{
public virtual string ProjectName {get;set;}
}
public class OrderItem : Item
{
public virtual IList<Payment> Payments {get;set;}
}
public class ProjectItem : Item
{
public virtual string ProjectName {get;set;}
}
public abstract class ItemCollection<T> where T: Item
{
public virutal IList<T> Items {get;set;}
}
public abstract class CommerceCollection<T> : ItemCollection<T> where T : Item
{
public virtual decimal Total {get;set;}
}
public class Project : ItemCollection<ProjectItem>
{
public virtual string ProjectName {get;set;}
}
public class Cart : CommerceCollection<CartItem> {}
public class Order : CommerceCollection<OrderItem>
{
public virtual string OrderTrackingNumber {get;set;}
}
/*------- MAPS -------*/
public class ItemMap: ClassMap<Item>
{
public ItemMap()
{
MapId();
Map(x=> x.Price);
}
}
public class OrderItemMap : SubclassMap<OrderItem>
{
public OrderItemMap()
{
Map(x => x.OrderItemProperty);
}
}
public class CartItemMap : SubclassMap<CartItem>
{
public CartItemMap()
{
Map(x => x.CartItemProperty);
}
}
public class ProjectItemMap : SubclassMap<ProjectItem>
{
public ProjectItemMap()
{
Map(x => x.ProjectItemProperty);
}
}
public class CartMap : ClassMap<Cart>
{
public CartMap()
{
Map(x => x.Total);
HasMany(x => x.Items);
}
}
public class OrderMap : ClassMap<Order>
{
public OrderMap()
{
Map(x => x.Total);
HasMany(x => x.Items);
}
}
I'm using Fluent NHibernate to map my entities and I've come to a set that I'm having probelsm gettting mapped. I've tried different variations of SubclassMap but I can't seem to get Cart, Project or Order mapped correctly. Item, CartItem, OrderItem and ProjectItem map fine. I'm fairly new to NHibernate and I just can't figure out how to tell it to do what I want.
EDIT: This is the schema I'm going for with this inheritance structure:
public abstract class Item
{
public virtual int Id {get;set;}
public virtual decimal Price {get;set;}
}
public class CartItem : Item
{
public virtual string ProjectName {get;set;}
}
public class OrderItem : Item
{
public virtual IList<Payment> Payments {get;set;}
}
public class ProjectItem : Item
{
public virtual string ProjectName {get;set;}
}
public abstract class ItemCollection<T> where T: Item
{
public virutal IList<T> Items {get;set;}
}
public abstract class CommerceCollection<T> : ItemCollection<T> where T : Item
{
public virtual decimal Total {get;set;}
}
public class Project : ItemCollection<ProjectItem>
{
public virtual string ProjectName {get;set;}
}
public class Cart : CommerceCollection<CartItem> {}
public class Order : CommerceCollection<OrderItem>
{
public virtual string OrderTrackingNumber {get;set;}
}
/*------- MAPS -------*/
public class ItemMap: ClassMap<Item>
{
public ItemMap()
{
MapId();
Map(x=> x.Price);
}
}
public class OrderItemMap : SubclassMap<OrderItem>
{
public OrderItemMap()
{
Map(x => x.OrderItemProperty);
}
}
public class CartItemMap : SubclassMap<CartItem>
{
public CartItemMap()
{
Map(x => x.CartItemProperty);
}
}
public class ProjectItemMap : SubclassMap<ProjectItem>
{
public ProjectItemMap()
{
Map(x => x.ProjectItemProperty);
}
}
public class CartMap : ClassMap<Cart>
{
public CartMap()
{
Map(x => x.Total);
HasMany(x => x.Items);
}
}
public class OrderMap : ClassMap<Order>
{
public OrderMap()
{
Map(x => x.Total);
HasMany(x => x.Items);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您的映射的简化版本吗?如果不是:
.KeyColumn("Cart_id")
这样的调用。Is this a simplified version of your mapping? If not:
KeyColumn("Item_id");
(this is needed to get a table-per-subclass mapping)..KeyColumn("Cart_id")
.我明白了我需要做什么。对于实现通用基类 (CommerceCollection) 的类,我必须为通用集合添加一个映射及其参数,然后在子类上指定 KeyColumn 属性。
这是新的 CartMap:
Nhibernate 正在生成如下所示的 SQL,该 SQL 会在列名中的 ' 上失败。
I figured out what I needed to do. For the classes that implemented the generic base class (CommerceCollection) I had to add a map for the generic collection with it's argument and then specify the KeyColumn property on the subclass.
Here is the new CartMap:
Nhibernate was generating SQL like below which would fail on the ' in the column name.