JPA:尝试持久化对象时出现奇怪的错误
我得到了 User
和 Group
之间的 OneToMany
关系Group.java
I got a OneToMany
relation between User
and Group
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);
我知道了
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)
如您所见,它尝试插入正确的值,但不知何故将其标记为语法错误.我认为它缺少 GROUP
周围的单引号,但由于它在后台进行查询,我不知道如何修复它.请注意,我对同一项目中的其他实体做了完全相同的事情,并且效果很好.好郁闷!!
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 确实是保留关键字,您必须将其转义.在 JPA 2.0 中,有一种指定分隔标识符的标准化方法.来自 JPA 2.0 规范:
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:
...
要指定分隔标识符,必须使用以下方法之一:
To specify delimited identifiers, one of the following approaches must be used:
- 可以通过在
persistence-unit 中指定
对象/关系 xml 映射文件的元素.如果指定了元素来指定将用于持久性单元的所有数据库标识符视为分隔标识符-defaults <delimited-identifiers/>
元素,则不能被覆盖. - 可以基于每个名称指定将数据库对象的名称解释为分隔标识符,如下所示:
- 使用注解,通过将名称括起来,将名称指定为分隔标识符在双引号内,其中内引号被转义,例如
@Table(name=""customer"")
. - 使用 XML 时,名称通过使用 double 指定为分隔标识符引号,例如
<table name=""customer""/>
- 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 { ... }
- 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.,
- 使用注解,通过将名称括起来,将名称指定为分隔标识符在双引号内,其中内引号被转义,例如
相关文章