从EntityManager抛出NullPointerException [英] NullPointerException thrown from EntityManager
本文介绍了从EntityManager抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对EntityManager
有问题,它引发了NullPointerException
.
这是代码:
I have a problem with an EntityManager
, it throws a NullPointerException
.
This is the code:
package com.smartpump.service.jpa;
import org.springframework.stereotype.Service;
import com.smartpump.persistent.entity.Person;
import com.smartpump.service.PersonService;
import java.util.List;
import javax.jdo.annotations.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Service("personService")
public class PersonServiceJpa implements PersonService {
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public EntityManager getEntityManager() {
return entityManager;
}
@Override
public String getNombre() {
return "Roberto";
}
@SuppressWarnings("unchecked")
@Transactional
public List<Person> getAll() {
Query query = entityManager.createNamedQuery("Person.findAll"); <--Here is the problem
List<Person> persons = null;
persons = query.getResultList();
return persons;
}
}
代码 Query query = entityManager.createNamedQuery("Person.findAll");
是NullPointerException
出现在entityManager
句子中的位置.
The code Query query = entityManager.createNamedQuery("Person.findAll");
is where the NullPointerException
appears, in the entityManager
sentence.
这是代码的其他部分.
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="transactions-optional">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<!-- <properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
<property name="datanucleus.appengine.datastoreReadConsistency" value="EVENTUAL" />
</properties> -->
</persistence-unit>
</persistence>
<!--
transaction-type="RESOURCE_LOCAL">
<class>com.persistent.entity.Person</class>
-->
applicationContext
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>\WEB-INF\database.properties</value>
</list>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="personService"
class="com.smartpump.service.jpa.PersonServiceJpa"></bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"
lazy-init="true">
<property name="persistenceUnitName" value="transactions-optional" />
</bean>
<bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
非常感谢!
推荐答案
尝试在EntityManager本身上使用@PersistenceContext,
Try to use @PersistenceContext on EntityManager itself,
@PersistenceContext
Private EntityManager entityManager;
我不是说它可以使用setter方法
I am not that it works on setter method
这篇关于从EntityManager抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文