Jersey JPA - 500仅在从SP返回带有参数的结果集时

时间:2015-02-17 13:29:56

标签: java rest jpa stored-procedures jersey

我正在使用Jersey和JPA。如果不使用带参数的存储过程,我的查询将按预期工作。如果存储过程使用参数,则查询到数据库的数据将完美返回。在调试时我可以看到List中的数据整个回到控制器上的return语句然后我得到一个500.(在调试时我收到的消息类没有找到,这给我一点点,因为没有参数一切正常精细)。任何帮助将不胜感激。

回购代码

package com.aeolos.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;

import com.aeolos.listener.LocalEntityManagerFactory;
import com.aeolos.model.Employee;
import com.aeolos.model.Callback;


public class EmployeeRepo implements IEmployeeRepo {

EntityManager em = LocalEntityManagerFactory.createEntityManager();

@Override
public Employee getEmployee(long id) {
    try {
        Employee test = new Employee();
        test = em.find(Employee.class, id);
        return test;
    } finally {
        em.close();
    }
}

@Override
public List<Employee> getStoredEmployee() {
    try {
        Query query = em.createStoredProcedureQuery("GetAllEmployees", Employee.class);
        List<Employee> result = query.getResultList();
        return result;
    } finally {
        em.close();
    }
}

@Override
public Employee save(Employee employee) {
    try{
        em.persist(employee);
        em.flush();
        return employee;
    } finally {
        em.close();
    }
}

@Override
public List<Callback> getAllCallbacks() {
    try {
        Query query = em.createStoredProcedureQuery("GetAllCallbacks", Callback.class);
        List<Callback> result = query.getResultList();
        return result;
    } finally {
        em.close();
    }
}


@Override
@SuppressWarnings("unchecked")
public List<Callback> getCallbacksByDateTime(String cbdate, String cbtime) {
    try {
        StoredProcedureQuery query = em.createStoredProcedureQuery("GetCallbacksByDateTime");
        query.registerStoredProcedureParameter("paramDate", String.class, ParameterMode.IN);
        query.registerStoredProcedureParameter("paramTime", String.class, ParameterMode.IN);
        query.setParameter("paramDate", cbdate);
        query.setParameter("paramTime", cbtime);
        query.execute();
        List<Callback> result = query.getResultList();
        return result;
    } finally {
        em.close();
    }
}

@Override
public Callback getCallback(long id) {
    try {
        Callback result = new Callback();
        result = em.find(Callback.class, id);
        return result;
    } finally {
        em.close();
    }
}

@Override
public Callback SaveCallback(Callback callback) {
    try{
        em.getTransaction().begin();
        em.persist(callback);
        em.flush();
        em.getTransaction().commit();
        return callback;
    } finally {         
        em.close();
    }
}

}

服务代码

package com.aeolos.service;

 import java.util.List;

 import org.springframework.transaction.annotation.Transactional;

 import com.aeolos.dao.EmployeeRepo;
 import com.aeolos.model.Callback;
 import com.aeolos.model.Employee;

public class EmployeeService implements IEmployeeService{

private EmployeeRepo repo;

public EmployeeService(EmployeeRepo repo){
    this.repo = repo;
}

@Override
public Employee read(long id) {     
    return repo.getEmployee(id);
}  

@Override
public List<Employee> store() {  
    return repo.getStoredEmployee();
}

public List<Callback> GetAllCallbacks() {
    return repo.getAllCallbacks();
}

public List<Callback> GetCallbacksByDateTime(String cbdate, String cbtime) {
    return repo.getCallbacksByDateTime(cbdate, cbtime);
}

@Override
public Callback GetCallback(long id) {      
    return repo.getCallback(id);
} 

@Override
public Callback SaveCallback(Callback callback) {       
    return repo.SaveCallback(callback);
} 

}

控制器代码(请注意注释掉的方法可以正常工作(没有参数的SP)

package com.aeolos.controller;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;  
import javax.ws.rs.POST;
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.Produces;  
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;  

import com.aeolos.dao.EmployeeRepo;
import com.aeolos.model.Callback;
import com.aeolos.service.EmployeeService;

@Path("/callback") 
public class CallbackController {

private EmployeeRepo repo = new EmployeeRepo();
private EmployeeService service = new EmployeeService(repo);

 //    @GET
//    @Produces(MediaType.APPLICATION_JSON) 
//    public List<Callback> store() {  
//      return service.GetAllCallbacks();
//    }

@GET  
@Produces(MediaType.APPLICATION_JSON) 
public List<Callback> read(@QueryParam("paramdate") String cbdate, @QueryParam("paramtime") String cbtime) {   
    List<Callback> callbacks = service.GetCallbacksByDateTime(cbdate, cbtime);
    return callbacks;
}

@GET  
@Produces(MediaType.APPLICATION_JSON)  
@Path("{id}")  
public Callback read(@PathParam("id") long id) {    
    return service.GetCallback(id);
} 

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)  
public Callback save(
        @FormParam("employeenumber") String employeenumber,
        @FormParam("customer") String customer,
        @FormParam("mobile") String mobile,
        @FormParam("landline") String landline,
        @FormParam("cbtype") String cbtype,
        @FormParam("cbreason") String cbreason,
        @FormParam("cbdate") String cbdate,
        @FormParam("cbtime") String cbtime
        ) {  
    Callback callback = new Callback();
    callback.setEmployeenumber(employeenumber);
    callback.setCustomer(customer);
    callback.setMobile(mobile);
    callback.setLandline(landline);
    callback.setCbtype(cbtype);
    callback.setCbreason(cbreason);
    callback.setCbdate(cbdate);
    callback.setCbtime(cbtime);
    return service.SaveCallback(callback);
}
}

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,我忘了将Callback.class添加到createStoredProcedureQuery调用,这导致结果集没有映射到已知实体。