gpt4 book ai didi

java - 标识符 userName 未解析(不是静态字段)<有时>

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:18 24 4
gpt4 key购买 nike

我正在使用 JDO 使对象持久化。我已经多次运行代码。有时,我会毫无错误地坚持下去。有时,我收到以下错误。

 1. org.datanucleus.exceptions.NucleusUserException
2. javax.jdo.JDOUserException

我不明白为什么有时运行成功有时失败。这些都是随机发生的。我的应用程序在注册期间验证新用户。这是我的代码。

User.java(实体被持久化)

 @Entity
@PersistenceCapable

public class User {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public String userName;

@Persistent
private String name, password, city, bikeModel, age;

public User(){

}
public User(String name, String uname, String pswd, String age, String city, String bike){
this.name=name;
this.userName=uname;
this.password=pswd;
this.age=age;
this.city=city;
this.bikeModel=bike;
}

public String getUserName() {
return userName;
}
//rest of the getters
}

主要 servlet:

    //Getting SignUp form values
String name=req.getParameter("name1");
String uname=req.getParameter("uname1");
String pswd=req.getParameter("pswd1");
String age=req.getParameter("age");
String city=req.getParameter("city");
String bike=req.getParameter("bike");

//PeristenceManager instance
PersistenceManager pm = PMF.get().getPersistenceManager();

//Creating instance for persistent entity class
User newEntry=new User(name,uname,pswd,age,city,bike);

//Query to match the userName entered in form and that of the dataStore user
Query q = pm.newQuery(User.class);
q.setFilter("userName == userNameParam");
q.declareParameters("String userNameParam");
try{
List<User> results = (List<User>) q.execute(uname);
if (!results.isEmpty()) {
//if userName is found in DataStore
resp.sendRedirect("invalidUser.html");
} else{
//If userName is not found in dataStore
pm.makePersistent(newEntry);
resp.sendRedirect("success.html");
return;
}
}
finally{

System.out.println("Finally!");
}

最佳答案

在您的情况下,请确保用户类每次都得到适当的增强,并建议您将用户类字段单独声明为持久字段。

关于java - 标识符 userName 未解析(不是静态字段)<有时>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24164090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com