JPQL MySQLSyntaxErrorException:您的 SQL 语法有错误
我有两个实体用户和组:
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String username;
private String password;
private String email;
@ManyToMany
@JoinTable(name="user_group", joinColumns={@JoinColumn(name="USERNAME")}, inverseJoinColumns={@JoinColumn(name="ID")})
private List<Group> group;
// getters and setters...
}
@Entity
public class Group implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
// getters and setters...
}
它是多对多单向的。它生成表user
和user_group
。现在我有一个简单的查询:
Query q = em.createQuery("select g from Group g");
List<Group> usersGroup = q.getResultList();
它抛出异常:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP' at line 1
Error Code: 1064
Call: SELECT ID FROM GROUP
Query: ReadAllQuery(referenceClass=Group sql="SELECT ID FROM GROUP")
... stack trace...
I have two entities User and Group:
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String username;
private String password;
private String email;
@ManyToMany
@JoinTable(name="user_group", joinColumns={@JoinColumn(name="USERNAME")}, inverseJoinColumns={@JoinColumn(name="ID")})
private List<Group> group;
// getters and setters...
}
@Entity
public class Group implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
// getters and setters...
}
It's ManyToMany unidirectional. It produces tables user
and user_group
. Now I have simple query:
Query q = em.createQuery("select g from Group g");
List<Group> usersGroup = q.getResultList();
which throws exception:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP' at line 1
Error Code: 1064
Call: SELECT ID FROM GROUP
Query: ReadAllQuery(referenceClass=Group sql="SELECT ID FROM GROUP")
... stack trace...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Group
或User
作为数据库对象,只要您告诉 JPA 提供者这些名称必须被解释为分隔标识符即可。使用 JPA 2.0 和注释,通过将名称括在双引号内,将名称指定为分隔标识符,从而对内部引号进行转义:
参考
You CAN use
Group
orUser
for the database objects as long as you tell the JPA provider that these names have to be interpreted as delimited identifiers.With JPA 2.0 and annotations, a name is specified as a delimited identifier by enclosing the name within double quotes, whereby the inner quotes are escaped:
Reference
不要将您的用户组实体称为“组”。它是 MySQL 语法中的保留字,用于聚合,如 GROUP BY。
尝试将其命名为其他名称,例如“UserGroup”。
Don't call your user group entity 'Group'. It's be a reserved word in MySQL syntax for use in aggregation as GROUP BY.
Try calling it something else, like 'UserGroup'.