我的 grails 项目面临错误“TransactionRequiredException:”

package com.fhjony.ocbt



import grails.web.servlet.mvc.GrailsParameterMap



class MemberService {


def save(GrailsParameterMap params) {

    Member member = new Member(params)

    def response = AppUtil.saveResponse(false, member)

    if (member.validate()) {

        member.save(true)

        if (!member.hasErrors()){

            response.isSuccess = true

        }

    }

    return response

}



def update(Member member, GrailsParameterMap params) {


    member.properties = params

    def response = AppUtil.saveResponse(false, member)

    if (member.validate()) {

        member.save(flush: true)

        if (!member.hasErrors()){

            response.isSuccess = true

        }

    }

    return response

}



def getById(Serializable id) {

    return Member.get(id)

}



def list(GrailsParameterMap params) {

    params.max = params.max ?: GlobalConfig.itemPerPage()

    List<Member> memberList = Member.createCriteria().list(params) {

        if (params?.colName && params?.colValue) {

            like(params.colName, "%" + params.colValue + "%")

        }

        if (!params.sort) {

            order("id", "desc")

        }

    }

    return [list: memberList, count: memberList.totalCount]

}



def delete(Member member) {

    try {


        member.delete(flush: true,failOnError:true)

    } catch (Exception e) {

        println(e.getMessage())

        return false

    }

    return true

}

}



慕丝7291255
浏览 134回答 1
1回答

猛跑小猪

您希望数据库交互发生在事务上下文中。其中一个简单的部分是您可以使用 标记您的服务类别@grails.gorm.transactions.Transactional。除此之外,这与您的问题并不真正相关,但是将GrailsParameterMapmap 作为方法参数传递是一件不寻常的事情。正确的做法取决于应用程序中的某些因素,您可能希望将值传递到服务中而不是整个地图中,但如果您确实想要服务中的整个地图,实现这一点的一种方法是通过WebAttributes.import grails.gorm.transactions.Transactionalimport grails.web.api.WebAttributes@Transactionalclass MemberService implements WebAttributes {&nbsp; &nbsp; def serviceMethod() {&nbsp; &nbsp; &nbsp; &nbsp; // you can access params here because&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // WebAttributes provides access to it&nbsp; &nbsp; &nbsp; &nbsp; Member member = new Member(params)&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java