I’m trying to make a test project work using eclipselink jpa, but I’ve met an awkward problem every time I am trying to persist Poll object in the database. It works fine with, for example, User class object. I think, that probably I have a problem with ManyToMany annotations.
So, I have four Entity classes:
Poll.class
@Entity
@Table(name = "Polls")
public class Poll implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
@Temporal(TemporalType.TIMESTAMP)
private Date expirationDate;
private String description;
private boolean expired;
private boolean show = false;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "poll")
private List<Option> options;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "polls")
@JoinTable(name = "POLL_USER")
private List<User> users;
public Poll() {
}
public Poll(String title, Date expirationDate, String description,
boolean expired, boolean show) {
super();
this.title = title;
this.expirationDate = expirationDate;
this.description = description;
this.expired = expired;
this.show = show;
}
public Poll(String title, Date expirationDate, String description,
boolean expired, boolean show, List<Option> options,
List<User> users) {
super();
this.title = title;
this.expirationDate = expirationDate;
this.description = description;
this.expired = expired;
this.show = show;
this.options = options;
this.users = users;
}
Option.class
@Entity
@Table(name = "Options")
public class Option implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String text;
private int voteCount = 0;
@ManyToOne
@JoinColumn(name = "POLL_ID", referencedColumnName = "id", nullable = false)
private Poll poll;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "option")
private List<Vote> votes;
public Option() {
}
public Option(String text, int voteCount) {
super();
this.text = text;
this.voteCount = voteCount;
}
public Option(String text, int voteCount, Poll poll, List<Vote> votes) {
super();
this.text = text;
this.voteCount = voteCount;
this.poll = poll;
this.votes = votes;
}
Vote.class
@Entity
@Table(name = "Votes")
public class Vote implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long vid;
private String comment;
@ManyToOne
@JoinColumn(name = "USER_ID", referencedColumnName = "id", nullable = false)
private User user;
@ManyToOne
@JoinColumn(name = "OPTION_ID", referencedColumnName = "id", nullable = false)
private Option option;
public Vote() {
}
public Vote(String comment) {
super();
this.comment = comment;
}
public Vote(String comment, User user, Option option) {
super();
this.comment = comment;
this.user = user;
this.option = option;
}
User.class
@Entity
@Table(name = "Users")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String login;
private Long groupId;
private String email;
@ManyToMany
private List<Poll> polls;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<Vote> votes;
public User() {
super();
}
public User(String login, Long groupId, String email) {
super();
this.login = login;
this.groupId = groupId;
this.email = email;
}
public User(String login, Long groupId, String email, List<Poll> polls,
List<Vote> votes) {
super();
this.login = login;
this.groupId = groupId;
this.email = email;
this.polls = polls;
this.votes = votes;
}
There is an error I am receivening:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): 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 'SHOW, TITLE) VALUES (351, 'Just testing', '2012-09-21 01:49:42', 1, 1, 'My first' at line 1
Error Code: 1064
Call: INSERT INTO Polls (ID, DESCRIPTION, EXPIRATIONDATE, EXPIRED, SHOW, TITLE) VALUES (?, ?, ?, ?, ?, ?)
bind => [6 parameters bound]
Query: InsertObjectQuery(Poll [id=351, title=My first poll, expirationDate=Fri Sep 21 01:49:42 CEST 2012, description=Just testing, expired=true, show=true])
Here is my persistence.xml file:
<persistence-unit name="company" transaction-type="RESOURCE_LOCAL">
<class>logic.Poll</class>
<class>logic.Option</class>
<class>logic.Vote</class>
<class>logic.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/eclipselink_example" />
<property name="javax.persistence.jdbc.user" value="" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.file" value="output.log" />
<property name="eclipselink.logging.logger" value="JavaLogger" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-and-drop-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
After compiling it, i have five tables in db:
Options, SEQUENCE, Users, Users_Polls and Votes. I can’t understand, where is Polls table.
Sorry for so many letters, hope you can help me. Thanks.
Try this inside your Poll.java
And Inside your User.java
Also don’t forget to have getters and setters for your variables
REVISION
Firstly I would changed all the:
@GeneratedValue(strategy = GenerationType.SEQUENCE)to:
@GeneratedValue(strategy = GenerationType.IDENTITY)And I would check that you don’t have variables that are RESERVED WORDS in Mysql.
For example:
Inside Poll.java you have:
private boolean show;but SHOW is a reserved word in MySQL