- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到以下异常。
NHibernate.PropertyValueException:非空属性引用空值或 transient
这是我的映射文件。
产品
<class name="Product" table="Products">
<id name="Id" type="Int32" column="Id" unsaved-value="0">
<generator class="identity"/>
</id>
<set name="PriceBreaks" table="PriceBreaks" generic="true" cascade="all" inverse="true" >
<key column="ProductId" />
<one-to-many class="EStore.Domain.Model.PriceBreak, EStore.Domain" />
</set>
</class>
<class name="PriceBreak" table="PriceBreaks">
<id name="Id" type="Int32" column="Id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="ProductId" column="ProductId" type="Int32" not-null="true" />
<many-to-one name="Product" column="ProductId" not-null="true" cascade="all" class="EStore.Domain.Model.Product, EStore.Domain" />
</class>
[Test]
public void Can_Add_Price_Break()
{
IPriceBreakRepository repo = new PriceBreakRepository();
var priceBreak = new PriceBreak();
priceBreak.ProductId = 19;
repo.Add(priceBreak);
Assert.Greater(priceBreak.Id, 0);
}
public int AddPriceBreak(Product product, PriceBreak priceBreak)
{
using (ISession session = EStore.Domain.Helpers.NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
product.AddPriceBreak(priceBreak);
session.SaveOrUpdate(product);
transaction.Commit();
}
return priceBreak.Id;
}
最佳答案
从映射和 PriceBreak 类中删除 ProductId 属性。并使用 PriceBreaks 集合添加 PriceBreaks,您不需要 PriceBreakRepository,而只需要 ProductRepository。
例子:
using (var session = sessionFactory.OpenSession())
{
using (var tx = session.BeginTransaction())
{
var product = session.Get<Product>(19);
product.AddPriceBreak(new PriceBreak());
tx.Commit();
}
}
class Product
{
// ...
public void AddPriceBreak(PriceBreak pb)
{
pb.Product = this;
PriceBreaks.Add(pb);
}
}
关于NHibernate.PropertyValueException : not-null property references a null or transient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2960171/
我有这些 Pojo: public class ParticipantPojo implements Serializable ..... @OneToMany(mappedBy = "partici
使用 Hibernate 5 作为 ORM。 我过去认为 optional=true 意味着该列可以保存空值。 我有两个实体 -: 选修 选修实践 选修课可能会或可能不会与选修实践相关联,但选修实践必
通过执行 mvn clean package,我看到一个奇怪的 Maven 输出: [ERROR] /home/user/Desktop/mycompany/myapp/server/src/main
您好,我正在尝试保留我的一个实体“角色”,它通过连接表与我的另一个实体“用户”具有多对一关系。但是,每次我尝试在没有附加任何用户的情况下插入角色时,都会收到以下错误: org.hibernate.Pr
我收到以下异常。 NHibernate.PropertyValueException:非空属性引用空值或 transient 这是我的映射文件。 产品
我是 JPA 新手,在尝试保存事务数据时遇到了上述错误。 这是我的实体: @Entity @Table(name = "transaction1") @NamedQueries({ @
我想将一个对象保存到我的子类 ArticleZoning 中,其父类(super class) Zoning 包含类 zoneData 列表,其中还包含类 ZoneCoordinate。当我保存我的子
您好,我目前正在开发小应用程序来练习 hibernate 。我正在尝试建立一对多关系,但我陷入困境。 我有 2 门类(class)的问题和答案: package com.sample; import
Here you can see the entity-relationship diagram of my database with associations 我正在 NetBeans 中使用 A
我对 hibernate 和 restSpring 很陌生 我已经使用 hibernate 工具从数据库生成了Country和State实体 我的数据库看起来像这样国家表,country_id为PK
我是一名优秀的程序员,十分优秀!