I have two domain classes
class Country {
... // some fields, including other domains
}
class City {
... // some fields, including other domains
Country itsCountry
}
One of my service method is here:
City createCity(String name, Country country) {
// country is existing and loaded in the controller's layer and passed here
City city = new City()
city.itsCountry = country // it is not persisted
city.save('flush':true)
}
The problem is that in the database the city has null country. Of course, I simplified the example, really is it more complicated. (Unfortunately, some important details can be lose, I hope if you faced this problem, you share the reason)
What I did without success:
I feel that it must be some trivial thing. What can be a reason of it?
in Grails we can already get the country id in params .Right. like for example, params?.countryId.
If it is new record for country table, then first create new object for country and assign in city object.
For example,
Country country = new Country()//for new record
country.name=params?.countryName
City city=new City()
city.itsCountry = country
city.save('flush':true)
If you have already country record in table then use get method to get object and assign it to city.
For example,
Country country=Country.get(Long.valueOf(params?.countryId))
city.itsCountry = country
city.save('flush':true)
I hope it will help you. If any doubt please ping me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.