您好我正在开发一个服务于RESTful WebServices的Web应用程序。
但是在我的服务中,实体管理器似乎为空..
我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="SoruPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.cag.kume.sorubankasi.soru.model.OgretmenDers</class>
<class>com.cag.kume.sorubankasi.soru.model.SinavTest</class>
<class>com.cag.kume.sorubankasi.soru.model.SoruDuzeyi</class>
<class>com.cag.kume.sorubankasi.soru.model.MasterSoru</class>
<class>com.cag.kume.sorubankasi.soru.model.DersSinif</class>
<class>com.cag.kume.sorubankasi.soru.model.SoruBasamagi</class>a
<class>com.cag.kume.sorubankasi.soru.model.SinavTestSoru</class>
<class>com.cag.kume.sorubankasi.soru.model.Ders</class>
<class>com.cag.kume.sorubankasi.soru.model.SoruHaritasi</class>
<class>com.cag.kume.sorubankasi.soru.model.Sinif</class>
<class>com.cag.kume.sorubankasi.soru.model.Soru</class>
<class>com.cag.kume.sorubankasi.soru.model.SoruHaritasiTerim</class>
<class>com.cag.kume.sorubankasi.soru.model.SoruHaritasiSembol</class>
<class>com.cag.kume.sorubankasi.soru.model.YoneticiTipi</class>
<class>com.cag.kume.sorubankasi.soru.model.Ogretmen</class>
<class>com.cag.kume.sorubankasi.soru.model.Yonetici</class>
<class>com.cag.kume.sorubankasi.soru.model.SoruTuru</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:jtds:sqlserver://xx.xx.xxx.xxx:xxxx/kume_harita"/>
<property name="javax.persistence.jdbc.password" value="xxxxx"/>
<property name="javax.persistence.jdbc.driver" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="xxxxx"/>
</properties>
MY AbstractFacade.java:
package com.cag.kume.sorubankasi.soru.service;
import java.util.List;
import javax.persistence.EntityManager;
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
// getEntityManager().getTransaction().begin();
getEntityManager().persist(entity);
// getEntityManager().flush();
// getEntityManager().getTransaction().commit();
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
我的SoruDuzeyiFacadeRest.java(具体服务)是:
package com.cag.kume.sorubankasi.soru.service;
import com.cag.kume.sorubankasi.soru.model.SoruDuzeyi;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("soruduzeyi")
public class SoruDuzeyiFacadeREST extends AbstractFacade<SoruDuzeyi> {
@PersistenceUnit(unitName = "SoruPU")
private EntityManager em;
public SoruDuzeyiFacadeREST() {
super(SoruDuzeyi.class);
}
@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(SoruDuzeyi entity) {
super.create(entity);
}
@PUT
@Path("{id}")
@Consumes({"application/xml", "application/json"})
public void edit(@PathParam("id") Integer id, SoruDuzeyi entity) {
super.edit(entity);
}
@DELETE
@Path("{id}")
public void remove(@PathParam("id") Integer id) {
super.remove(super.find(id));
}
@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public SoruDuzeyi find(@PathParam("id") Integer id) {
//return new SoruDuzeyi(1, "cenk", "123", true);
return super.find(id);
}
@GET
@Override
@Produces({"application/xml", "application/json"})
public java.util.List<SoruDuzeyi> findAll() {
return super.findAll();
}
@GET
@Path("{from}/{to}")
@Produces({"application/xml", "application/json"})
public java.util.List<SoruDuzeyi> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
return super.findRange(new int[]{from, to});
}
@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
return String.valueOf(super.count());
}
@Override
protected EntityManager getEntityManager() {
//return entityManager;
return em;
}
}
我得到了:
java.lang.NullPointerException
at com.cag.kume.sorubankasi.soru.service.AbstractFacade.findAll(AbstractFacade.java:45)
at com.cag.kume.sorubankasi.soru.service.SoruDuzeyiFacadeREST.findAll(SoruDuzeyiFacadeREST.java:67)
有人能看到问题吗?提前谢谢。