Reputation: 755
My model needs to have multiple enums of the same type:
class Broker {
static constraints = {
brokerTypes(nullable:false)
}
List<BrokerType> brokerTypes
}
The model is being instantiated with the params from the request, which has in it a list of BrokerTypes:
def save(){
def brokerInstance = new Broker(newParams)
System.out.println(brokerInstance.getBrokerTypes().toString());
if (!brokerInstance.save(flush: true)) {
render(view: "create", model: [brokerInstance: brokerInstance])
return
}
redirect(action: "show", id: brokerInstance.id)
}
The println prints out the list of BrokerTypes as expected, so i know that it exists in the instance. Later, the model is retrieved as follows:
def brokerInstance = Broker.findByLatAndLon(lat,lon)
System.out.println(brokerInstance.getBrokerTypes().toString());
This time the println prints out 'null'
So i imagine the problem is that GORM doesn't know how to store this list of enums, and instead when the brokerInstance.save() is called, its saving the brokerTypes field as null.
Do i need to create a mapping somehow to get GORM to recognize the list? A hack alternative would be to instead of storing the list of enums, to store a list of strings or something and then map back to the enum when needed, but this doesnt seem clean
Upvotes: 1
Views: 929
Reputation: 3047
You will have to use a hasMany
clause so that grails/gorm initializes a one to many relationship
You should add the following snippet to your domain class.
static hasMany = [brokerTypes : BrokerType]
Upvotes: 3