I have the same issue with the current 1.4.0.BUILD-SNAPSHOT as with https://github.com/spring-projects/spring-boot/issues/5842 . it has to do with having the starter application class not in the root package. with 1.3.5 it works without any additional @EntityScan but with the latest build it fails.
@wilkinsona Could this have anything to do with the changes behind SPR-14246? If any refinements are needed there, let's sort this out ASAP since we're aiming to release 4.3.1 later today...
@masrawi you are reporting an issue and the sample doesn't match. I already replied to #5842 so if you want us to look into this, please attach a sample that actually reproduces the issue you're experiencing. Maybe you forgot to add orm.xml
to your sample?
@snicoll I already replied to your comment there. the only difference to the sample here is that @ComponentScan({ „base“ }) is not there as it is redundant. I do not use orm.xml in any of the examples
This isn't the same as #5842 as there's no orm.xml
in the application and scanning is being performed. However, the failure is the same:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) ~[spring-context-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) ~[spring-context-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.0.BUILD-SNAPSHOT.jar!/:1.4.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar!/:1.4.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar!/:1.4.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar!/:1.4.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar!/:1.4.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar!/:1.4.0.BUILD-SNAPSHOT]
at bootstrap.Application.main(Application.java:14) [classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [spring-boot-example-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [spring-boot-example-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [spring-boot-example-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58) [spring-boot-example-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:603) ~[spring-orm-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:443) ~[spring-orm-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:424) ~[spring-orm-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:310) ~[spring-orm-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) ~[spring-orm-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) ~[spring-orm-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
... 24 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187) ~[spring-core-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:600) ~[spring-orm-4.3.1.BUILD-SNAPSHOT.jar!/:4.3.1.BUILD-SNAPSHOT]
... 31 common frames omitted
I need to dig into it some more to figure out exactly where the problem lies, but I don't think it's directly related to SPR-14246 which handled a case where there _was_ an orm.xml
and scanning _wasn't_ being performed.
@masrawi it would help if you could be a bit more precise when reporting an issue. For one you added a comment in a very focused issue that's actually not your issue at all (only the exception is). Then, you project is broken in 1.3 and 1.4 for me. If you start that project in the IDE, your persistence unit is empty (there's no DDL generation even though you enforce SQL generation in your custom configuration). All that noise in the sample really doesn't help.
So I tried your sample in 1.3 running the fat jar. Yes it does start but the persistence unit is empty and you can't really use the JPA infrastructure. Is that sample representative of a real world use case?
I think I now understand what's happening and why. It "fails" silently with 1.3.5 as the app starts but leaves you with an empty persistence unit, whereas 1.4 catches the misconfigured entity scanning and fails early on. If the sample tried to make use of the entities, 1.3.5 would also fail, albeit later on.
The sample is a little unusual as it has no orm.xml
, its entity isn't in the same package as the main application class, and @EntityScan
isn't used. This means that, as far as Spring ORM/Hibernate are concerned, there aren't any entities. As mentioned above, it only works at all as the entity isn't actually used in the sample.
When using 1.3.5 and a Spring Boot fat jar, the ill-advised fix #420 means that startup doesn't fail but the persistence unit is empty. When using 1.3.5 and a shaded jar the application fails to start due to the mis-configured entity scanning. That it to say, the behaviour of a shaded jar is arguably better than the behaviour of a fat jar as it catches the misconfiguration earlier.
When using 1.4, the app fails to start due to the misconfigured entity scanning. This is the same for both a fat jar and a shaded jar. There's no orm.xml
in the application so the fix for SPR-14246 doesn't come into play.
adding this to the starter class works whereas 1.4 does not
@Autowired
private DataSource dataSource;
@Bean
public LocalSessionFactoryBean sessionFactory()
{
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setAnnotatedClasses(User.class);
sessionFactory.setAnnotatedPackages("base.models");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
private Properties hibernateProperties()
{
final Properties properties = new Properties();
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.dialect", org.hibernate.dialect.HSQLDialect.class.getName());
return properties;
}
Adding a LocalSessionFactoryBean
doesn't switch off Spring Boot's auto-configuration of Hibernate (perhaps it should, but that's a separate issue that's somewhat similar to #5542).
By adding the LocalSessionFactoryBean
above, what you have ended up with in 1.3.5 is one persistence unit that's misconfigured and is therefore empty, and a SessionFactory
that you've setup yourself that contains your User
entity. If you use the Shade plugin to build your app with 1.3.5, it still fails even with these changes. And it fails in 1.4 due to the misconfigured, empty persistence unit that your changes haven't addressed.
If you want to use Spring Boot's auto-configuration of Hibernate, you should use @EntityScan
. If you want to configure things yourself (but I don't understand why you would) then you should probably exclude HibernateJpaAutoConfiguration
entirely.
If you need some more help, then please come and chat on Gitter or post a question on Stack Overflow.
I face the same issue in a project without persistence. The project worked with Spring Boot 1.3, but has this issue with 1.4.
@andrashatvani if you want us to investigate, please create a separate issue (this issue is closed) and attach a sample that reproduces the issue. Thanks!
@snicoll I further analyzed the issue and found out that
@EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class)
must be set on the application class and
spring.data.jpa.repositories.enabled=false
must be set in the application properties/yml.
From my point of view it's sufficient to include this information in the release notes, too.
Thanks @andrashatvani your last comment solved this issue for me. After upgrading from srping boot 1.3.6 to spring-boot 1.4.0 launching the app from the IDE was working, but not with a fat jar.
@andrashatvani that doesn't sound right. You shouldn't have to do this. Could you please share more details? Please do so in a new issue.
It is a bug,
just update ur SpringBoot to 1.4.1.BUILD-SNAPSHOT
it is ok~
@andrashatvani thank u,I use the jar package to execute the project,and messy code,why?
I met a error of unable to resolve persistence root url when tried to run runnable jar from command line. It could be run on Intelllij Idea without any problem. sqljdbc42.jar file was directly included into external libraries.
2018-04-08 09:11:06.230 INFO 11488 --- [ main] o.s.j.d.DriverManagerDataSource : Loaded JDBC driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
2018-04-08 09:11:06.239 INFO 11488 --- [ main] o.s.j.d.DriverManagerDataSource : Loaded JDBC driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
2018-04-08 09:11:06.841 WARN 11488 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
2018-04-08 09:11:06.845 INFO 11488 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-04-08 09:11:06.860 INFO 11488 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-04-08 09:11:06.869 ERROR 11488 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1085) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at com.vtyc.SqlserverApplication.main(SqlserverApplication.java:14) [sqlserver.jar:na]
Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:637) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:459) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:440) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:328) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 16 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:195) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:633) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 21 common frames omitted
The same error here. Is there any solution/workaround?
@pnaszarkowski Please read all of the comments above yours in the issue. There are several suggestions for what the cause of the problem might be. If you are sure that none of those apply, please open a new issue with a minimal sample (attached as a zip or in a GitHub repository) that reproduces the problem.
I investigated this a bit more, I found that adding basic orm.xml file in resources/META-INF directory was needed - that fixed the error. However finally I disabled autoconfiguration of hibernate by adding exclude option for application class like that as I don't really need this:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
I hope that helps anybody facing similar issue.
Most helpful comment
@snicoll I further analyzed the issue and found out that
@EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class)
must be set on the application class and
spring.data.jpa.repositories.enabled=false
must be set in the application properties/yml.From my point of view it's sufficient to include this information in the release notes, too.