Guest User

Untitled

a guest
Jul 27th, 2012
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.18 KB | None | 0 0
  1. Spring Data JPA - injection fails - BeanCreationException: Could not autowire field
  2. <jpa:repositories base-package="my.package.to.scan" />
  3.  
  4. <bean id="ClassName" class="my.package.to.scan.ClassName"/>
  5.  
  6. <beans *** ALL MY XMLN's and XSI's *** />
  7. <context:annotation-config />
  8. <jpa:repositories base-package="model.repositories" />
  9.  
  10. // JPA specific configuration here: dataSource, persistenceUnitManager exceptionTranslator, entityManagerFactory, SessionFactory, transactionManager - should not be relevant for this problem, tell me if i'm wrong
  11.  
  12. <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
  13.  
  14. import model.entities.Customer;
  15. public interface ICustomerService {
  16. // example method
  17. public Customer getCustomer(Long customerId);
  18. }
  19.  
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.data.domain.Page;
  22. import org.springframework.data.domain.Pageable;
  23. import org.springframework.stereotype.Repository;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import model.entities.Customer;
  26. import model.repositories.CustomerRepository;
  27. import model.service.interfaces.ICustomerService;
  28. @Repository
  29. @Transactional(readOnly = true)
  30. public class CustomerService implements ICustomerService{
  31. @Autowired
  32. private CustomerRepository repository;
  33.  
  34. // example method
  35. @Override
  36. public Customer getCustomer(Long customerId){
  37. return repository.findById(customerId);
  38. }
  39.  
  40. import javax.annotation.Resource;
  41. import org.springframework.data.jpa.repository.JpaRepository;
  42. import org.springframework.transaction.annotation.Transactional;
  43. import model.entities.Customer;
  44. @Resource
  45. @Transactional(readOnly = true)
  46. public interface CustomerRepository extends JpaRepository<Customer, Long>{
  47.  
  48. public Customer findById(Long id);
  49. }
  50.  
  51. import javax.persistence.Column;
  52. import javax.persistence.Entity;
  53. import javax.persistence.GeneratedValue;
  54. import javax.persistence.GenerationType;
  55. import javax.persistence.Id;
  56. import javax.persistence.Table;
  57.  
  58. @Entity
  59. @Table(name = "Customers")
  60. public class Customer{
  61.  
  62. @Id
  63. @GeneratedValue(strategy=GenerationType.IDENTITY)
  64. @Column(name = "ID_CUSTOMER")
  65. private Long id;
  66.  
  67. @Column(name = "dbfirstname")
  68. private String firstName;
  69.  
  70. @Column(name = "dbname")
  71. private String lastName;
  72.  
  73. public Long getId(){
  74. return id;
  75. }
  76.  
  77. public String getFirstName(){
  78. return firstName;
  79. }
  80.  
  81. public void setFirstName(String firstName){
  82. this.firstName = firstName;
  83. }
  84.  
  85. public String getLastName(){
  86. return lastName;
  87. }
  88.  
  89. public void setLastName(String lastName){
  90. this.lastName = lastName;
  91. }
  92. }
  93.  
  94. 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
  95. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
  96. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
  97. [...]
  98. at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:522)
  99. at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1563)
  100. Caused 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
  101. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
  102. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
  103. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
  104. ... 96 more
  105. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
  106. at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
  107. at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
  108. at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
  109. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)
  110. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
  111. at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
  112. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
  113. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
  114. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
  115. ... 98 more
  116. Caused by: java.lang.NullPointerException
  117. at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73)
  118. at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115)
  119. at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
  120. at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1215)
  121. at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:177)
  122. at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89)
  123. at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:179)
  124. at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:174)
  125. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  126. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
  127. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  128. at java.lang.reflect.Method.invoke(Method.java:600)
  129. at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:376)
  130. at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:517)
  131. at $Proxy325.createEntityManager(Unknown Source)
  132.  
  133. at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:234)
  134. at $Proxy328.createNamedQuery(Unknown Source)
  135. at org.springframework.data.jpa.repository.query.NamedQuery.<init>(NamedQuery.java:74)
  136. at org.springframework.data.jpa.repository.query.NamedQuery.lookupFrom(NamedQuery.java:96)
  137. at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:128)
  138. at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
  139. at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:71)
  140. at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:303)
  141. at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:157)
  142. at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)
  143. at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)
  144. at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
  145.  
  146. <context:annotation-config />
  147.  
  148. <jpa:repositories base-package="model.repositories" />
  149.  
  150. <context:component-scan base-package="model,model.repositories,model.service,controller" />
  151.  
  152. <bean class="model.service.CustomerService"/>
  153. <bean class="model.service.OrderService"/>
  154. <bean class="model.repositories.CustomerRepository"/>
  155. <bean class="model.repositories.OrderRepository"/>
  156.  
  157.  
  158. <bean id="myExceptionTranslator" class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
  159.  
  160. <jee:jndi-lookup id="dataSource" jndi-name="jdbc/mydata"
  161. resource-ref="true" cache="true" />
  162.  
  163.  
  164. <bean id="pum"
  165. class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
  166. <property name="persistenceXmlLocations">
  167. <list>
  168. <value>classpath*:META-INF/OverridePersistence.xml</value>
  169. </list>
  170. </property>
  171. <property name="defaultDataSource" ref="dataSource" />
  172. </bean>
  173.  
  174.  
  175. <bean id="entityManagerFactory"
  176. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  177. <property name="dataSource" ref="dataSource" />
  178. <property name="jpaVendorAdapter">
  179. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  180. <property name="generateDdl" value="true" />
  181. <property name="database" value="MYSQL" />
  182. </bean>
  183. </property>
  184. <property name="persistenceUnitManager" ref="pum" />
  185. <property name="persistenceUnitName" value="default" />
  186. </bean>
  187.  
  188. <bean id="mySessionFactory"
  189. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  190. <property name="dataSource" ref="dataSource" />
  191. <property name="packagesToScan" value="model"/>
  192. <property name="hibernateProperties">
  193. <value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value>
  194. </property>
  195. </bean>
  196.  
  197. <bean id="transactionManager"
  198. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  199. <property name="entityManagerFactory" ref="entityManagerFactory"/>
  200. <property name="sessionFactory" ref="mySessionFactory" />
  201. </bean>
  202.  
  203. <tx:annotation-driven />
  204.  
  205. <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
  206.  
  207. git clone git://github.com/zzantozz/testbed tmp
  208. cd tmp/stackoverflow/10539417-basic-spring-data-jpa
  209. mvn -q compile exec:java -Dexec.mainClass=rds.testbed.springDataJpa.SpringDataJp
Advertisement
Add Comment
Please, Sign In to add comment