“我正在基于该2个ID发送两个ID(州ID和城市ID),我需要从数据库中获取一些数据”
// this is my service class
@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path("/getstatustill")
public Response getStatustill(Statustillrequest statustillrequest, @HeaderParam("Content-Type") String type )
{
Logger custLogger = Logger.getLogger("AdminService.java");
custLogger.info("=====status till service called");
List<Statustillresponse> statustillresponse=null;
try{
statustillresponse= dao.getStausstills(statustillrequest);
}catch(Exception e){
return Response.status(500).type(type).entity(new ErrorResponse("ERR500", UserPrompts.STATUSTILL_NOTFOUND)).build();
}
return null;
}
//this is my DAO class
public List<Statustillresponse> getStausstills(Statustillrequest statustillrequest) {
Statustillresponse statustillresponse=null;
List<Statustillresponse> response=new ArrayList<>();
Logger custLogger = Logger.getLogger("AdminDao.java");
EntityManagerFactory EMF = EntityManagerFactoryProvider.get();
EntityManager em = EMF.createEntityManager();
custLogger.info("entity manager opened at ststus till ");
int stateid=statustillrequest.getState_id();
long cityid=statustillrequest.getCity_id();
long numberofstudents=0;
try{
if(stateid==0 && cityid==0){
custLogger.info("==========entered into all block ");
numberofstudents= (long) em.createQuery("select count(id) from testing.Student").getSingleResult();
statustillresponse.setNumberofstudents(numberofstudents);
}
else if(stateid!=0 && cityid==0){
custLogger.info("==========entered into state one city all ");
numberofstudents= (long) em.createQuery("SELECT COUNT(ID) FROM testing.student where STATEID=:stateid")
.setParameter("stateid", stateid).getSingleResult();
statustillresponse.setNumberofstudents(numberofstudents);
}
else if(stateid!=0 && cityid!=0){
custLogger.info("==============entered into state one city one ");
numberofstudents= (long) em.createQuery("SELECT COUNT(ID) FROM testing.student where CITYID=:cityid")
.setParameter("cityid", cityid).getSingleResult();
statustillresponse.setNumberofstudents(numberofstudents);
}
}catch(Exception e){
custLogger.warning("error in student count queries========"+e.toString());
}finally {
if (em != null) {
try {
em.close();
} catch (Exception ignored) {
custLogger.info("Couldn't close emFactory :" + ignored);
}
}
}
response.add(statustillresponse);
return response;
}
// this is my service class
@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path("/getstatustill")
public Response getStatustill(Statustillrequest statustillrequest, @HeaderParam("Content-Type") String type )
{
Logger custLogger = Logger.getLogger("AdminService.java");
custLogger.info("=====status till service called");
List<Statustillresponse> statustillresponse=null;
try{
statustillresponse= dao.getStausstills(statustillrequest);
}catch(Exception e){
return Response.status(500).type(type).entity(new ErrorResponse("ERR500", UserPrompts.STATUSTILL_NOTFOUND)).build();
}
return null;
}
//this is my DAO class
public List<Statustillresponse> getStausstills(Statustillrequest statustillrequest) {
Statustillresponse statustillresponse=null;
List<Statustillresponse> response=new ArrayList<>();
Logger custLogger = Logger.getLogger("AdminDao.java");
EntityManagerFactory EMF = EntityManagerFactoryProvider.get();
EntityManager em = EMF.createEntityManager();
custLogger.info("entity manager opened at ststus till ");
int stateid=statustillrequest.getState_id();
long cityid=statustillrequest.getCity_id();
long numberofstudents=0;
try{
if(stateid==0 && cityid==0){
custLogger.info("==========entered into all block ");
numberofstudents= (long) em.createQuery("select count(id) from testing.Student").getSingleResult();
statustillresponse.setNumberofstudents(numberofstudents);
}
else if(stateid!=0 && cityid==0){
custLogger.info("==========entered into state one city all ");
numberofstudents= (long) em.createQuery("SELECT COUNT(ID) FROM testing.student where STATEID=:stateid")
.setParameter("stateid", stateid).getSingleResult();
statustillresponse.setNumberofstudents(numberofstudents);
}
else if(stateid!=0 && cityid!=0){
custLogger.info("==============entered into state one city one ");
numberofstudents= (long) em.createQuery("SELECT COUNT(ID) FROM testing.student where CITYID=:cityid")
.setParameter("cityid", cityid).getSingleResult();
statustillresponse.setNumberofstudents(numberofstudents);
}
}catch(Exception e){
custLogger.warning("error in student count queries========"+e.toString());
}finally {
if (em != null) {
try {
em.close();
} catch (Exception ignored) {
custLogger.info("Couldn't close emFactory :" + ignored);
}
}
}
response.add(statustillresponse);
return response;
}
“ [INFO]警告:学生人数错误 query ========= javax.persistence.PersistenceException:类 testing.Student尚未解决。检查查询并 任何导入/别名规范“