tags:

views:

704

answers:

2

In controller :

AssocCovList.addAssoc(3, 4)

In Domain :

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()
}


Here is sql structure :

CREATE TABLE omni.assoc_cov_list (
ASSOC_COV_LIST_ID int(11) NOT NULL auto_increment,
ASSOCIATION_ID smallint(6) NOT NULL default '0',
COV_LIST_ID int(11) NOT NULL default '0',
EDIT_DATE date default NULL,
PRIMARY KEY (ASSOC_COV_LIST_ID),
UNIQUE KEY ASSOC_COV_LIST_I2 (ASSOCIATION_ID,COV_LIST_ID),
KEY ASSOC_COV_LIST_FK1 (COV_LIST_ID),
KEY ASSOC_COV_LIST_FK2 (ASSOCIATION_ID)
) ENGINE=InnoDB AUTO_INCREMENT=9584 DEFAULT CHARSET=utf8;

This was returning No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

why it is returning null object ? I am able to update and delete the record(s) . Not working for new record .

Please help me

thanks

+1  A: 

Wait a minute... I think the domain class itself is not the right place to call a save() on itself ! This should be done at the controller or service level. Can you give a try to this :

In your domain class :

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

In your controller :

AssocCovList.addAssoc(<yourargs>).save()    
Philippe
You may need to go over the basics of Groovy constructors by reading http://groovy.codehaus.org/Groovy+Beans
Burt Beckwith
Good point ;-)Thanks for refreshing my brain. Is my assumption about the nullable properties correct though ?
Philippe
Hi all, The creation was not working for this domain only , i am able to create new records on other domains. Could you please go through the post again (i edited the code and error message i was getting ).@Philippe - I tried with your eg, but same error occurs. I can also provide more code and sql schema .thanks in advance
srinath
See my new answer for another idea... hope it's the right one although I didn't test it
Philippe
thanks for the input Philippe - I just tried now, but resulting same error "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" - My id is auto-increment and should create dynamically .
srinath
What version of Grails are you using ?
Philippe
A: 

You defined AssocCovList to have the following properties:

Integer id
Integer association_id
Integer cov_list_id
Date edit_date

And then try to create a new AssocCovList(association_id:3) using only the association_id. However, by default all properties are both persistent and required. To create a new AssocCovList you would need to provide also the id, cov_list_id and edit_date.

Cesar