我在Grails中使用表每个子类策略,方法是将超类中静态mapping
字段的tablePerHierarchy
属性设置为false。这样,Grails为我的超类创建一个表,为我的每个子类创建一个额外的表。
然而,虽然父类和子类记录共享相同的ID(主键),但没有外键约束来保持它们的一致性,即有可能删除父类记录,使子类记录处于无效状态。我想知道是否有一个设置/属性使GORM以某种方式解决这个问题,例如通过约束。还是我唯一的选择是手动添加外键?
例如,给定以下域类作为超类:
class Product {
String productCode
static mapping = {
tablePerHierarchy false
}
}
和以下域类作为子类:
class Book extends Product {
String isbn
}
这导致创建两个表,Product
表和Book
表。当创建Book(例如,通过脚手架页面)时,将一条记录插入到每个表中,它们之间的唯一联系是每个表的ID值是相同的。具体来说,数据可能如下所示:
PRODUCT
Id Version ProductCode
1 1 BLAH-02X1
BOOK
Id ISBN
1 123-4-56-7891011-1
因为没有在数据库级别为这些表定义正式的关系,所以有可能删除其中一条记录而留下另一条记录,从而导致无效数据。显然,我可以使用SQL在两个ID字段上手动创建一个外键约束,但我希望让Grails来处理这个问题。这可能吗?
使用Grails 2.2.1
解决!
下面的解决方案为我解决了这个问题。将下面的类添加到src/java
(这个类不能用Groovy编写)
package org.example;
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration;
import org.hibernate.MappingException;
import org.hibernate.mapping.JoinedSubclass;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
import java.util.Iterator;
public class TablePerSubclassConfiguration extends GrailsAnnotationConfiguration {
private static final long serialVersionUID = 1;
private boolean alreadyProcessed = false;
@Override
protected void secondPassCompile() throws MappingException {
super.secondPassCompile();
if (alreadyProcessed) {
return;
}
for (PersistentClass persistentClass : classes.values()) {
if (persistentClass instanceof RootClass) {
RootClass rootClass = (RootClass) persistentClass;
if (rootClass.hasSubclasses()) {
Iterator subclasses = rootClass.getSubclassIterator();
while (subclasses.hasNext()) {
Object subclass = subclasses.next();
// This test ensures that foreign keys will only be created for subclasses that are
// mapped using "table per subclass"
if (subclass instanceof JoinedSubclass) {
JoinedSubclass joinedSubclass = (JoinedSubclass) subclass;
joinedSubclass.createForeignKey();
}
}
}
}
}
alreadyProcessed = true;
}
}
然后在DataSource.groovy
中将其设置为配置类
dataSource {
configClass = 'org.example.TablePerSubclassConfiguration'
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
更新我已经向Grails提交了这个问题的pull request。这个修复包含在Grails 2.3.8或2.3.9中(不记得是哪个了)。
Hibernate确保每个子类对应一个表的情况下数据的完整性。在每个子类对应一个表的情况下,子类与超类保持主键关联。看看每个子类的Hibernate表。下面是您的测试用例:
class Product {
String productCode
static mapping = {
tablePerHierarchy false
}
}
class Book extends Product{
String isbn
}
//Test Case
def testTablePerSubclass{
def product = new Product(productCode: 'XYZ456')
product.save(flush: true, failOnError: true)
def book = new Book(isbn: '123456123', productCode: 'ABC123')
book.save(flush: true, failOnError: true)
assert Book.list().size() == 1 //One Book
assert Book.list()*.id == [2] //Book id
assert Product.list().size() == 2 //One Product, one Book (2 Products)
assert Product.list()*.id == [1, 2] //Product id, Book Id
//Grab the product (book) to delete
def productToDelete = Product.get(book.id)
productToDelete.delete(flush: true)
assert Book.list().isEmpty() //Book deleted from Book table as well
assert Product.list().size() == 1 //One Product remaining in Product table
assert Product.list()*.id == [1] //Remaining Product Id
}
在DataSource.groovy
中保持logSql
为true,以查看相应的sql被执行。
Log Sql Output:-
Hibernate: insert into product (id, version, product_code) values (null, ?, ?)
Hibernate: insert into product (id, version, product_code) values (null, ?, ?)
Hibernate: insert into book (isbn, id) values (?, ?)
Hibernate: select this_.id as id0_0_, this_1_.version as version0_0_, this_1_.product_code as product3_0_0_, this_.isbn as isbn1_0_ from book this_ inner join product this_1_ on this_.id=this_1_.id
[com.example.Book : 2]
Hibernate: select this_.id as id0_0_, this_.version as version0_0_, this_.product_code as product3_0_0_, this_1_.isbn as isbn1_0_, case when this_1_.id is not null then 1 when this_.id is not null then 0 end as clazz_0_ from product this_ left outer join book this_1_ on this_.id=this_1_.id
[com.example.Product : 1, com.example.Book : 2]
Hibernate: delete from book where id=?
Hibernate: delete from product where id=? and version=?
Hibernate: select this_.id as id0_0_, this_1_.version as version0_0_, this_1_.product_code as product3_0_0_, this_.isbn as isbn1_0_ from book this_ inner join product this_1_ on this_.id=this_1_.id
[]
Hibernate: select this_.id as id0_0_, this_.version as version0_0_, this_.product_code as product3_0_0_, this_1_.isbn as isbn1_0_, case when this_1_.id is not null then 1 when this_.id is not null then 0 end as clazz_0_ from product this_ left outer join book this_1_ on this_.id=this_1_.id
[com.example.Product : 1]
使用Grails 2.2.2