java google app引擎中的一对多关系导致错误?

时间:2012-07-15 17:52:20

标签: google-app-engine servlets persistence jdo datanucleus

我已经实施了一个系统来保存用户信息。 所以我在我的课上有聚合,所以用户类有联系类的列表......等等 在第一页“测试它仅用于通过电话号码注册用户”必须将用户保存在数据库中,但这会导致我在Google应用程序引擎中部署项目时出错< 500服务器错误>

和另一个页面用于更新以前已注册的现有用户,因此此时将添加到用户对象所具有的列表中。

用户类

@PersistenceCapable
public class User implements Serializable{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

 @Persistent
public String userNumber;

 @Persistent (mappedBy = "userC")
 public List<Contact> UserContacts =new  ArrayList<Contact>();

 public void AddContact(String name,String number) {
     Contact C=new Contact();
     C.ContactName=name;
     C.PhoneNumber=number;
     this.UserContacts.add(C);   }
     }

联系班级

    @PersistenceCapable
public class Contact implements Serializable{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

     @Persistent
        public String ContactName;

     @Persistent
    public String PhoneNumber;  
             @Persistent
    public User userC; }

此页面导致用户测试注册将获取用户电话号码并注册应创建具有此号码的新用户 测试页

@SuppressWarnings("serial")

public class Test扩展了HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("<html><body><form  method = \"POST\"     action=\"/signup\">" + "please enter ur number :"+"<h4><label>name :  <input name =  \"userphoneNUMBER\" type =  \"text \" size =  \"25 \" />  </label>"+"<p> <input type = \"submit\" value = \"Submit\" />"+ "</form></body></html>");

    }
}

此页面使用数字创建用户

@SuppressWarnings("serial")
public class SignUP extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/plain");
        String user_PhoneNumber=req.getParameter("userphoneNUMBER");
        User obj = new User();
    obj.userNumber=user_PhoneNumber;
    resp.getWriter().println(obj.userNumber );
        PersistenceManager pm = PMF.get().getPersistenceManager();

        try {
            pm.makePersistent(obj);
        } finally {
            pm.close();

        } }  }

此页面继续更新已存在的用户对象的值

    @SuppressWarnings("serial")
public class Testinfo extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("<html><body><form  method = \"POST\" action=\"/saveinfo\">" +
                "<center> <h2>please fill this form :</h2> </br> "+
"<h4><label> ContactName :  <input name =  \"ContactName\" type =  \"text \" size =  \"25 \" />  </label>"
+
"<p> <input type = \"submit\" value = \"Submit\" />"+
                "</form></body></html>");
    }

}

此页面用于保存导致错误的信息,并且没有值将保存在应用引擎

    @SuppressWarnings("serial")
public class SaveInfo extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/plain");

        String ContactName = req.getParameter("ContactName");
        String ContactNumber = req.getParameter("ContactNumber");
                   PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery("select from " + User.class.getName());
        List<User> list = (List<User>) query.execute();
        for (User obj : list) {


            if (obj.userNumber.equals("111")) {
                 pm.currentTransaction().begin();
                                     obj.AddContact(ContactName, ContactNumber);

                pm.makePersistent(obj);

                 pm.currentTransaction().commit(); }
            }
                          pm.close(); }  }

111 用于测试我之前输入的用户电话号码。

那么我该如何处理列表和聚合问题?

当要更新用户信息时发生此错误

Uncaught exception from servlet
javax.jdo.JDOUserException: Identifier expected at character 1 in "*" at         org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:375)
at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:230)
at sstooree.SaveInfo.doPost(SaveInfo.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:102)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)

1 个答案:

答案 0 :(得分:-1)

查询中的问题

更改此

Query query = pm.newQuery("select from " + User.class.getName());

Query query = pm.newQuery("select * from " + User.class.getName());

您没有选择任何列。导致sql语法错误