2017-09-30 36 views
0

私はJavaのWebアプリケーションプロジェクトのサービスの一部には、このコードを書いた:私はcheckUserNameメソッドを使用せずに新しいレコードを挿入しようとしたときSpringトランザクションでEntityManagerがnullになるのはなぜですか?

@Service 
@Transactional 
public class PersonService extends BaseService { 
    @PersistenceContext 
    private EntityManager entityManager; 

    public void insert(Person person) { 
     entityManager.persist(person); 
    } 

    public boolean checkUserName(String userName) { 
     try { 

       System.out.println("Entity null ni"); 
       Person person = (Person) entityManager.createQuery("select entity from person entity where entity.USER_NAME=:userName") 
         .setParameter("userName", userName) 
         .getSingleResult(); 
       if (person == null) 
        return true; 
       else { 
        return false; 
       } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return false; 
     } 
    } 
} 

、レコードが正しく挿入されるだろうとのEntityManagerは、この内のNULLではなかったです私は最終的に私はEntityManagerのはnull.whyのEntityManagerは春Transaction.thisでnullであることを理解NullPointerException.Iはnullになる可能性が私のコード内のすべてのオブジェクトをチェックしまし繰り返しユーザー名をチェックしたいときの条件はなく、spring.xml

<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-3.2.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

    <!--SpringMVC--> 
    <context:component-scan base-package="Laitec.Doctor"/> 
    <mvc:annotation-driven/> 
    <mvc:resources mapping="/resources/**" location="/resources/"/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/"/> 

    </bean> 
    <!-- SpringTransaction--> 
    <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="packagesToScan" value="Laitec.Doctor.model.entity"/><!--ToPackageAddress--> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
     </property> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
       <!--<prop key="hibernate.hbm2ddl.auto">create-drop</prop>--> 
       <prop key="hibernate.show_sql">false</prop> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
      </props> 
     </property> 
    </bean> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
     <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/> 
     <property name="username" value="username"/> 
     <property name="password" value="password"/> 
    </bean> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactoryBean"/> 
    </bean> 
    <tx:annotation-driven/> 
</beans> 
+0

おそらくコントローラからそれを呼び出す必要があります。しかし、例外と関連するコードの完全なスタックトレースなしで言うことは不可能です。 –

答えて

0
です

多分あなたはPersonServiceを呼び出してインスタンス化していますかw PersonService()?この場合、Springは操作を実行していないため、フィールド 'entityManager'はnullです。あなたは、あなたのアプリケーション・コンテキスト

applicationContext.getBean(PersonService.class); 

からBeanを取得したり、新しい使用してサービスインスタンスを作成したので、テストまたは

@RestController 
@GetMapping("people") 
public class PersonController() { 

    @Autowired 
    private PersonService personService; 

    @PostMapping 
    public void home(@RequestBody Person person) { 
     personService.insert(person); 
    } 
} 
+0

あなたは正しいですか?コントローラ内のpersonServiceにAutowiredを使用し、正しく挿入されているので、他のクラスでcheckUserNameメソッドを使用し、personServiceの新しいインスタンスを作成してnullPointerExceptionを発生させました。 –

+0

checkUserNameをどこで使用しているのか分かりませんが、ヘルパーやutilクラスを作成して@Componentでマークし、希望のスコープで使用することができます。もう一つの方法はコンストラクタでIoCを使うことです(最近推奨されています):baeldung.com/constructor-injection-in-spring –

関連する問題