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);
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 !!