Hibernate/Domain NullPointerException?
Context: I am trying to upgrade a Grails project from 1.0.3 to 1.3.7 and am running into a bunch of problems. I just was getting a lot of DuplicateMappingExceptions because the project had Hibernate XML files as well as domain files. I moved the domain files out to src/groovy, but now find myself with a new NullPointerException, and I don't know how to go about resolving it:
2011-05-30 10:27:29,037 [main] ERROR context.GrailsContextLoader - Error executing bootstraps: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.grails.tomcat.InlineExplodedTomcatServer.doStart(InlineExplodedTomcatServer.groovy:112)
at org.grails.tomcat.InlineExplodedTomcatServer$doStart.callCurrent(Unknown Source)
at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:97)
at grails.web.container.EmbeddableServer$start.call(Unknown Source)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
at RunApp$_run_closure1.doCall(RunApp:33)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException
... 25 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException
... 25 more
Caused by: java.lang.NullPointerException
... 25 more
It looks like the NullPointerException is in:
Caused by: java.lang.NullPointerException
at org.codehaus.groovy.grails.commons.DefaultGrailsDomainClassProperty.equals(DefaultGrailsDomainClassProperty.java:506)
at org.codehaus.groovy.grails.commons.GrailsDomainConfigurationUtil.configureDomainClassRelationships(GrailsDomainConfigurationUtil.java:167)
at org.codehaus.groovy.grails.commons.DomainClassArtefactHandler.initialize(DomainClassArtefactHandler.java:67)
at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.initializeArtefacts(DefaultGrailsApplication.java:738)
at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.initializeArtefacts(DefaultGrailsApplication.java:721)
at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:682)
at org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.configureDomainClass(GrailsHibernateUtil.java:163)
at org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.configureHibernateDomainClasses(GrailsHibernateUtil.java:117)
at org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration.buildSessionFactory(GrailsAnnotationConfiguration.java:124)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
at org.codehaus.groovy.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean.newSessionFactory(ConfigurableLocalSessionFactoryBean.java:126)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 461 more
This makes it seem like the error is in the Domain Classes (either the classes themselves or the Hibernate XML) that I just moved to src/groovy. I have both domain classes and hibernate xml in the project.
Here are a couple of links that I have found that show a similar stack trace. It definitely seems like something funky in开发者_如何学运维 the relationship between the Hibernate XML and the Domain Classes:
- Similar Stack Trace 1
- Similar Stack Trace 2
Looks like you shouldn't use Hibernate mapping on non-domain classes. The exception says that there's a relationship configured that Grails can't find a target property for (see here, line 167). Namely, there's a null in some class' persistentProperties
.
I have a very little idea why that bug occurs, but would start with moving all mapped classes to domain
, then remove all XML mappings, then start running the app on HQL and adding XML configs back one-by-one. This would narrow the search pretty quickly.
You can also give a shot to debugging Grails itself - the source is available.
Tough to diagnose this, since the stack trace doesn't really have any useful info. I would try increasing the logging to see if that gives any clues, and then start to disable plugins one by one to see if it gets you further.
精彩评论