java - Spring Data JPA (CrudRepository) - BeanCreationException: Could not autowire field -
i trying use spring data jpa - crudrepository use without implementation (all default options).
with code having exception:
exception in thread "main" org.springframework.beans.factory.beancreationexception: error creating bean name 'springjpacontactservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.sample.hibernate.contactrepository com.sample.hibernate.repositorycontactserviceimpl.contactrepository; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'contactrepository': invocation of init method failed; nested exception java.lang.abstractmethoderror: org.springframework.data.repository.core.support.repositoryfactorysupport.gettargetrepository(lorg/springframework/data/repository/core/repositoryinformation;)ljava/lang/object; @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:334) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1214) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:305) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:301) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:196) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:772) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:834) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:537) @ com.sample.hibernate.hibernatejpaapplication.main(hibernatejpaapplication.java:15)
there classes responsible crudrepository:
contactrepository:
package com.sample.hibernate; import java.util.list; import org.springframework.data.repository.crudrepository; public interface contactrepository extends crudrepository<contact, long> { list<contact> findbyfirstname(string firstname); list<contact> findbyfirstnameandlastname(string firstname, string lastname); }
repositorycontactservice:
package com.sample.hibernate; import java.util.list; public interface repositorycontactservice { list<contact> findall(); list<contact> findbyfirstname(string firstname); list<contact> findbyfirstnameandlastname(string firstname, string lastname); }
repositorycontactserviceimpl:
package com.sample.hibernate; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.repository; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; import com.google.common.collect.lists; @service("springjpacontactservice") @repository @transactional public class repositorycontactserviceimpl implements repositorycontactservice { @autowired private contactrepository contactrepository; @transactional(readonly = true) public list<contact> findall() { return lists.newarraylist(contactrepository.findall()); } @transactional(readonly = true) public list<contact> findbyfirstname(string firstname) { return contactrepository.findbyfirstname(firstname); } @transactional(readonly = true) public list<contact> findbyfirstnameandlastname(string firstname, string lastname) { return contactrepository .findbyfirstnameandlastname(firstname, lastname); } }
contact:
package com.sample.hibernate; import java.io.serializable; import java.util.date; import java.util.hashset; import java.util.set; import javax.persistence.cascadetype; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.entityresult; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.jointable; import javax.persistence.manytomany; import javax.persistence.namedqueries; import javax.persistence.namedquery; import javax.persistence.onetomany; import javax.persistence.sqlresultsetmapping; import javax.persistence.table; import javax.persistence.temporal; import javax.persistence.temporaltype; import javax.persistence.version; @entity @table(name = "contact") @namedqueries({ @namedquery(name="contact.findall", query="select c contact c"), @namedquery(name = "contact.findallwithdetail", query = "select distinct c contact c left join fetch c.contactteldetails t left join fetch c.hobbies h"), @namedquery(name = "contact.findbyid", query = "select distinct c contact c left join fetch c.contactteldetails t left join fetch c.hobbies h c.id = :id") }) @sqlresultsetmapping(name="contactresult", entities=@entityresult(entityclass=contact.class) ) public class contact implements serializable { private static final long serialversionuid = -8008307767408320097l; private long id; private int version; private string firstname; private string lastname; private date birthdate; private set<contactteldetail> contactteldetails = new hashset<contactteldetail>(); private set<hobby> hobbies = new hashset<hobby>(); @id @generatedvalue(strategy = generationtype.identity) @column(name = "id") public long getid() { return id; } public void setid(long id) { this.id = id; } @version @column(name = "version") public int getversion() { return version; } public void setversion(int version) { this.version = version; } @column(name = "first_name") public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } @column(name = "last_name") public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } @temporal(temporaltype.date) @column(name = "birth_date") public date getbirthdate() { return birthdate; } public void setbirthdate(date birthdate) { this.birthdate = birthdate; } @onetomany(mappedby = "contact", cascade = cascadetype.all, orphanremoval = true) public set<contactteldetail> getcontactteldetails() { return contactteldetails; } public void setcontactteldetails(set<contactteldetail> contactteldetails) { this.contactteldetails = contactteldetails; } public void addcontactteldetail(contactteldetail contactteldetail) { contactteldetail.setcontact(this); getcontactteldetails().add(contactteldetail); } public void removecontactteldetail(contactteldetail contactteldetail) { getcontactteldetails().remove(contactteldetail); } @manytomany @jointable(name = "contact_hobby_detail", joincolumns = @joincolumn(name = "contact_id"), inversejoincolumns = @joincolumn(name = "hobby_id")) public set<hobby> gethobbies() { return hobbies; } public void sethobbies(set<hobby> hobbies) { this.hobbies = hobbies; } @override public string tostring() { return "contact [id=" + id + ", version=" + version + ", firstname=" + firstname + ", lastname=" + lastname + ", birthdate=" + birthdate + "]"; } }
app-config.xml:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemalocation=" http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <jdbc:embedded-database id="datasource" type="h2"> <jdbc:script location="classpath:sql/schema.sql" /> <jdbc:script location="classpath:sql/test-data.sql" /> </jdbc:embedded-database> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="emf" /> </bean> <tx:annotation-driven transaction-manager="transactionmanager" /> <bean id="emf" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="datasource" /> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter" /> </property> <property name="packagestoscan" value="com.sample.hibernate" /> <property name="jpaproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.h2dialect</prop> <prop key="hibernate.max_fetch_depth">3</prop> <prop key="hibernate.jdbc.fetch_size">50</prop> <prop key="hibernate.jdbc.batch_size">10</prop> <prop key="hibernate.show_sql">false</prop> </props> </property> </bean> <context:component-scan base-package="com.sample.hibernate" /> <jpa:repositories base-package="com.sample.hibernate" entity-manager-factory-ref="emf" transaction-manager-ref="transactionmanager" /> </beans>
i glad clue.. example book , checked 3 times + online tutorials , have no clue why not working expected...
everything annotations fine - no need move @repository contactrepository. because contactrepository extends crudrepository tells spring can autowire reference in repositorycontactserviceimpl. problem in maven configuration (as thinking correctly , that). changed:
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.0.m2</version> <relativepath /> <!-- lookup parent repository --> </parent>
to
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.0.2.release</version> <relativepath /> <!-- lookup parent repository --> </parent>
and repository works expected.
Comments
Post a Comment