有2个表 WorkForm(one)和WorkFormDetail(many)
用NH插入数据的时候无法将数据插入WorkFormDetail
1.WorkForm映射文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Test.Domain" namespace="Test.Domain.Entities">
<class name="WorkForm" table="WorkForm">
<id name="WorkFormId" column="WorkFormId" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Title" column="Title" type="string" length="100" not-null="true"/>
<property name="Content" column="[Content]" type="string" length="500" not-null="true"/>
<set generic="true" inverse="true" name="WorkFormDetails" table="WorkFormDetail">
<key column="WorkFormId" foreign-key="FK_WorkFormDetail_WorkForm"/>
<one-to-many class="Test.Domain.Entities.WorkFormDetail"/>
</set>
</class>
</hibernate-mapping>
2.WorkFormDetail映射文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Test.Domain" namespace="Test.Domain.Entities">
<class name="WorkFormDetail" table="WorkFormDetail">
<id name="FormDetailId" column="FormDetailId" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Title" column="Title" type="string" length="100" not-null="true"/>
<property name="Content" column="[Content]" type="string" length="500" not-null="true"/>
<many-to-one foreign-key="FK_WorkFormDetail_WorkForm" class="Test.Domain.Entities.WorkForm"
column="WorkFormId" name="WorkForm"/>
</class>
</hibernate-mapping>
3.WorkForm持久类
public class WorkForm
{
private ISet<WorkFormDetail> _workFormDetails = new HashedSet<WorkFormDetail>();
public virtual int WorkFormId { get; set; }
public virtual string Title { get; set; }
public virtual string Content { get; set; }
public virtual ISet<WorkFormDetail> WorkFormDetails
{
get
{
return _workFormDetails;
}
set
{
_workFormDetails = value;
}
}
}
4.WorkFormDetail持久类
public class WorkFormDetail
{
public virtual int FormDetailId { get; set; }
public virtual string Title { get; set; }
public virtual string Content { get; set; }
public virtual WorkForm WorkForm { get; set; }
}
5.问题来了,下面是插入数据的代码
protected void Submit_Click(object sender, EventArgs e)
{
string pTitle = txtTitle.Text.Trim();
string pContent = txtContent.Text.Trim();
string cTitle = txtCTitle.Text.Trim();
string cContent = txtCContent.Text.Trim();
WorkForm wf = new WorkForm();
wf.Title = pTitle;
wf.Content = pContent;
WorkFormDetail wfd = new WorkFormDetail();
wfd.Title = cTitle;
wfd.Content = cContent;
wfd.WorkForm = wf;
wf.WorkFormDetails.Add(wfd);
try
{
_session.Save(wf);
_session.Flush();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
希望有人帮忙看下,一大早上碰到这问题,很纠结...
我的也不行 ,先留个足,解决了告知一下....
嗨你的可以了吗,我的还是不行,是咋回事了
在WorkForm类中添加以下方法:
///<summary>
/// 增加一个工作单明细
///</summary>
///<param name="wf"></param>
public virtual void AddWorkFormDetails(WorkFormDetail wf)
{
this.WorkFormDetails.Add(wf);
wf.WorkForm = this;
}
难道这个方法一定要是virtual的吗?去掉virtual关键字就报错.
然后逻辑代码中
WorkForm wf = new WorkForm();
wf.Title = "title";
wf.Content = "content";
WorkFormDetail wfd = new WorkFormDetail();
wfd.Title = "child title";
wfd.Content = "chile content";
wf.AddWorkFormDetails(wfd);
_session.save(wf);
这样可以级联添加了,怎么感觉和之前的没什么差别呢,但是有添加成功了。。。
嗨你的可以了吗,我的还是不行,是咋回事了
其实没有必要写那个virtual方法,只要把WorkForm.hbm.xml文件中set节点中的inverse="true"换成cascade="all"就可以了,其他的都不需要修改……
public class Order { public Order() { this.Items = new Iesi.Collections.Generic.HashedSet<OrderItem>(); //it is ok too //this.Items = new List<OrderItem>(); } public virtual Guid ID { get; set; } public virtual Iesi.Collections.Generic.ISet<OrderItem> Items { get; set; } //it is ok too //public virtual ICollection<OrderItem> Items { get; set; } //public virtual Customer Customer { get; set; } public virtual DateTime CreateTime { get; set; } public virtual DateTime ModifyTime { get; set; } public virtual int Version { get; set; } public virtual void AddItem(int amount) { var now = DateTime.Now; var item = new OrderItem() { CreateTime = now, ModifyTime = now, Amount = amount, Order = this }; this.Items.Add(item); } }
OrderItem
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Andyshi.NH3.Domain { public class OrderItem { public virtual int Amount { get; set; } public virtual Order Order { get; set; } //public virtual int Version { get; set; } public virtual DateTime CreateTime { get; set; } public virtual DateTime ModifyTime { get; set; } public override bool Equals(object obj) { var item = obj as OrderItem; return this.Amount == item.Amount && this.CreateTime == item.CreateTime && this.ModifyTime == item.ModifyTime; } public override int GetHashCode() { return base.GetHashCode(); } } }
Order.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Com.Andyshi.NH3" namespace="Com.Andyshi.NH3.Domain"> <class name="Order" table="Orders" optimistic-lock="version"> <id name="ID"> <generator class="guid"></generator> </id> <version name="Version" unsaved-value="null"/> <property name="CreateTime"></property> <property name="ModifyTime"/> <set name="Items" generic="true" inverse="true" cascade="all" > <key column="OrderID" not-null="true"></key> <one-to-many class="OrderItem"/> </set> </class> </hibernate-mapping>
OrderItem.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Com.Andyshi.NH3" namespace="Com.Andyshi.NH3.Domain"> <class name="OrderItem" table="OrderItems" optimistic-lock="version" > <composite-id> <!--<version name="Version" unsaved-value="null"></version>--> <key-property name="Amount"></key-property> <key-property name="CreateTime"></key-property> <key-property name="ModifyTime"/> </composite-id> <many-to-one name="Order" column="OrderID" not-null="true" class="Order" ></many-to-one> </class> </hibernate-mapping>
Repository
public void Add(Order order) { using (var session = SessionManager.Instance.CreateSession()) { using (var tran = session.BeginTransaction()) { session.Save(order); tran.Commit(); } } }
Test
[Test] public void test_add() { var now = DateTime.Now; var order = new Order() { CreateTime = now, ModifyTime = now }; order.AddItem(100); order.AddItem(200); var repository = new OrderRepository(); repository.Add(order); var orderindb = repository.Get(order.ID); Assert.IsNotNull(orderindb); }