JPA:当我尝试坚持一个对象时奇怪的错误 [英] JPA: Weird error when I try to persist an object
问题描述
User
和 Group
之间的 OneToMany
关系> Group.java
@Entity
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
私人字符串groupid;
@ManyToOne
@JoinColumn(name =USER_FK)
私人用户用户;
...
}
User.java
@Entity
public class User {
@ Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String userId;
私人字符串密码;
private String fname;
private String lname;
@OneToMany(mappedBy =user,cascade = CascadeType.ALL)
private List< Group>组;
$ b public void addGroup(Group group){
if(this.groups == null){
this.groups = new ArrayList< Group>();
}
this.groups.add(group);
group.setUser(this);
$ b 所以当我尝试坚持对象
User user = em.find(User.class,1L);
user.addGroup(group);
persist(user);
我得到了这个
(Eclipse持久性服务 - 2.0.1.v20100213-r6600):org.eclipse.persistence.exceptions.DatabaseException
内部异常:com.mysql.jdbc.exceptions。 jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行的GROUP(ID,GROUPID,USER_FK)VALUES(2501,'fdsaf',1)'附近使用正确的语法
错误代码:1064
调用:INSERT INTO GROUP(ID,GROUPID,USER_FK)VALUES(?,?,?)
bind => [2501,fdsaf,1]
查询:InsertObjectQuery(org.xdrawings.entity.Group@a1c)
正如你所看到的,它试图插入正确的值,但不知何故它被标记为语法错误。我认为它缺少单引号围绕 GROUP
,但由于它在底层查询,我不知道如何解决它。请注意,我对同一个项目中的其他实体做了完全相同的事情,并且工作正常。所以很沮丧! GROUP确实是一个保留关键字,你必须转义它。在JPA 2.0中,有一种标准化的方式来指定分隔标识符。从JPA 2.0规范:
$ b
2.13数据库对象的命名
..
要指定分隔标识符,必须使用以下方法之一:
- 可以指定将持久化单元中使用的所有数据库标识符视为分隔标识符,方法是在
中指定
< delimited-identifiers /> code> persistence-unit-defaults
元素的对象/关系xml映射文件。如果指定了< delimited-identifiers />
元素,则不能被覆盖。
- 数据库对象的名称将被解释为分隔标识符,如下所示:
- 使用注释,名称被指定为分隔符标识符通过将名称
包含在双引号中,从而内部引号将被转义,例如@Table(name =\customer \)
。 使用XML时,通过使用双
引号将名称指定为分隔标识符,例如< table name =& quot; quot ;客户& quot; />
$ b所以像这样的东西应该工作:
$ $ p $ $ $ c $ @Entity
@Table =\GROUP \)
public class Group {
...
}
I got a
OneToMany
relation betweenUser
andGroup
Group.java
@Entity public class Group { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String groupid; @ManyToOne @JoinColumn(name="USER_FK") private User user; ... }
User.java
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String userId; private String password; private String fname; private String lname; @OneToMany(mappedBy="user", cascade=CascadeType.ALL) private List<Group> groups; public void addGroup(Group group){ if(this.groups == null){ this.groups = new ArrayList<Group>(); } this.groups.add(group); group.setUser(this); } }
So when I try to persist the object
User user = em.find(User.class, 1L); user.addGroup(group); persist(user);
I got this
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 (ID, GROUPID, USER_FK) VALUES (2501, 'fdsaf', 1)' at line 1 Error Code: 1064 Call: INSERT INTO GROUP (ID, GROUPID, USER_FK) VALUES (?, ?, ?) bind => [2501, fdsaf, 1] Query: InsertObjectQuery(org.xdrawings.entity.Group@a1c)
As you can see, it try to insert the correct values, but somehow it marked as syntax error. I think it missing single quote around
GROUP
, but since it does the query under the hood, I have no idea how to fix it. Note that I did the exact same thing to with other entity in the same project and it works fine. So frustrated !!解决方案GROUP is indeed a reserved keyword, you'll have to escape it. In JPA 2.0, there is a standardized way to specify delimited identifiers. From the JPA 2.0 specification:
2.13 Naming of Database Objects
...
To specify delimited identifiers, one of the following approaches must be used:
- It is possible to specify that all database identifiers in use for a persistence unit be treated as delimited identifiers by specifying the
<delimited-identifiers/>
element within thepersistence-unit-defaults
element of the object/relational xml mapping file. If the<delimited-identifiers/>
element is specified, it cannot be overridden.- It is possible to specify on a per-name basis that a name for a database object is to be interpreted as a delimited identifier as follows:
- Using annotations, a name is specified as a delimited identifier by enclosing the name within double quotes, whereby the inner quotes are escaped, e.g.,
@Table(name="\"customer\"")
.- When using XML, a name is specified as a delimited identifier by use of double quotes, e.g.,
<table name=""customer""/>
So something like this should work:
@Entity @Table(name="\"GROUP\"") public class Group { ... }
这篇关于JPA:当我尝试坚持一个对象时奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!