Thang Pham Thang Pham - 29 days ago 6x
Java Question

JPA: Weird error when I try to persist an object

I got a

relation between

public class Group {

@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String groupid;

private User user;

public class User {

@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>();

So when I try to persist the object

User user = em.find(User.class, 1L);

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
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
, 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 the persistence-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="&quot;customer&quot;"/>

So something like this should work:

public class Group {