【发布时间】:2011-10-04 03:20:01
【问题描述】:
基本问题:我有一个名为 ProductOffering 的查找实体的多对多关系(商店、产品)。在我有一个持久的 StoreLocation 并尝试附加瞬态产品的情况下,即使我不想保存,休眠也会抱怨。
问题:
- 为什么 grails/gorm/Hibernate 试图保存我的实体?
- 有没有更好的方法来解决这个问题?
我的域类如下所示:
class StoreLocation {
String name
List offerings
static hasMany = [offerings:ProductOffering]
static constraints = {
}
}
class Product {
String name
static hasMany = [offeredBy:ProductOffering]
static constraints = {
name(unique:true)
offeredBy(nullable:true)
}
}
class ProductOffering {
static belongsTo = [product:Product, store:StoreLocation]
static constraints = {
}
}
我的 StoreLocationController “编辑”操作如下所示。
def edit = {
//get the store we want to edit
def storeLocation = StoreLocation.get(params.id)
//create a transient product and add it to the store
// by attaching it to a transient productOffering
def product = new Product()
def offering = new ProductOffering()
offering.product = product
storeLocation.addToOfferings(offering)
//render the edit page
render(view:"edit", model:[storeLocation:storeLocation])
}
假设我有一个 ID=1 的 storeLocation。我去编辑 StoreLocation 本地主机:8080/myapp/storeLocation/edit/1
我的观点是什么样的并不重要。可能是<p>hello</p>
我收到以下错误。
错误 500:非空属性引用空值或瞬态值:ProductOffering.product;嵌套异常是 org.hibernate.PropertyValueException:非空属性引用空值或瞬态值:ProductOffering.product
堆栈跟踪并没有多大用处:
org.springframework.dao.DataIntegrityViolationException:非空属性引用空值或瞬态值
【问题讨论】:
标签: hibernate grails grails-orm