1

我看到这个错误消息已经在休眠的上下文中发布了好几次。

我在使用 grails 服务和域类时遇到此错误,任何帮助将不胜感激

领域类

class Coupon {
    Date dateCreated
    Date lastUpdated
    String code
    String email
    String address
    String state
    String city
    String zip
    def couponCodeGeneratorService

    def beforeValidate() {
        println code+"---------8-"
        code = couponCodeGeneratorService.generate()
        println code+"----------"
    }
    static constraints = {
        email blank:false,email:true
        address blank:false
        state blank:false
        city blank:false
        zip blank:false
    }
}

服务

class CouponCodeGeneratorService {
    Random randomGenerator = new Random()
    def serviceMethod() {

    }
    def generate(){
        def group1 =  randomGenerator.nextInt(9999)+"";
        def group2 =  randomGenerator.nextInt(9999)+"";
        def group3 =  randomGenerator.nextInt(9999)+"";
        def group4 =  randomGenerator.nextInt(9999)+"";
        return group1.padLeft(4,"0") +group2.padLeft(4,"0")+group3.padLeft(4,"0")+group4.padLeft(4,"0")
    }
}

我得到的错误是

---------8-
4844634041715590----------
4844634041715590---------8-
| Error 2012-09-10 11:32:54,938 [http-bio-8080-exec-7] ERROR hibernate.AssertionFailure  - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs)
   Line | Method
->>  19 | beforeValidate     in com.easytha.Coupon
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    46 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
|    24 | save . . . . . . . in com.easytha.CouponController
|   186 | doFilter           in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|    63 | doFilter . . . . . in grails.plugin.cache.web.filter.AbstractFilter
|   886 | runTask            in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . . . . . . .  in     ''
^   662 | run                in java.lang.Thread
| Error 2012-09-10 11:32:54,944 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver  - AssertionFailure occurred when processing request: [POST] /EasyTha/coupon/save - parameters:
zip: asdf
address: asd
email: s.s@s.xom
state: asd
code: 
create: Create
city: asdf
null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs). Stacktrace follows:
Message: null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs)
   Line | Method
->>  19 | beforeValidate     in com.easytha.Coupon
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    46 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
|    24 | save . . . . . . . in com.easytha.CouponController
|   186 | doFilter           in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|    63 | doFilter . . . . . in grails.plugin.cache.web.filter.AbstractFilter
|   886 | runTask            in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . . . . . . .  in     ''
^   662 | run                in java.lang.Thread

我对Hibernate不是很熟悉,这也是创建看起来像信用卡号的优惠券代码的正确方法吗?

4

1 回答 1

6

我怀疑问题可能CouponCodeGeneratorService是事务性的。因此,当您从内部调用服务方法时,beforeValidate您正在打开和关闭事务(即使您没有在方法内部触摸数据库),这将导致会话再次刷新。

尝试使服务非事务性:

static transactional = false
于 2012-09-10T13:10:17.583 回答