GORM:懒洋洋地保存

时间:2013-05-02 21:25:53

标签: hibernate grails gorm

a)我尝试保存属于数据存储区域classe的联系人项目,如下所示:

class Contacts implements Serializable{

    static belongsTo = [dataStore:DataStore]

    DataStore dataStore
    String emailaddress

DS类:

class DataStore implements Serializable{

    static belongsTo = [service:Service,user:User]
    static hasMany = [users:User,contacts:Contacts,groups:Groups]

    Service service
    User user
    User users
    List<Contacts> contacts

b)在控制器级别,我创建一个联系人作为ajax请求:

def create() {
        String key = session["KEY"]
        DataStore ds = session["DS"]
        def user = springSecurityService.currentUser
        def c = new Contacts(dataStore:ds,emailaddress:params.contacts.email)
        if (c.validate()) {
            ds.addToContacts(c).save(flush:true)
            c.save()

c)以下错误会阻止保存

failed to lazily initialize a collection of role: $$$.DataStore.contacts, no session or session was closed. Stacktrace follows:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: $$$.DataStore.contacts, no session or session was closed
    at $$$$.ContactsController$$EO4iccbg.create(ContactsController.groovy:115)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at net.bull.javamelody.JspWrapper.invoke(JspWrapper.java:149)
    at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:259)
    at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:202)
    at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:175)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)

会话有效......这意味着什么?有什么想法解决这个问题吗?感谢

1 个答案:

答案 0 :(得分:1)

在关于GrailsOpenSessionInViewFilter的hibernate方面看了一段时间之后,我终于通过重置dataStore会话找到了一个解决方案:

def dsx = DataStore.get(ds.id)