我必须在有效且具有特定关键字的用户中进行搜索。我有User
和User_personal
个课程,我在user_personal
课程中没有状态,所以我通过search
使用User_personal
到User
因为Activeusers
类中有User
个查找函数,如下所示
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// TODO Auto-generated method stub
HashMap<String,String> map = RequestToMap.getBody(request);
JsonObject output = new JsonObject();
JsonArray SearchArray = new JsonArray();
boolean validToken=true;
if(validToken == true){
EntityManager em = Entitymanager.getEntitymanager();
try{
String name = map.get("fname");
if(name!=null && !name.isEmpty()){
List<User> user = User.search(name, em);
if(user!=null && !user.isEmpty()){
for(User u :user){
if(User.ActiveUsers(em).contains(u)){
JsonObject personal_details = new JsonObject();
personal_details .addProperty("id",u.getId());
personal_details .addProperty("email", u.getUser_p().getEmail());
User current_user = User.find(u.getUser_p().getEmail(), em);
personal_details.addProperty("status",current_user.getStatus().toString());
personal_details .addProperty("first_name", u.getUser_p().getFname());
personal_details .addProperty("last_name", u.getUser_p().getLname());
personal_details .addProperty("image", u.getUser_p().getImage());
personal_details .addProperty("score",current_user.getScore());
SearchArray.add(personal_details);
}
}
if((SearchArray.size()!=0))
output.add("searched_users",SearchArray);
else
output.addProperty(Constants.MESSAGE, "ACTIVE User not found. Try typing another name");
}
else
output.addProperty(Constants.MESSAGE, "User not found.Try typing another name");
}
else
output.addProperty(Constants.MESSAGE, "Please type a name to search");
}
finally{
if(em.getTransaction().isActive())
em.getTransaction().rollback();
em.close();
}
}
response.setContentType("application/JSON");
response.getWriter().println(output);
}
search
类
User
函数
public static List<User> search(String name,EntityManager em) {
List<User_personal> u_p = User_personal.search(name,em);
Query p = em.createQuery("select c from User c where (c.status=:stat) and (c.user_p = :u_p)");
p.setParameter("stat",Status.ACTIVE);
p.setParameter("u_p", u_p);
@SuppressWarnings("unchecked")
List<User> result = p.getResultList();
if (!result.isEmpty()) {
return result;
}
return null;
}
我在search
班级中的User_personal
public static List<User_personal> search(String str,EntityManager em) {
Query q = em.createQuery("SELECT u FROM User_personal u WHERE lower(CONCAT(u.fname,u.lname)) LIKE lower(:name) ORDER BY LOCATE(CONCAT(u.fname,u.lname),lower(:given)), LENGTH(CONCAT(u.fname,u.lname))", User_personal.class);
q.setParameter("given",str);
q.setParameter("name", "%"+str+"%");
@SuppressWarnings("unchecked")
List<User_personal> result = q.getResultList();
if (!result.isEmpty()) {
return result;
}
return null;
}
我有一个例外
<pre>com.objectdb.o.InternalException: Unexpected internal exception
com.objectdb.o.JPE.h(JPE.java:168)
com.objectdb.o.ERR.f(ERR.java:66)
com.objectdb.o.OBC.onObjectDBError(OBC.java:1560)
com.objectdb.jpa.JpaQuery.getResultList(JpaQuery.java:728)
com.professionalVerdict.model.User.search(User.java:99)
com.professionalVerdict.servlet.SearchServlet.doPost(SearchServlet.java:53)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
</pre>
<pre>java.lang.NullPointerException
com.objectdb.o.EXR$e.c(EXR.java:190)
com.objectdb.o.VAR.aB(VAR.java:887)
com.objectdb.o.VAR.aA(VAR.java:830)
com.objectdb.o.BCN.o(BCN.java:305)
com.objectdb.jpa.JpaQuery.getResultList(JpaQuery.java:719)
com.professionalVerdict.model.User.search(User.java:99)
com.professionalVerdict.servlet.SearchServlet.doPost(SearchServlet.java:53)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
</pre>
任何人都可以帮我找到解决方案吗?