2

我已经阅读了很多帖子和文档,并且一定遗漏了一些东西。

在我的应用程序(下面的模型)中,我遇到了一个似乎超出我控制范围的数据问题,我在连接表 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
}
4

1 回答 1

2

这些情况并不是最有趣的,但我之前不得不处理这种 Roll-Your-Own 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 类型的情况。)但总的来说,处理起来还不错:) 希望有帮助!

于 2012-03-07T16:38:53.053 回答