我有旧的数据库,有些表有复合ids。
class Client {
String id
static hasMany = [
settings: Setting
]
static mapping = {
id column: 'client_id', generator: 'assigned'
}
}
class Setting {
Client client
String nodeId
String ccyPairPattern
Character qualifier
static mapping = {
id composite: ['client', 'nodeId', 'pattern', 'qualifier']
}
} 我想删除GORM协会的条目:
client.get('1').removeFromSettings(settingToRemove)
// settingToRemove.delete(flush: true)
// delete-orphans does not help这总是在刷新后引发异常。
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) :之所以会发生这种情况,是因为方法removeFrom*将client属性设置为null,并生成使用clientId = null进行删除的查询,因为client是组合键的一部分
在这种情况下,最好的解决方案是什么。看起来GORM对复合键的支持很差,或者我的映射是不正确的。
发布于 2015-09-29 15:50:32
当您在多个方面使用没有belongsTo的belongsTo时,换句话说,是一个单向关联,您将得到一个联接表。例如..。
class PurchaseOrder {
static hasMany = [items: Item]
}
class Item { }将产生三个数据库表: purchase_order、item和purchase_order_items。purchase_order_items表将包含两列: purchase_order_id和item_id。您可以阅读有关联接表这里的更多信息。
由于您处理的是遗留数据库,我认为最好的解决方案是不使用addTo*()和removeFrom*()。
Setting实例。示例:
def client = Client.get(1)
// Adding a setting
def setting = new Setting(client: client, nodeId: blah...)
setting.save()
// Removing a setting
/*
The prototype is used to search for a domain instance with a composite primary key.
So simply set the composite key properties accordingly.
*/
def prototype = new Setting(client: client, nodeId: blah...)
def setting = Setting.get(prototype)
setting.delete()缺少hasMany关联,您将无法通过client.settings属性访问客户端的设置。相反,您必须像这样查询它们:
def settings = Setting.findByClient(client)使用遗留数据库的一个后果是,如果数据库与该数据库不一致,GORM/Hibernate希望它将限制在它能为您做的事情上。
https://stackoverflow.com/questions/32846904
复制相似问题