问题
Given a GORM class:
class PriceSheet {
Client client
Population population
Product product
RevenueModelType modelType
BigDecimal price
static constraints = {
client(unique: ['population', 'product', 'modelType'])
}
}
I'm wanting a PriceSheet to be saved/updated only if the client, population, product and modelType are unique. (There should only be one pricesheet item for combination of client, population, product and modelType).
The key is being created in mySQL.
My issue is the grails validation passes, but the save fails.
priceSheetInstance.validate()
if (priceSheetInstance.hasErrors()) {
respond priceSheetInstance.errors, view:'create'
return
}
priceSheetInstance.save flush:true
Any ideas or suggestions? I put the debugger on a breakpoint after validate and see errors are empty.
Grails 2.3.10
回答1:
First, you need to tell GORM which properties make up your composite primary key and then your domain class needs to implement Serializable. The end result is something like this:
import org.apache.commons.lang.builder.HashCodeBuilder
class PriceSheet implements Serializable {
Client client
Population population
Product product
RevenueModelType modelType
BigDecimal price
static constraints = {
}
static mapping = {
id composite: ['client', 'population', 'product', 'modelType']
}
boolean equals(other) {
if(!(other instanceof PriceSheet)) return false
other.client == client && other.population == population && other.product == product && other.modelType == modelType
}
int hashCode() {
def builder = new HashCodeBuilder()
builder.with {
append client
append population
append product
append modelType
}
builder.toHashCode()
}
}
You can read more about GORM composite keys here.
回答2:
Final solution was wrapping withNewSession in controller for saves:
PriceSheet.withNewSession {
priceSheetInstance.validate()
// needed to call save in order to check the unique compound key
// validate alone wasn't working
// also needed to wrap withNewSession above
// PriceSheet GORM object implements Serializable as well
// spent way more time than expected on this.... wrestled with Grails
if (priceSheetInstance.hasErrors() || (priceSheetInstance.save(failOnError: true) && priceSheetInstance.hasErrors())) {
respond priceSheetInstance.errors, view:'create'
return
}
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'priceSheet.label', default: 'Price Sheet'), priceSheetInstance?.id])
redirect (action: 'index')
}
'*' { respond priceSheetInstance, [status: CREATED] }
}
}
I didn't have to wrap withNewSession for updates... in fact, wrapping withNewSession for updates was causing StaleObjectExceptions
来源:https://stackoverflow.com/questions/31816736/grails-unique-compound-key-issues