没有Hibernate会话绑定到线程,配置不允许在这里创建非事务性的

时间:2010-02-10 11:22:56

标签: grails

在控制器中:

AssocCovList.addAssoc(3, 4)

在域中:

package com.org.domain
class AssocCovList {
    Integer id
    Integer association_id
    Integer cov_list_id
    Date edit_date

    static belongsTo = [association : Association, cov_list : CoverageList]

    static constraints = {
        edit_date(nullable:true )
    }

    static mapping = {
        table 'assoc_cov_list'
        version false
        columns {
        id column : 'ASSOC_COV_LIST_ID'
        association_id column : 'ASSOCIATION_ID'
        cov_list_id column : 'COV_LIST_ID'
        edit_date column : 'EDIT_DATE'
        }
    }


def static addAssoc(3, 4){
   def aclist = new AssocCovList(association_id:3,cov_list_id:4, edit_date:new Date())
   aclist.save()
}


这是sql结构:

CREATE TABLE omniassoc_cov_list
  ASSOC_COV_LIST_ID int(11)NOT NULL auto_increment,
  ASSOCIATION_ID smallint(6)NOT NULL默认为'0',
  COV_LIST_ID int(11)NOT NULL默认为'0',
  EDIT_DATE日期默认为NULL,
  PRIMARY KEY(ASSOC_COV_LIST_ID),
  独特的钥匙ASSOC_COV_LIST_I2ASSOCIATION_IDCOV_LIST_ID),
  KEY ASSOC_COV_LIST_FK1COV_LIST_ID),
  KEY ASSOC_COV_LIST_FK2ASSOCIATION_ID
)ENGINE = InnoDB AUTO_INCREMENT = 9584 DEFAULT CHARSET = utf8;

这是返回No Hibernate Session绑定到线程,配置不允许在这里创建非事务性

为什么它返回null对象? 我能够更新和删除记录。不适用于新纪录。

2 个答案:

答案 0 :(得分:1)

等一下......我认为域类本身不是自己调用save()的正确位置! 这应该在控制器或服务级别完成。你可以尝试一下这个:

在您的域类中:

def static addAssoc(<yourargs>){
    return new AssocList(/*Whatever arguments you pass */)
}

在您的控制器中:

AssocCovList.addAssoc(<yourargs>).save()    

答案 1 :(得分:0)

您将AssocCovList定义为具有以下属性:

Integer id
Integer association_id
Integer cov_list_id
Date edit_date

然后尝试仅使用new AssocCovList(association_id:3)创建association_id。但是,默认情况下,所有属性都是持久性必需。要创建新的AssocCovList,您还需要提供idcov_list_idedit_date