Question

I create 2 tables one Category and one Manufacturer, and There relationship is Many-to-Many, So i use a join table, I insert values into two tables individually. Now i want to join two table by their id, but i cannot do, Can you help me.... When i try to insert value in join table give an exception, here is the exception:

    Cannot invoke method addToManufacturers() on null object. Stacktrace follows:
java.lang.NullPointerException: Cannot invoke method addToManufacturers() on null object

here is my domain class for Category

static hasMany    =  [manufacturers: Manufacturer]

static constraints = {
    name     blank: false, size: 0..60, unique: false
}

static mapping = {
    table 't01i001'
    id      column: 'f_category_id'
    name    column: 'f_name', length: 30     
    version column: 'f_revision'

    manufacturers joinTable: [name: 't01j001', key: 'k_category_id', column: 'k_manufacturer_id']
}

here is my domain class for manufacturer

static belongsTo  =  Category 
static hasMany    =  [categories: Category]

static constraints = {
    name     blank: false, size: 0..60, unique: false
}

static mapping = {
    table 't01i002'
    id        column: 'f_manufacturer_id'
    name      column: 'f_name',         length: 30
    version   column: 'f_revision'

    categories joinTable: [name: 't01j001', key: 'k_manufacturer_id', column: 'k_category_id']
}

add my controller where i try to insert

def manuInsertInCat(){
    def message, flag,count=0,categories = []
    int  catid  = params.containsKey("catid") ? params.catid : '0' 
    int  manuid = params.containsKey("manuid") ? params.manuid : '0' 
    def category    = Category.get(catid);
    def manufacture = Manufacturer.get(manuid)
    category.addToManufacturers(manufacture)
    message = "Successfully Loaded"
    count++
    flag =true
    render Category.getJsonData(categories, count, flag, message)        
}
Was it helpful?

Solution

At last i complete my job by this process its works fine.

def catInsertInManu(){
    def message, flag,count=0,manufacturers = []
    String  catid  = params.containsKey("catid") ? params.catid : '0' 
    String  manuid = params.containsKey("manuid") ? params.manuid : '0' 
    def category    = Category.get(catid)
    def manufacture = Manufacturer.get(manuid)
    manufacture.addToCategories(category)

    def m01i001001s = []
    manufacture.categories.each{ cat ->
        m01i001001s << [id:cat.id, name:cat.name]
    }
    manufacturers << [id: manufacture.id, name:manufacture.name, m01i001001s:m01i001001s]

    message = "Successfully Loaded"
    count++
    flag =true        
    render Manufacturer.getJsonData(manufacturers, count, flag, message)        
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top