插入与另一个对象具有一对一关系的对象时出现Nhibernate错误
当我尝试插入一个与另一个对象具有一对一关系的对象时,nhibernate有问题。有一个类叫做Article 映射文件插入与另一个对象具有一对一关系的对象时出现Nhibernate错误,nhibernate,Nhibernate,当我尝试插入一个与另一个对象具有一对一关系的对象时,nhibernate有问题。有一个类叫做Article 映射文件 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> Object someArticle已插入,但Object someSpecialArticle的insert语句为: 插入tbl(价
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
Object someArticle已插入,但Object someSpecialArticle的insert语句为:
插入tbl(价格)值(@p0);选择范围_标识()
用于指定父项目的项目id在哪里
thx的正确映射是:
<many-to-one name="Article" fetch="join" cascade="all" unique="true" />
当您在NHibernate中创建一对一映射时,必须将一侧映射为多对一-这将是负责存储外键的一侧。因为这里有单向关系,所以必须将其映射为多对一,并使用唯一约束使关系的行为类似于一对一从特殊文章到文章使用多对一关系会更容易。一对一的关系可能会很棘手,而且很复杂
在您的情况下,让SpecialArticle扩展文章似乎是一个合适的解决方案。然后,您可以使用获取第二个表中某个特殊项目所需的附加信息。这是与相同的问题吗?不是-但都是从那里开始的
<id name="ID" column="id" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Price" column="Price" type="Double" not-null="true" />
<one-to-one name="Article" class="..." fetch="join" cascade="all" />
public class SpecialArticle
{
/// <summary>
/// Get/Set the ID for this question
/// </summary>
public virtual int ID
{
get;
set;
}
/// <summary>
/// Get/Set the expected price for this question or answer
/// </summary>
public virtual Double Price
{
get;
set;
}
/// <summary>
/// Get/Set the article that contains the rest of the information for this question
/// </summary>
public virtual Article Article
{
get;
set;
}
}
// Populate someArticle
// Populate someSpecialArticle
// Following code is wrapped in a transaction
someSpecialArticle.Article = someArticle;
Session.SaveOrUpdate(someArticle);
Session.SaveOrUpdate(someSpecialArticle);
<many-to-one name="Article" fetch="join" cascade="all" unique="true" />