-
Notifications
You must be signed in to change notification settings - Fork 40.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when adding @EnableJpaRepositories #256
Comments
Having some trouble replicating this, if I change the jpa sample main class to the following: @Configuration
@ComponentScan
@EnableAutoConfiguration
@EntityScan("sample.data.jpa.domain")
@EnableJpaRepositories("sample.data.jpa.service")
public class SampleDataJpaApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleDataJpaApplication.class, args);
}
} Things appear to work as expected. Could you attach a full stack trace and also run your app with |
package com.mobenga.snowcat.web.config;
import com.jolbox.bonecp.BoneCPDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.*;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import javax.sql.DataSource;
@Configuration
@ComponentScan({"com.mobenga.snowcat.web", "com.mobenga.snowcat.service"})
@PropertySource("classpath:application.properties")
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableJpaRepositories("com.mobenga.snowcat.service.data.repository")
@EntityScan("com.mobenga.snowcat.service.data")
public class SnowcatMVCConfig extends WebMvcConfigurerAdapter {
@Value("${port:8080}")
private int port;
@Value("jdbc:h2:${db.url}")
private String url;
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
@Bean
public DataSource dataSource() {
BoneCPDataSource dataSource = new BoneCPDataSource();
dataSource.setDriverClass("org.h2.Driver");
dataSource.setJdbcUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
public static void main(String[] args) {
//Used by Spring Boot
SpringApplication.run(SnowcatMVCConfig.class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(port);
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/"));
return factory;
}
} |
|
Do you see anything strange there? |
Just trying to replicate. Do you have a stack trace as well? |
|
That is the bottom part of it. |
I can't really work out what is causing this. It looks like Do you have any code that you can share? Feel free to email me privately (pwebb at gopivotal dot com) if you can't share anything publicly. |
/cc @olivergierke in case he has any suggestions |
I have sent you a mail, Phil. |
I can reproduce this. You just need to define a |
I already have that bean defined. It still doesn't work. @Bean
public DataSource dataSource() {
BoneCPDataSource dataSource = new BoneCPDataSource();
dataSource.setDriverClass("org.h2.Driver");
dataSource.setJdbcUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
} |
No, that's not what I meant. Defining that |
This did not work @Bean
@Scope("prototype")
public DataSource dataSource() {
... I will try and get rid of the datasource to see if that helps. |
I removed DataSource from java config and added the following to application.properties: spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:${db.url}
spring.datasource.username=${db.username}
spring.datasource.password=${db.password} Still the same error:
|
The other thing that worked for me is moving the |
Thanks. I got your app working on 1.0.0.RC1 by removing the Even if you can confirm that works for you it doesn't solve the underlying problem, which I don't think I'm any closer to understanding. |
On a related note, you can probably lose the |
Yeah I forgot to say I did that (but it probably doesn't make any difference). |
Actually, once it was working (right version of Hibernate etc) with a native I think there are probably many issues here, and some may be unrelated to each other. The errors and stacktraces appear to be less than helpful, but we can keep this issue open while we track them down. |
I have tried normalising hibernate and spring data dependencies, but it still does not work. Can you send me your working copy of pom.xml? Regarding @propertysource("classpath:application.properties"), I keep that there since my unit tests doesn't work otherwise. |
I'll send you a working pom. And here's a simpler project showing the same problem: https://github.com/scratches/gh-256 (there are 2 commits and one works, the other doesn't, only difference is Hibernate version). I'll ask @olivergierke and @jhoeller to comment on the version compatibility, and maybe we can fail in a more friendly way. Maybe your unit tests should use |
It seems like this commit has changed the package declaration of Simply changing the package name back should allow us to be compatible with older versions o Hibernate, as the legacy type is still available (although deprecated) in Hibernate 4.3. |
Great, now it is working, with the modified pom file Dave sent. Looks like the solution to everything was pretty much to not specify versions for hibernate. |
However, I would prefer to have datasource config in Java. Since I want to do some checks on operating system before setting jdbc url. |
That works for me. Did you change anything else? It might help to put the |
I'm closing this one because the original error is gone I believe. If you still have problems with the |
Works for me now too. Thanks everyone for helping out. |
Change TomcatEmbeddedServletContainerFactory to allow per context skip patterns to be defined, rather than using a global system property. This commit also renames `skipPatterns` to `tldSkip` to align it with Tomcat context.xml configuration. Updates gh-256
...without any deprecation warnings and with some chance that it will still work with Hibernate 5. Fixes spring-projectsgh-256
Change TomcatEmbeddedServletContainerFactory to allow per context skip patterns to be defined, rather than using a global system property. This commit also renames `skipPatterns` to `tldSkip` to align it with Tomcat context.xml configuration. Updates spring-projectsgh-256
Is this issue closed? I'm having the same issue if I don't write create a datasource bean and rely on application.properties in the classpath for datasource I have this in application.properties
and these are the annotations and my app class.
I have a slightly older project where I don't use the JPA repositories, and that one starts and connects to the data just fine. Does anybody see anything wrong with this? |
That issue was closed over a year ago. As @dsyer said above, please create another issue. The |
Thanks. I just wanted to confirm. I will do that if I'm not able to solve it by tinkering with some of the values in the pom. Cheers! |
This solved my issues: <dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency> |
@Hard-system We'd always recommend using our managed dependencies rather then defining your own versions. |
in our case was working fine but not for tests with @WebMvcTest Took a while to find out but issue was:
instead do:
and
|
@damienpalacio this one caused a lot of pain, thnx for finding out |
Related to #254 (which was closed, that is why I created a new issue)
If I add @EnableJpaRepositories I get:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
The text was updated successfully, but these errors were encountered: