我正在尝试使用带有以下查询的结果类来查询两个数据库表:
public List<UserStatus> getUserStatus(String username) {
String qlString =
"SELECT NEW no.uia.slit.ejb.UserStatus(file.filename, file.uploadTime, mod.title, mod.dueAt) "
+ "FROM DownloadableFile file, Module mod"
+ "where file.moduleId = mod.id";
TypedQuery<UserStatus> query = em.createQuery(qlString, UserStatus.class);
return query.getResultList();
}
运行此操作我收到以下错误:
Caused by: <openjpa-2.3.0-nonfinal-1540826-r422266:1542644 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Encountered "mod ." at character 71, but expected: ["(", "+", "-", ":", "?", "ABS", "AVG", "CASE", "COALESCE", "CONCAT", "COUNT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "ENTRY", "INDEX", "KEY", "LENGTH", "LOCATE", "LOWER", "MAX", "MIN", "MOD", "NULLIF", "SIZE", "SQRT", "SUBSTRING", "SUM", "TRIM", "TYPE", "UPPER", "VALUE", <BOOLEAN_LITERAL>, <DATE_LITERAL>, <DECIMAL_LITERAL>, <IDENTIFIER>, <INTEGER_LITERAL>, <STRING_LITERAL2>, <STRING_LITERAL>, <TIMESTAMP_LITERAL>, <TIME_LITERAL>].
我的结果类如下:
public class UserStatus {
private String title;
private Date dueAt;
private String filename;
private Timestamp uploadTime;
public UserStatus(){
super();
}
public UserStatus(String filename, Timestamp uploadTime, String title, Date dueAt) {
this.filename = filename;
this.uploadTime = uploadTime;
this.title = title;
this.dueAt = dueAt;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setDueAt(Date dueAt) {
this.dueAt = dueAt;
}
public Date getDueAt() {
return dueAt;
}
public void setFileName(String filename) {
this.filename = filename;
}
public String getFileName() {
return filename;
}
public void setUploadTime(Timestamp uploadTime) {
this.uploadTime = uploadTime;
}
public Timestamp getUploadTime() {
return uploadTime;
}
}
我使用了一个我发现的例子来进行查询,我无法弄清楚出了什么问题,我知道它说的是#34; mod。 &#34;是错的,但为什么。
编辑:https://blogs.oracle.com/adf/entry/ejb_dc_using_jpql_constructor这是我使用的例子。
答案 0 :(得分:1)
&#34; MOD&#34;是JPQL语言使用的保留关键字,因此您无法将其用于变量名称。选择不在该列表中的其他名称
答案 1 :(得分:1)
mod
(ducumentation)是sql中的保留字,将其更改为module
并且它应该可以正常工作