【发布时间】:2014-02-27 22:16:02
【问题描述】:
我有以下 ListProducts 域对象;
String name
Date lastUpdated
Date dateCreated = new Date()
String lastUpdatedBy
CatLists catLists //
Product product // has a composite primary key
ListProducts listProducts
static constraints = {
name nullable: true, blank: true
catLists nullable: false
lastUpdatedBy nullable: true
lastUpdated nullable: true
dateCreated nullable: false
product unique: 'listProducts'
}
static mapping = {
version true
table 'list_products'
sort "name"
}
Product 域类具有以下约束,它是一个复合键
FOREIGN KEY (product_fulfillment_sku, product_merchant_id) REFERENCES product(fulfillment_sku, merchant_id)
因为它没有主键,即 ID,因为表因批处理作业而被删除,因此唯一值是 sku 和商家 ID。
我可以在 list_products 表中看到 product_fulfillment_sku、product_merchant_id 和 cat_lists_id 列,但是如何在 grails 域类中表示这些,以便如果产品已经存在则无法添加?
我想要实现的是,一个产品可以在多个 ListProducts 分组中,但每个分组只能出现一次。
针对 Postgres Sql 数据库的 Grails v2.2.1!
有什么想法吗?
J
【问题讨论】:
-
那么您在此处显示的这个域对象代表一个分组?
-
正确。所以我重构了代码,以便 catLists 域对象在其列表中添加和删除项目。如果有人删除了该列表,那么 ListProducts 表中的所有引用也会被删除。我从这个博客mrpaulwoods.wordpress.com/2011/02/07/… 中获得了一些灵感
标签: sql hibernate grails grails-orm