Reputation: 21
I'm trying to extend the **SimpleJpaRepository ** by BaseRepository interface and the BaseRepositoryImpl.
Tha BaseRepositoryImpl extends **SimpleJpaRepository ** and implements BaseRepository.
In addition, I have some other Repositories such as CandidateRepository and EmployeeRepository that extends the BaseRepository.
Below you can see the code following the error:
To see the project structure click here
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>{
//..
}
@Repository
public abstract class BaseRepositoryImpl<T, ID extends Serializable> extends
SimpleJpaRepository<T, ID> implements BaseRepository<T, ID>{
private final EntityManager em;
public BaseRepositoryImpl(JpaEntityInformation<T,ID> entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
this.em = entityManager;
}
//..
}
The extend Repositories:
@NoRepositoryBean
public interface CandidateRepository extends BaseRepository<DatCandidate, UUID>{
//..
}
@Repository(value="CandidateRepositoryImpl")
public class CandidateRepositoryImpl extends BaseRepositoryImpl<DatCandidate, UUID> implements CandidateRepository{
public CandidateRepositoryImpl(
JpaEntityInformation<DatCandidate, UUID> entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
}
}
Here is my configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.projects"></context:component-scan>
<jpa:repositories base-package="com.projects.repositories.impl"
base-class="com.projects.repositories.impl.BaseRepositoryImpl"></jpa:repositories>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<entry key="hibernate.hbm2ddl.auto" value="none"/>
<entry key="hibernate.format_sql" value="true" />
</map>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/relocationdb?autoReconnect=true" />
<property name="username" value="root"></property>
<property name="password" value="1234" />
</bean>
</beans>
Service:
@Service(value="CandidateServiceImpl")
public class CandidateServiceImpl extends BaseServiceImpl<CandidateForCreationDto, CandidateDto,DatCandidate, UUID>
implements CandidateService{
private CandidateRepositoryImpl candidateRepository;
@Autowired
public CandidateServiceImpl (
@Qualifier("CandidateRepositoryImpl") JpaRepository<DatCandidate, UUID> candidateRepository
) {
this.candidateRepository = (CandidateRepositoryImpl)candidateRepository;
}
}
The Error:
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
I followed the Spring JPA documentation, but can't understand the error
attached is the running code
4:39:10 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.12.Final} March 21, 2018 4:39:10 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found March 21, 2018 4:39:10 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} March 21, 2018 4:39:12 PM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect March 21, 2018 4:39:16 PM org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'candidateController': Unsatisfied dependency expressed through field 'candidateBaseService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CandidateServiceImpl' defined in file [C:\Users\alon\Documents\workspace-sts-3.9.2.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Relocation\WEB-INF\classes\com\projects\service\impl\CandidateServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CandidateRepositoryImpl' defined in file [C:\Users\alon\Documents\workspace-sts-3.9.2.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Relocation\WEB-INF\classes\com\projects\repositories\impl\CandidateRepositoryImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5110) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5633) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1700) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1690) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CandidateServiceImpl' defined in file [C:\Users\alon\Documents\workspace-sts-3.9.2.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Relocation\WEB-INF\classes\com\projects\service\impl\CandidateServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CandidateRepositoryImpl' defined in file [C:\Users\alon\Documents\workspace-sts-3.9.2.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Relocation\WEB-INF\classes\com\projects\repositories\impl\CandidateRepositoryImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ... 24 more Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CandidateRepositoryImpl' defined in file [C:\Users\alon\Documents\workspace-sts-3.9.2.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Relocation\WEB-INF\classes\com\projects\repositories\impl\CandidateRepositoryImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:721) ... 37 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:721) ... 51 more
מרץ 21, 2018 4:39:16 PM org.apache.catalina.core.StandardContext startInternal SEVERE: One or more listeners failed to start. Full details will be found in the appropriate container log file מרץ 21, 2018 4:39:16 PM org.apache.catalina.core.StandardContext startInternal SEVERE: Context [/Relocation] startup failed due to previous errors מרץ 21, 2018 4:39:16 PM org.apache.catalina.core.ApplicationContext log INFO: Closing Spring root WebApplicationContext מרץ 21, 2018 4:39:16 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc SEVERE: The web application [/Relocation] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. מרץ 21, 2018 4:39:16 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads SEVERE: The web application [/Relocation] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. מרץ 21, 2018 4:39:16 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"] מרץ 21, 2018 4:39:16 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["ajp-bio-8009"] מרץ 21, 2018 4:39:16 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 17422 ms
Upvotes: 0
Views: 8814
Reputation: 1020
Being pragmatic, you're trying to extend SimpleJpaRepository<T, ID>
, but once you disabling it Spring does not load any implementation of JpaEntityInformation
and thus the exception.
Also, as @jens-schauder said, your mixing concepts.
Try to follow this very well explained tutorial to see if it helps.
Upvotes: 3
Reputation: 81988
You are doing a somewhat weird combination of things here.
On the one hand, you effectively disable all the Spring Data stuff by using @NoRepositoryBean
on CandidateRepository
and providing your own implementation. But then your implementation is based extends SimpleJpaRepository<T, ID>
which is a core component of Spring Data but it doesn't get configured properly since you basically disabled it.
I guess it's time to make up your mind:
Do you want to change the behavior that is provided by SimpleJpaRepository<T, ID>
? Possibly adding methods to your base implementation.
If so extend it, leaving constructors as they are and use it as your repositoryBaseClass
. Here is a tutorial how to do that.
Just provide custom implementation for one (or some) repositories. Then use custom implementations.
Upvotes: 5