one to many relationship in java google ap engine caused error ?

偶尔善良 提交于 2019-12-29 09:40:19

问题


i have implement a system to save information about user . So i have aggregation at my class so the user class has list from contact class ...etc in the first page "test it's just for register the user just by phone number " that must save the user in database but this cause error when i deploye my project in Google app engine < 500 server error >

and the other page for update the exist user who previously has been registered, so at this will add to the list which the user object has .

user class

@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);   }
     }

Contact class

    @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; }

this page cause register for the user test will get user phone number and sign up should create new user with this number test page

@SuppressWarnings("serial")

public class Test extends 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>");

    }
}

this page take the number to create user

@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();

        } }  }

this page to continue update value at user object who already exist

    @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>");
    }

}

this page to save the information which cause error and no value will save at app engine

    @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(); }  }

this 111 for testing which i entered before as user phone number .

So how can i deal with lists and aggregation issues ??

when going to update the user information this error occurred

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:


the problem in the query

change this

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

to

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

you are not selecting any column . which cause a sql syntax error



来源:https://stackoverflow.com/questions/11494240/one-to-many-relationship-in-java-google-ap-engine-caused-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!