spring - when I am running test cases that time entity manager is injected successfully but while running web app its throwing NullPointerException -


i have strange problem. injecting entity manager using persistencecontext using applicatiocontext bean. problem when running test cases time entity manager injected while running web app throwing nullpointerexception

this abstractrepository injecting entitymanager

package com.ajit.retail.repository;  import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.persistence.persistencecontexttype;  public class abstractrepository {      @persistencecontext(type = persistencecontexttype.extended)     entitymanager entitymanager; } 

this repository using entity manager

package com.ajit.retail.repository;  import com.ajit.retail.exception.invalididexception; import com.ajit.retail.model.buyer; import org.springframework.stereotype.repository;  import javax.persistence.noresultexception;  @repository public class buyerrepo extends abstractrepository {      public buyer getbuyer(string buyername) throws invalididexception {         javax.persistence.query buyerid = entitymanager.createnativequery("select b.buyer_id buyer b b.name = :name").setparameter("name", buyername);         integer id;          try {             id = (integer) buyerid.getsingleresult();          } catch (noresultexception e) {             return null;         }          return getbuyer(id);     }      public buyer getbuyer(long buyerid) throws invalididexception {         buyer buyer = entitymanager.find(buyer.class, buyerid); **====> null**         if (buyer == null) throw new invalididexception("invalid article id");         return buyer;     } } 

this controller calling repository method

package com.ajit.retail.web;  import com.sun.jersey.spi.inject.inject; import com.ajit.retail.exception.invalididexception; import com.ajit.retail.model.buyer; import com.ajit.retail.repository.*; import org.springframework.stereotype.component;  import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype;  @path("/buyer") @component public class buyercontroller {      @inject     private buyerrepo buyerrepo;      @get     @produces(mediatype.application_json)     public buyer searchfields() throws invalididexception {         string buyerid = "51";         buyer buyer;          try {             buyer = buyerrepo.getbuyer(long.parselong(buyerid));         } catch (numberformatexception e) {             buyer = buyerrepo.getbuyer(buyerid);         }          return buyer;     } } 

these unit tests passing

package com.ajit.retail.repository;

import com.ajit.retail.exception.invalididexception; import org.hamcrest.core.isequal; import org.junit.before; import org.junit.test; import org.springframework.beans.factory.annotation.autowired;  import static org.junit.assert.assertthat;  public class buyerrepotest extends baserepository {      @autowired     private buyerrepo buyerrepo;      @test     public void shouldgivenameofgivenbuyerid() throws invalididexception {         assertthat(buyerrepo.getbuyer(2).getname(), isequal.equalto("supervisordls"));     }      @test(expected = invalididexception.class)     public void shouldthrowexceptionforinvalidbuyerid() throws invalididexception {         buyerrepo.getbuyer(-10);     } } 

this base repository

 package com.ajit.retail.repository;  import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.abstracttransactionaljunit4springcontexttests;  @contextconfiguration(locations = {"classpath:applicationcontext.xml"}) public class baserepository extends abstracttransactionaljunit4springcontexttests {   } 

this applicationcontext.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:mvc="http://www.springframework.org/schema/mvc"        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         http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">      <context:annotation-config/>     <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="entitymanager" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">         <property name="datasource" ref="datasource"/>          <property name="packagestoscan" value="com.ajit.retail"/>          <property name="jpavendoradapter">             <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter">                 <property name="showsql" value="true"/>                 <property name="databaseplatform" value="org.hibernate.dialect.postgresqldialect"/>             </bean>         </property>     </bean>      <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager">         <property name="entitymanagerfactory" ref="entitymanager"/>     </bean>      <tx:annotation-driven transaction-manager="transactionmanager"/>      </beans> 

please help!

i solved problem replacing

 <servlet>         <servlet-name>retail</servlet-name>          **<servlet-class>com.sun.jersey.spi.container.servlet.servletcontainer</servlet-class>**          <init-param>             <param-name>com.sun.jersey.config.property.packages</param-name>             <param-value>com.ajit.retail.web</param-value>         </init-param>          <init-param>             <param-name>com.sun.jersey.api.json.pojomappingfeature</param-name>             <param-value>true</param-value>         </init-param>           <load-on-startup>1</load-on-startup>     </servlet> 

with

<servlet>         <servlet-name>retail</servlet-name>          **<servlet-class>com.sun.jersey.spi.spring.container.servlet.springservlet</servlet-class>**          <init-param>             <param-name>com.sun.jersey.config.property.packages</param-name>             <param-value>com.ajit.retail.web</param-value>         </init-param>          <init-param>             <param-name>com.sun.jersey.api.json.pojomappingfeature</param-name>             <param-value>true</param-value>         </init-param>           <load-on-startup>1</load-on-startup>     </servlet> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -