Grails枚举为域属性,间歇性地保存为数据库

时间:2015-10-12 19:03:05

标签: grails enums

Grails 2.3.7,Java 1.7,tomcat

由于没有明显的原因(我可以看到),Review域的reviewResults属性有时会保存为枚举的序数值。

数据库架构表示该列是varchar类型。

有时NA会保存为NA,但有时会保存为0。

这与传递相同,除非传递有时保存为1。

为什么会发生这种情况的任何想法?

abstract class Work {
    // Nothing defined in here related 
    // to the review domain enum.
}

有问题的域类。

class Review extends Work {
    Results reviewResults
    String notes

    enum Results {
        NA,
        Passed,
        Error
    }

    static constraints = {
        reviewResults(nullable: true, enumType: 'string')
    }

    // Is this redundant if it is already declared in the constraints?
    static mapping = {
        reviewResults enumType: 'string'
    }
}

域名相关的控制器。

// Reviews created with Quartz job, results property is not set.
class ReviewController {
    def reviewService

    def show(Long id) {
        def reviewInstance = Review.get(id)

        def reviewResultsOptions = []
        reviewResultsOptions.addAll(com.mycompany.app.Review.Results.values())

        [reviewInstance: reviewInstance, reviewResultsOptions: reviewResultsOptions]
    }

    def closeWork(Long id, String reviewResults, String notes) {
        def review = Review.get(id)

        review.reviewResults = Review.Results.valueOf(reviewResults)

        def result = reviewService.closeReview(review, notes)
    }
}

控制器/域的服务。

class ReviewService {
    def workService

    Review closeReview(Review work, String notes) {
        work.notes = notes
        workService.closeWork(work)

        return work.errors.allErrors.empty ? work : null
    }
}

最终保存Review对象的服务。

class WorkService {
    Tracking closeWork(Work workInstance) {
        def tracked = new Tracking()

        workInstance.setStatus(status)
        workIntance.setClosed(new Date())

        WorkInstance.save(flush: true)

        // Set some tracking properties and save and return the tracking object.
    }
}

0 个答案:

没有答案