我的父表有复合主键,id1自动生成为SERIAL(1),id2只有默认值。 子表具有完全相同的主键,即父表中的外键。
子表类似于“plus”表或extanded Parent表。我们在PArent表中需要额外的列,但我们无法更改Parent表。因此,他们为我们创建了具有附加信息的Child表,将Parent表中的外键添加到Child表中,并将此组合键作为主键:)
我尝试用Grails映射它:
Parent.groovy
package test
class Parent implements Serializable{
Integer id1
Integer id2
String parentDesc
static hasOne = [child: Child]
static mappedBy = [child: 'parent']
static constraints = {
child nullable:true
}
static mapping = {
table 'parent'
id composite: ['id1', 'id2']
id1 column: 'id1'
id2 column: 'id2'
parentDesc column: 'parent_desc'
}
}
Child.groovy
package test
import java.io.Serializable
class Child implements Serializable{
Integer id1
Integer id2
Parent parent
String childDesc
static belongsTo = [parent: Parent]
static mapping = {
table 'child'
id composite: ['id1', 'id2']
id1 column: 'id1'
id2 column: 'id2'
childDesc column: 'child_desc'
columns {
parent(insertable: false, updateable: false) {
column name: 'id1'
column name: 'id2'
}
}
}
} Parent具有一个或零个具有复合键id1,id2的Child。 Child属于Parent(使用parent保存和删除),具有外键Parent的主键,此复合外键也是他的主键。
当有一些行时,Hibernate不能返回值(空表没有问题:) - 我认为它报告了同样的错误here(但这次已经解决了)。
技术信息: 数据库:MaxDB(旧版)也在H2嵌入式
上进行了测试完整测试项目:dropbox
堆栈跟踪:
[2m2017-06-05 13:59:26.063[0;39m [31mERROR[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.g.web.errors.GrailsExceptionResolver [0;39m [2m:[0;39m NullPointerException occurred when processing request: [GET] /
Stacktrace follows:
java.lang.reflect.InvocationTargetException: null
at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: null
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2172)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:692)
at org.hibernate.type.EntityType.resolve(EntityType.java:434)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:151)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:125)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1139)
at org.hibernate.loader.Loader.processResultSet(Loader.java:998)
at org.hibernate.loader.Loader.doQuery(Loader.java:936)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doList(Loader.java:2622)
at org.hibernate.loader.Loader.doList(Loader.java:2605)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2434)
at org.hibernate.loader.Loader.list(Loader.java:2429)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1787)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
at org.grails.orm.hibernate.query.AbstractHibernateQuery.listForCriteria(AbstractHibernateQuery.java:700)
at org.grails.orm.hibernate.HibernateGormStaticApi$_list_closure1.doCall(HibernateGormStaticApi.groovy:87)
at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:286)
at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:230)
at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:116)
at org.grails.orm.hibernate.HibernateGormStaticApi.list(HibernateGormStaticApi.groovy:73)
at org.grails.orm.hibernate.HibernateGormStaticApi.list(HibernateGormStaticApi.groovy:72)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.list(GormEntity.groovy:654)
at test.ParentController.$tt__index(ParentController.groovy:10)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
... 14 common frames omitted
答案 0 :(得分:1)
我知道这个问题有些陈旧,但我认为与你的问题相同。
无论如何,经过一些调试和研究,我得到了我的工作。我所要做的就是将获取行为切换为“join”,这样它将在同一个sql查询中获取父级和子级。请注意,这可能意味着性能瓶颈取决于您的用例。
static mapping = {
child fetch: 'join'
}
编辑:refresh()
仍然无法正常工作