Domanda

I am facing a problem to wire entity manager with the bean present in application context. whenever i do some operation it gives NullPointerException.

this is my applicationContext.xml file

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.ajit.retail"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost/retail"/>
        <property name="username" value="retail_user"/>
        <property name="password" value="password"/>
    </bean>

    <bean id="entityManagerOne" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>
                <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
            </bean>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerOne"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    </beans>

this is the java file in which i am creating the entity manager

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class AbstractRepository {

    @PersistenceContext
    EntityManager entityManager;
}

so whenever i use this entity manager it gives null pointer exception please help!

È stato utile?

Soluzione 2

my application context was not on the correct place (src/main/resources). Now I put that there and its working.

Altri suggerimenti

Your entity manager bean is called enetityManagerOne, but the variable is called entityManager. Maybe renamme your bean in your XML file:

<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

Another solution could be you forgot the following bean declarations:

For the support for transaction:

<tx:annotation-driven/>

The support for parsing JPA annotations:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

Use this code in your dispatcher-servlet.xml

<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence.xml</value>
            </list>
        </property>
        <property name="defaultDataSource" ref="dataSource"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
        <property name="persistenceUnitName" value="etray"/>
    </bean>
    <jee:jndi-lookup id="dataSource" jndi-name="java:/prateek" />


<!-- we plan to use JTA transactions and declare them using annotations -->
    <bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager" />
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- to inject instances of EntityManager using @PersistenceContext annotation -->
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

it resolve my problem, hope it will help.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top