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:
- Manually save the other object before the enclosing transaction is committed.
- 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.
精彩评论