2

I'm trying to move my application from JDBC to Spring JPA.

I have extended CrudRepository [of org.springframework.data.repository] and created my own DAO Interface

public interface IMyRepository extends CrudRepository<MyEntity, Long> { // Queries } 

But, IMyRepositoty bean is not getting created with below XML 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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:repository="http://www.springframework.org/schema/data/repository" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:component-scan base-package="com.project.dao" /> <bean id="projectDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire-candidate="true"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://ABC:3306/project" /> <property name="username" value="userA" /> <property name="password" value="1234" /> </bean> <bean id="projectEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="projectDataSource" /> <property name="packagesToScan" value="com.project.dao" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">create-drop</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> </props> </property> </bean> <bean id="projectJpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="projectEntityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="projectJpaTransactionManager" /> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <!-- <jpa:repositories base-package="com.project.dao"/> --> </beans> 

The error is:

No qualifying bean of type [com.project.dao.IMyRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

After searching many articles, found that need to add <jpa:repositories base-package="com.project.dao"/> into XML. But that is resulting in another error as below

Caused by: org.xml.sax.SAXParseException; lineNumber: 68; columnNumber: 69; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'jpa:repositories'. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) 

Am I missing something? Thanks in advance!

More Info, In dependencies of module I have:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.0.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.0.5.RELEASE</version> </dependency> </dependencies> 

And parent module have dependency on : spring-jdbc, spring-test, spring-core, spring-context-support, spring-orm all of version 4.1.6.RELEASE

2 Answers 2

2

From Spring Docs on JPA repository :

You need to add this namespace JPA Repository Support :

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

As well as JPA schema location :

xsi:schemaLocation="http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" 

This is missing from your xml file. That's why you are getting that schema error.


Try this :

<?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: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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <jpa:repositories base-package="com.project.dao" /> </beans> 

It should work.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Anish, thanks for the reply. Yes, adding springframework.org/schema/data/jpa springframework.org/schema/data/jpa/spring-jpa.xsd indeed solve the issue. But why can't I use CrudRepository!
Typo was only on stackoverflow. No worries there.
@Jake You can use crudrepository also.
@Jake Check here for difference between crudrepository and jparepository :stackoverflow.com/questions/14014086/…
After those changes I'm running into another error ** Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IMyRepository': Initialization of bean failed; nested exception is java.lang.AbstractMethodError at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) **
|
0

On your Application class you have to use the:

@EnableJpaRepositories(basePackages = "your repositories directory")

and annotate the IMyRepositoty as a

@Repository

3 Comments

The problem might be with your XML declaration header refer to this thread : stackoverflow.com/questions/12542616/…
There is no application class, hence the configuration is in XML :|
@Jake yes i commented a link the related issue under my first proposal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.