gpt4 book ai didi

java - .setProperty 100% 的时间都不起作用

转载 作者:行者123 更新时间:2023-12-01 11:42:56 25 4
gpt4 key购买 nike

编辑:代码已更新以包含 @Igor Artamonov 建议的更改,但问题仍然出现!我确实注意到了以下内容 reference to this image ,所有者 2 有高度值,没有 tool_proficiency 值,直到添加所有者 3,然后 2 的高度值消失,出现 2 的工具值,2 在创建时拥有史密斯工具
编辑结束

这可能类似于“This Question”,但我无法使用那里给出的解决方案解决问题,而且我无法发表评论,因为我的代表太少了:(

所以我希望你不介意我问我在这里的问题:) *注意如果有更好的地方让我提出我的问题或有任何问题请告诉我,我在这里很新:P

我的网页有一个 java 帮助程序,用于设置数据存储中实体的属性,但每次至少会错过一项(如果不是更多的话)?所有项目都使用相同的功能设置!

JSP页面上的代码:

String Username = (String)session.getAttribute("username");
System.out.println("Username carried from session variable = "+Username);

if(request.getParameter("alignment")!=null
&& request.getParameter("Size")!=null
&& request.getParameter("age")!=null
&& request.getParameter("tools")!=null){

String HD = "Hill Dwarf";
String Age = request.getParameter("age");
String Alignment = request.getParameter("alignment");

int Size = Integer.parseInt(request.getParameter("Size"));
int Rem = Size%12;
Size = (Size-(Size%12))/12;
String Height = (Size+"ft "+Rem+"in");

String Tools = request.getParameter("tools");

System.out.println("setString for hill dwarf: ");
login.setString(Username, "Race", HD);
System.out.println("setString for age: ");
login.setString(Username, "Age", Age);
System.out.println("setString for Alignment: ");
login.setString(Username, "Alignment", Alignment);
System.out.println("setString for height: ");
login.setString(Username, "Height", Height);
System.out.println("setString for tools: ");
login.setString(Username, "Tool_Proficiency", Tools);
System.out.println("redirecting to class page");
response.sendRedirect("class.jsp");
}


登录助手中的代码:

    public void setString(String usernamein,String columnin, String stringin){
Transaction txn = datastore.beginTransaction();
try{
Filter usernamefilter = new FilterPredicate("owner",
FilterOperator.EQUAL, usernamein);
Query validuserquery = new Query("Char").setFilter(usernamefilter);
Entity theUser = datastore.prepare(validuserquery).asSingleEntity();
System.out.println("Username passed to setString = "+usernamein);
System.out.println("Column name passed to setString = "+columnin);
System.out.println("String passed to setString = "+stringin);
System.out.println("Query = "+validuserquery);
if (theUser==null && counter < 30){
System.out.println("theUser was equal to null");
try {
System.out.println("sleeping for 400ms");
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter ++;
System.out.println("counter increased, counter = "+counter);
System.out.println("Recursing function");
setString(usernamein,columnin,stringin);
}else{
System.out.println("theUser was not == null ");
System.out.println("inputting: "+columnin+" = "+stringin);
theUser.setProperty(columnin,stringin);
datastore.put(theUser);
System.out.println("item added");
}
txn.commit();
}finally{
if (txn.isActive()){
txn.rollback();
}
}
}

如果有人需要我解释代码,我只会问:)

好的,这是将年龄、对齐方式等放入数据存储中的代码,但它在数据存储中给出了这个结果?我一生都不知道为什么!?

这本来是一张图像,但我需要 10 次代表,所以你必须 follow this link for the image :(再次抱歉


这是 eclipse 控制台从所有 System.out.println() 获取的内容这不是代码,但我会将其放入代码片段中以提高可读性:)

HillDwarf page loaded
Username carried from session variable = 12
setString for hill dwarf:
Username passed to setString = 12
Column name passed to setString = Race
String passed to setString = Hill Dwarf
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Race = Hill Dwarf
item added
setString for age:
Username passed to setString = 12
Column name passed to setString = Age
String passed to setString = 10
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Age = 10
item added
setString for Alignment:
Username passed to setString = 12
Column name passed to setString = Alignment
String passed to setString = N
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Alignment = N
item added
setString for height:
Username passed to setString = 12
Column name passed to setString = Height
String passed to setString = 4ft 5in
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Height = 4ft 5in
item added
setString for tools:
Username passed to setString = 12
Column name passed to setString = Tool_Proficiency
String passed to setString = Brewer's Supplies
Query = SELECT * FROM Char WHERE owner = 12
theUSer was not == null
inputting: Tool_Proficiency = Brewer's Supplies
item added
redirecting to class page


因此,如图所示,我的问题是数据存储区并未每次都获取所有项目,即使我每次都使用相同的帮助器方法!

请帮助我几天来一直在尝试通过多种方法解决这个问题:(

最佳答案

您没有使用事务,因此某些写入会被其他写入替换。

试试这个:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try {
/// your current code
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}

参见https://cloud.google.com/appengine/docs/java/datastore/transactions

PS,您还应该了解,您当前的代码比在一个 .put 中进行相同更新的代码贵 4 倍。我认为没有任何理由要为每个字段分别进行 4 次。

关于java - .setProperty 100% 的时间都不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29373192/

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