JPA: Weird error when I try to persist an object(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 { ... }
这篇关于JPA:尝试持久化对象时出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
- 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.,
- 使用注解,通过将名称括起来,将名称指定为分隔标识符在双引号内,其中内引号被转义,例如
本文标题为:JPA:尝试持久化对象时出现奇怪的错误
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01