addTo* method work into controller but not in service

ChriX

New member
Hi,

I have some troubles since I upgraded to Grails 2.0RC1.
When I call an addTo* method into a service, this doesn't work. And I almost sure with previous Grails 2.0M2 release, this code worked well.

The code is simple, I've 2 classes: Application and Company. There are relationship between these classes: a company has many application and one application belongs to one company.

To add an application to a company, I use a service method after the comapny is populated into the database. Unfortunately, the method doesn't work. But I tried by putting implementation into a controller and in this case, the same code works.

Thanks.


The domain classes:
Code:
package org.test

class Application {
	
	String name

    static constraints = {
		name(blank: false, nullable: false)
    }
	
	static belongsTo = [ compagny : Company ]
}

package org.test

class Company {
	
	String name

    static constraints = {
		name(blank: false, nullable: false)
    }
	
	static hasMany = [ applications : Application ]
}

The service code:

Code:
package org.test.svc

import org.test.Application
import org.test.Company

class ApplicationManagementService {

    def addApplicationToCompany(Application a, Company c) { 
		if (a.validate(['name'])) {
			c.addToApplications(a).save()
			
			return a
		} else {
			return null
		}
    }
}


The integration test which failed:
Code:
package addtoproblem

import static org.junit.Assert.*
import org.junit.*
import org.test.Application;
import org.test.Company;
import org.test.svc.ApplicationManagementService


class ApplicationManagementIntegrationTestTests {
	def applicationManagementService
	
	Company c
	
    @Before
    void setUp() {
        c = new Company(name: 'My Company')
		c.save()
    }

    @After
    void tearDown() {
        // Tear down logic here
    }

    @Test
    void testApplicationManagement() {
		assert applicationManagementService
		
		// checks company is well saved into db 
        assert Company.list().size() == 1
		
		// creates application for adding
		Application a = new Application(name: 'My Application')
		
		applicationManagementService.addApplicationToCompany a, c
		
		assert Application.list().size() == 1
    }
}
 

dave

Administrator
Staff member
Are you receiving an error or just not saving the data?

I'm wondering if the transactional nature of a service method is the difference?

Any difference if you do a .save(flush:true) rather than just .save()
 
Top