开发者

Transiend Exception NHibernate

开发者 https://www.devze.com 2023-03-21 18:44 出处:网络
I have 3 tables: Employee {PK: EmployeeId, name, lastname} Project {PK: ProjectId, name, description} EmployeebyProject {PK:EmployeeId :int, PK:ProjectId :Project, DateBegin :int, DateEnd : DateTim

I have 3 tables:

Employee {PK: EmployeeId, name, lastname} Project {PK: ProjectId, name, description}

EmployeebyProject {PK:EmployeeId :int, PK:ProjectId :Project, DateBegin :int, DateEnd : DateTime}

I need make some CRUD's in this table.

for now I need Insert, by the way in开发者_高级运维 the software in nhibernate the class EmployeebyProject have the object Employee and Project instead of EmployeeId and ProjectId.

This is the mapping

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="AdminProject"
                   namespace="AdminProject.Business.Entity">

  <class name="EmployeebyProject">

    <composite-id>
      <key-many-to-one name="Employee" column="EmployeeId"  class="Employee"></key-many-to-one>
      <key-many-to-one name="Project" column="ProjectId"  class="Project" ></key-many-to-one>
    </composite-id>


    <property name="DateBegin"/>
    <property name="DateEnd"/>

  </class>

</hibernate-mapping>

The problem is when I try to save occurs the NHibernate.TransientObjectException. How can I avoid it?


A TransientObjectException means that you've told Hibernate to save some object, but that object references another object which you haven't saved. There are generally two ways to fix that:

  1. Manually save the other object before the enclosing transaction is committed.
  2. Add cascading to the relationship so that when you save the first object, the other object is saved, too, just be being referenced from the first one.
0

精彩评论

暂无评论...
验证码 换一张
取 消