{"id":5593,"date":"2014-04-02T08:22:02","date_gmt":"2014-04-02T08:22:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/02\/spring-data-jpa-injection-fails-beancreationexception-could-not-autowire-field-collection-of-common-programming-errors\/"},"modified":"2014-04-02T08:22:02","modified_gmt":"2014-04-02T08:22:02","slug":"spring-data-jpa-injection-fails-beancreationexception-could-not-autowire-field-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/02\/spring-data-jpa-injection-fails-beancreationexception-could-not-autowire-field-collection-of-common-programming-errors\/","title":{"rendered":"Spring Data JPA &#8211; injection fails &#8211; BeanCreationException: Could not autowire field-Collection of common programming errors"},"content":{"rendered":"<p>i followed the tutorial posted here to get a basis application to work with Spring Data JPA. Now, how i understood, using the configuration<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>should result in that package beeing scanned by Spring Data JPA for interfaces extending <code>JpaRepository<\/code> and create a concreate bean of it so it can be used anywhere in my service classes using simple Spring <code>@Autowired<\/code>. But it fails, saying it can&#8217;t find a bean with className (which is the default name the bean gets when created, simply using the de-capitalized ClassName).<\/p>\n<p>However, when i configure the bean manualy in my applicationContext like this:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>Spring is able to find the bean. I then of course get an error because i want to create a bean from an interface, which obviously can&#8217;t work. BUT the point is that it seems like the Spring Data JPA &#8220;automatic bean creation&#8221; seems to fail somehow.<\/p>\n<p>I attached the relevant code so you can look at it. Btw, i should mention that i&#8217;m developing a portlet, so don&#8217;t wonder why i dont have a spring-config. I&#8217;m currently using a applicationConfig only plus an MyPortlet-Portlet.xml for portlet configurations (but that should be not relevent for this problem). I added the import statements just to make sure i&#8217;m not using the wrong annotions \/ classes.<\/p>\n<p><strong>applicationContext.xml<\/strong><\/p>\n<pre><code>\n\n\n\n\/\/ JPA specific configuration here: dataSource, persistenceUnitManager exceptionTranslator, entityManagerFactory, SessionFactory, transactionManager - should not be relevant for this problem, tell me if i'm wrong\n\n\n<\/code><\/pre>\n<p><strong>ICustomerService &#8211; just a interface for the CustomerService<\/strong><\/p>\n<pre><code>import model.entities.Customer;\npublic interface ICustomerService {\n        \/\/ example method\n    public Customer getCustomer(Long customerId);   \n}\n<\/code><\/pre>\n<p><strong>CustomerService &#8211; the class used by my application logic to get \/ set ORM data<\/strong><\/p>\n<pre><code>import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.domain.Page;\nimport org.springframework.data.domain.Pageable;\nimport org.springframework.stereotype.Repository;\nimport org.springframework.transaction.annotation.Transactional;\nimport model.entities.Customer;\nimport model.repositories.CustomerRepository;\nimport model.service.interfaces.ICustomerService;\n@Repository\n@Transactional(readOnly = true)\npublic class CustomerService implements ICustomerService{\n    @Autowired\n    private CustomerRepository repository;\n\n    \/\/ example method\n    @Override\n    public Customer getCustomer(Long customerId){\n        return repository.findById(customerId);\n    }\n<\/code><\/pre>\n<p><strong>CustomerRepository &#8211; the repository for Spring Data JPA<\/strong><\/p>\n<pre><code>import javax.annotation.Resource;\nimport org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.transaction.annotation.Transactional;\nimport model.entities.Customer;\n@Resource\n@Transactional(readOnly = true)\npublic interface CustomerRepository extends JpaRepository{\n\n    public Customer findById(Long id);\n}\n<\/code><\/pre>\n<p><strong>Customer &#8211; my sample entity<\/strong><\/p>\n<pre><code>import javax.persistence.Column;\nimport javax.persistence.Entity;\nimport javax.persistence.GeneratedValue;\nimport javax.persistence.GenerationType;\nimport javax.persistence.Id;\nimport javax.persistence.Table;\n\n@Entity\n@Table(name = \"Customers\")\npublic class Customer{\n\n    @Id\n    @GeneratedValue(strategy=GenerationType.IDENTITY)\n    @Column(name = \"ID_CUSTOMER\")\n    private Long    id;\n\n    @Column(name = \"dbfirstname\")\n    private String  firstName;\n\n    @Column(name = \"dbname\")\n    private String  lastName;\n\n    public Long getId(){\n        return id;\n    }\n\n    public String getFirstName(){\n        return firstName;\n    }\n\n    public void setFirstName(String firstName){\n        this.firstName = firstName;\n    }\n\n    public String getLastName(){\n        return lastName;\n    }\n\n    public void setLastName(String lastName){\n        this.lastName = lastName;\n    }\n}\n<\/code><\/pre>\n<p>i just came from classpath hell with WebSphere (damn, what a fu**ed up product) and now i&#8217;m here. hope somebody can help me with this.<\/p>\n<p>A basic explanation of what exacly goes wrong and maybe providing a better understanding of springs autowired injection feature would be great. I&#8217;ve read the spring documentation, but to say the truth: there are so many ways to configure something and it&#8217;s not quite visible to me WHAT is really needed when choosing one of the config styles.<\/p>\n<p><strong>EDIT<\/strong><\/p>\n<p>After trying to update the project i&#8217;m still getting the error. as requested here a little more details (trace):<\/p>\n<pre><code>Exception created : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private model.repositories.CustomerRepository model.service.CustomerService.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException\n    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)\n    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)\n    [...]\n        at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:522)\n    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1563)\nCaused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private model.repositories.CustomerRepository model.service.CustomerService.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException\n    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)\n    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)\n    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)\n    ... 96 more\nCaused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException\n    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)\n    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)\n    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)\n    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)\n    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)\n    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)\n    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)\n    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)\n    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)\n    ... 98 more\nCaused by: java.lang.NullPointerException\n    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73)\n    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115)\n    at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)\n    at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1215)\n    at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:177)\n    at org.hibernate.ejb.EntityManagerImpl.(EntityManagerImpl.java:89)\n    at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:179)\n    at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:174)\n    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)\n    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n    at java.lang.reflect.Method.invoke(Method.java:600)\n    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:376)\n    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:517)\n    at $Proxy325.createEntityManager(Unknown Source)\n\n    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:234)\n    at $Proxy328.createNamedQuery(Unknown Source)\n    at org.springframework.data.jpa.repository.query.NamedQuery.(NamedQuery.java:74)\n    at org.springframework.data.jpa.repository.query.NamedQuery.lookupFrom(NamedQuery.java:96)\n    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:128)\n    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)\n    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:71)\n    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:303)\n    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:157)\n    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)\n    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)\n    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)\n<\/code><\/pre>\n<p><strong>EDIT #2<\/strong> compleate applicationContext.xml (includeing the changes i made based on the ongoing discussion) added as requested<\/p>\n<pre><code>\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n    \n        \n            classpath*:META-INF\/OverridePersistence.xml\n        \n    \n    \n\n\n\n\n    \n    \n        \n            \n            \n        \n    \n    \n    \n\n\n\n    \n    \n    \n        hibernate.dialect=org.hibernate.dialect.MySQLDialect\n    \n\n\n\n    \n    \n\n\n\n\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>i followed the tutorial posted here to get a basis application to work with Spring Data JPA. Now, how i understood, using the configuration should result in that package beeing scanned by Spring Data JPA for interfaces extending JpaRepository and create a concreate bean of it so it can be used anywhere in my service [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5593","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5593"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5593\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}