Spring JPA Data scanning for repository does not work
Solution 1
What is your project structure? I copied your code on a project with UserRepository in basepackage.data and the repository was discovered.
I think it is because you have config and data children under default package. I got it to work by moving your data, web and config packages under a common package.
Here are my configuration annotations
package basepackage.config;
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages={"basepackage.data"})
@EntityScan(basePackageClasses=User.class)
@ComponentScan(basePackages={"basepackage"})
public class MyConfig {
@Autowired
private UserRepository repo;
@PostConstruct
public void init(){
for(int i=0; i<10; i++){
repo.save(new User("u-"+i));
}
}
}
You can also define your scanning by using basePackageClasses.
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackageClasses={UserRepository.class})
@EntityScan(basePackageClasses=User.class)
@ComponentScan(basePackages={"basepackage"})
Hope that helps.
Solution 2
I fixed it. In my case the problem was at a @Configuration class that implemented Spring JPA Auditing. I removed both @EnableTransactionManagement and @EnableJpaRepositories that were not necessary and the repositories were scanned again.
K4liber
Updated on July 07, 2022Comments
-
K4liber almost 2 years
It seems like i have disable jpa repositories. Have this error:
SEVERE: StandardWrapper.Throwable org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [C:\Users\jasiu\workspace2.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\BuyMyTime\WEB-INF\classes\web\UserController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [data.UserRepository] found for dependency [data.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [data.UserRepository] found for dependency [data.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
Config:
@Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages={"data"}) @ComponentScan(basePackages={"data"}, excludeFilters={ @Filter(type=FilterType.ANNOTATION, value = EnableWebMvc.class) }) public class RootConfig { @Bean public JpaTransactionManager transactionManager(EntityManagerFactory emf, DataSource dataSource){ JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); transactionManager.setDataSource(dataSource); return transactionManager; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/HelloWorld"); dataSource.setUsername("login"); dataSource.setPassword("haslo"); return dataSource; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("data"); em.setJpaVendorAdapter(jpaVendorAdapter); em.setJpaProperties(additionalProperties()); return em; } Properties additionalProperties() { Properties properties = new Properties(); //properties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); return properties; } @Bean public JpaVendorAdapter jpaVendorAdapter(){ HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); //adapter.setDatabase(); adapter.setShowSql(true); adapter.setGenerateDdl(false); adapter.setDatabasePlatform("org.hibernate.dialect.HSQLDialect"); return adapter; } }
Repo is in package "data":
@Repository @Transactional public interface UserRepository extends JpaRepository<User, Long>{ User findByNick(String nick); }
Controller:
@Controller public class UserController { @Autowired protected UserRepository userRepository; ... }
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.app</groupId> <artifactId>BuyMyTime</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>BuyMyTime Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.3.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.0.Final</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.10.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.3.RELEASE</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.0.1.Final</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.0.ALPHA03</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>BuyMyTime</finalName> </build> </project>
Please for any help.
-
ByeBye almost 8 years
UserRepository
is in packagedata
or*.data.*
? -
Aritz almost 8 yearsIs it working if you're not that specific in
@EnableJpaRepositories
and@ComponentScan
? -
K4liber almost 8 yearsByeBye - in package data, it works for components, but not for repository; Xtreme - if i clean specifics it does not make difference, same error
-
-
K4liber over 7 yearsIt does not work. If I remove properieties from scanning annotations it should scan all packages? But it didnt work too. I also not use spring boot, maybe i should start using it.
-
K4liber over 7 yearsWhen I try to use AutoConfiguration with boot i have: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/BuyMyTime]]
-
K4liber over 7 yearsEven if I remove annotations Repository and Autowired under protected UserRepository i get error creating bean "userRepository" ? It's enough when i enableJpaRepositories and interface implements JpaRepository to scan this interface? This error is caused by NoSuchMethodError setTransactionManagerBeanName.
-
Francois Mottard over 7 yearsIf you remove attribute, it doesn't scan all packages : Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation. -- This is from the link
-
Francois Mottard over 7 yearsScanning components works the same way with or without boot. The difference is that spring-boot create a lot of default components depending on your configuration.
-
K4liber over 7 yearsOk, thanks. Scanning is working pretty well now, dont get any error but have another problem: "The requested resource is not available." for any request mapped, I will try to configure all environment again, cause i even not get any info about what is wrong.
-
Emerson Micu almost 3 yearsThis solution was already presented in the accepted answer.