我正在从h2数据库中检索数据。我的问题是在q.setClass()中。 在这里,我尝试设置表chatUsers.class,数据库具有相同的类,但这表明没有表ChatUsers。那我该怎么办?
如果我不写q.setClass()。那么它正在提供数据,但是有一个问题,那就是我无法获取该类的列表。
ChatUser类
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import java.util.List;
import java.util.Properties;
@PersistenceCapable(detachable="true")
public class ChatUsers {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
int id;
String user;
String mobileNo;
String email;
String password;
public ChatUsers(String user, String password, String mobileNo, String email) {
this.user = user;
this.mobileNo = mobileNo;
this.email = email;
this.password = password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
检索逻辑
public static List<ChatUsers> getAllUsersList() {
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(setProperties());
PersistenceManager pm = pmf.getPersistenceManager();
List<ChatUsers> chatUsersList = null;
pm.currentTransaction().begin();
try {
Query q = pm.newQuery("javax.jdo.query.SQL","SELECT * FROM `CHATUSERS`");
q.setClass(ChatUsers.class);//This line has error
chatUsersList = (List<ChatUsers>) q.execute();
}catch (Exception ex)
{
System.out.println(ex);
}
return chatUsersList;
}
错误代码
javax.jdo.JDOUserException: Persistent class "ChatUsers" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class.
NestedThrowables:
org.datanucleus.store.rdbms.exceptions.NoTableManagedException: Persistent class "ChatUsers" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class.
答案 0 :(得分:1)
当执行execute()查询时,应改用executeList()。 [1]
另一个解决方案可能是使用setResultClass()而不是setClass()。 [1]
第一个解决方案的示例是:
Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT MY_ID, MY_NAME FROM MYTABLE");
query.setClass(MyClass.class);
List<MyClass> results = query.executeList();
[1]搜索“设置候选课程 ”和“定义结果类型 “”(位于文档URL:http://www.datanucleus.org/products/accessplatform/jdo/query.html
中)