JPQL MySQLSyntaxErrorException:您的 SQL 语法有错误

发布于 2024-09-17 22:50:07 字数 1367 浏览 6 评论 0原文

我有两个实体用户和组:

@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...
}

它是多对多单向的。它生成表useruser_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

和影子一齐双人舞 2024-09-24 22:50:07

您可以使用 GroupUser 作为数据库对象,只要您告诉 JPA 提供者这些名称必须被解释为分隔标识符即可。

使用 JPA 2.0 和注释,通过将名称括在双引号内,将名称指定为分隔标识符,从而对内部引号进行转义:

@Entity
@Table(name="\"User\"")
public class User implements Serializable {
    ...
}

@Entity
@Table(name="\"Group\"")
public class Group implements Serializable {
    ...
}

参考

  • JPA 2.0 规范
    • 第 2.13 节“数据库对象的命名”

You CAN use Group or User 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:

@Entity
@Table(name="\"User\"")
public class User implements Serializable {
    ...
}

@Entity
@Table(name="\"Group\"")
public class Group implements Serializable {
    ...
}

Reference

  • JPA 2.0 specification
    • Section 2.13 "Naming of Database Objects"
吐个泡泡 2024-09-24 22:50:07

不要将您的用户组实体称为“组”。它是 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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文