0

我刚刚开始使用 App Engine,我尝试制作一个非常简单的应用程序,它将具有独特名称的 Person 对象添加到数据存储区。这个对象:

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Person {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
@Unique
private String name;

public Person(String nameIn){
    this.name = nameIn;
}

public Long getId(){
    return this.id;
}

public void setId(Long idIn){
    this.id = idIn;
}

}

此 servlet 负责在数据存储上持久化对象。但在此之前,该方法会doesUserExist(String)检查是否存在具有相同“名称”字段的对象:

@SuppressWarnings("serial")
public class PersonDatastoreServlet extends HttpServlet {

private static final String PARAM_NAME = "name";
private PersistenceManager pmf = PMF.get().getPersistenceManager();

public void doGet(HttpServletRequest req, HttpServletResponse response)
        throws IOException {

        String name = req.getParameter(PARAM_NAME);
        PrintWriter printWriter  = response.getWriter();

        try{

            if(!doesUserExist(name)) {
                Person p = new Person(name);
                pmf.makePersistent(p);

                response.setContentType("text/html");
                printWriter.println("<h1>"+p.getId()+"</h1>");
            }
            else {
                response.setContentType("text/html");
                printWriter.println("<p>User already exists</p>");
            }   
        }
        catch(Exception e) {
            throw new IOException();
        }
        finally{
            pmf.close();
        }
    }

private boolean doesUserExist(String nameIn) {

    Query q = pmf.newQuery(Person.class);
    q.setFilter("name == lastNameParam");
    q.declareParameters("String lastNameParam");

    String name = nameIn;

    try{
        List<Person> list = (List<Person>) q.execute(name);

        if (list.isEmpty()){
            return false;
        }
        else return true;
    }
    finally{
        q.closeAll();
    }
}

}

采取似乎很简单,但它只是不起作用。我有一个处理请求的表格。当我第一次运行我的应用程序时,它确实成功创建并保留了一个对象,但是每当我想添加另一个具有不同名称的对象时,我都会收到错误消息

Error: Server Error

The server encountered an error and could not complete your request.
If the problem persists, please report your problem and mention this error message and the query that caused it.

它表明查询导致了问题,但我知道我的查询出了什么问题。有人可以帮忙吗?

4

0 回答 0