我已经阅读了很多帖子和文档,肯定遗漏了一些东西。
在我的应用程序(下面的模型)中,我遇到了一个似乎超出我控制范围的数据问题,其中连接表 JOBORDERCATEGORIES 中的 CategoryId 在 CATEGORY 表中没有相应的行。我通过 JobOrder 中的 getJobCategories() 访问类别数据。当我的类别表缺少引用行时,这会产生以下错误:
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
}
最佳答案
这些情况并不是最有趣的,但我之前曾经处理过这种自行设计的 ORM 问题;) 基本上,您在这里要做的是存储对象属性不是以对象引用的形式输入,而是以整数的形式输入,虽然您会丢失 GORM 提供的一些动态查找器,但您将有一种相当直接的方法来访问数据,而无需费力去处理Hibernate 的内部结构。
基本上,这将涉及放弃 JobOrder 和 Category 上的 hasMany 和 BelongsTo 属性。相反,您会想做类似的事情
def myJobOrder = JobOrder.get(yourId);
def myCategoryIds = JobOrderCategories.findAllByJobOrderID(myJobOrder.id)
def myCategories = Categories.withCriteria {
in('id', myCategoryIds)
}
您可以将这些遍历的变体放入类的辅助方法中,例如,您的 getJobCategories 方法可能会变成
class JobOrder {
//...
def getJobCategories() {
def myCategoryIds = JobOrderCategories.findAllByJobOrderID(this.id)
def myCategories = Categories.withCriteria {
in('id', myCategoryIds)
}
}
}
等等。这绝对不是世界上最漂亮的事情,而且你会失去使用 GORM 轻松遍历事物的能力(例如
jobOrder.withCriteria {
categories {
eq('name', blah)
}
}
成为executeQuery类型的情况。) 但总的来说,处理起来还不错:) 希望有帮助!
关于grails - 我可以从关联(joinTable)中恢复引用丢失的数据吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9603964/