简体   繁体   中英

Spring is unable to inject an EntityManager using @PersistenceContext

I have a Java 8/spring 4.3.5.RELEASE web application tha runs on a wildfly 10 server. I use a persistence.xml file. I enabled trace logging on the jboss jpa and hibernate classes and I can see this file gets picked up and is resolved smoothly into a persistence unit:

DEBUG [] [org.hibernate.jpa.internal.util.LogHelper] PersistenceUnitInfo [
    name: testcontext
    persistence provider classname: org.hibernate.jpa.HibernatePersistenceProvider
    classloader: ModuleClassLoader for Module "deployment.BasicWebapp.war:main" from Service Module Loader
    excludeUnlistedClasses: false
    JTA datasource: org.jboss.as.connector.subsystems.datasources.WildFlyDataSource@fb80232
    Non JTA datasource: null
    Transaction type: JTA
    PU root URL: vfs:/C:/Users/Me/Wildfly 10.0.0/standalone/deployments/BasicWebapp.war/WEB-INF/classes/
    Shared Cache Mode: UNSPECIFIED
    Validation Mode: AUTO
    Jar files URLs []
    Managed classes names [
        com.company.project.data.User]
    Mapping files names []
    Properties [
        jboss.entity.manager.jndi.name: persistence/testcontext]

I now want a dao class to have an entity manager injected by spring:

UserDao.class

@Repository
public class UserDao
{
    @PersistenceContext(unitName = "testcontext")
    private EntityManager entityManager;
}

I have component scanning and annotation config so both the @Repository and the @PersistenceContext annotation gets processed upon starting my application:

spring-servlet.xml

<context:component-scan base-package="com.company.project" />
<context:annotation-config/>

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/testcontext"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>

<tx:annotation-driven/>

The injection fails though, in two possible ways:

If I use @PersistenceContext(unitName = "testcontext") , the error is: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testcontext' available

If I use @PersistenceContext , the error is:

Caused by: java.lang.NullPointerException
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:580)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:546)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:707)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:680)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:354)
    ... 44 more

So I'm missing something in this configuration to tell the part of Spring that processes the @PersistenceContext annotation to look at either the entityManagerFactory bean that I declared in the spring-servlet.xml or just use the container's persistence unit directly. What do I need to add to achieve that?

I'm also a bit sketchy on the transactionmanager part. Does wildfly provide the transactionmanager or not? If it does, do I need to create a bean for it in Spring (will it pick up the one created by jboss or make its own one?)

I think you should double check you configuration setup against Spring official docs : https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#orm-jpa-setup-jndi .No, the transaction manager is provided by Spring. More info on this: https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#transaction-strategies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM