我已经阅读了很多帖子和文档,并且一定遗漏了一些东西。
在我的应用程序(下面的模型)中,我遇到了一个似乎超出我控制范围的数据问题,我在连接表 JOBORDERCATEGORIES 中有一个 categoryId,在 CATEGORY 表中没有相应的行。我正在通过 JobOrder 中的 getJobCategories() 访问类别数据。当我的 Category 表缺少引用的行时,这会产生以下错误:
2012-03-07 08:02:10,223 [quartzScheduler_Worker-1] ERROR listeners.SessionBinderJobListener - Cannot flush Hibernate Sesssion, error will be ignored
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.matrixres.domain.Category#416191]
我的代码正在停止。
我曾尝试使用 ignoreNotFound ,但它并没有帮助我克服上述错误。
如果我错过了有关此问题解决方案的帖子,请将我链接到该帖子,否则欢迎就如何前进提出想法。也许我必须敲定一条更直接的途径来实现获得一个好的类别列表的目标,但我对框架不够熟悉,不知道下一步是什么。作为说明,我不能写任何这些表。
谢谢,有钱
我的模型的简化版本:
工单对象:
class JobOrder {
def getJobCategories() {
def cats = []
try {
def jocategories = this.categories
if(!jocategories.isEmpty() && jocategories!=null){
println "we got categories for ${this.id}"
jocategories.each { cat ->
if(cat?.parentCategoryID == 0){
if(cat.occupation != null){
cats << cat.occupation
} else {
cats << cat.name
}
}
}
}
} catch(e) {
cats << "Other Area(s)"
}
cats
}
static mapping = {
table 'dbo.JOBORDER'
version false
id generator: 'identity', column: 'JOBORDERID'
/*
* several other mapped columns deleted here
*/
categories joinTable:[name:'jobOrderCategories', column: 'categoryId', key:'jobOrderID']
}
/*
* several properties deleted here
*/
static hasMany = [categories: Category] //several other hasMany associations exist
}
类别对象:
class Category {
static mapping = {
table 'CATEGORY'
version false
id generator: 'identity', column: 'categoryID'
occupation column: 'OCCUPATION'
name column: 'NAME'
parentCategoryID column: 'PARENTCATEGORYID'
/*
* several other mapped columns deleted here
*/
jobOrders joinTable:[name:'jobOrderCategories', column: 'jobOrderID', key:'categoryId']
}
String name
String occupation
int parentCategoryID
/*
* several properties deleted here
*/
static belongsTo = [JobOrder]
static hasMany = [jobOrders:JobOrder]
}
加入表:
class JobOrderCategories {
static mapping = {
table 'JOBORDERCATEGORIES'
version false
isDeleted column: 'ISDELETED'
jobOrderID column: 'JOBORDERID'
categoryId column: 'CATEGORYID'
}
Boolean isDeleted
Integer jobOrderID
Integer categoryId
}