0

我正在尝试使用 eclipselink jpa 使测试项目工作,但是每次尝试将 Poll 对象持久保存在数据库中时,我都会遇到一个尴尬的问题。例如,它适用于 User 类对象。我想,这可能是我对 ManyToMany 注释有问题。所以,我有四个实体类:

投票类

@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;
}

选项类

@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;
    }

投票类

@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;
    }

用户类

@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;
    }

我收到一个错误:

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])

这是我的 persistence.xml 文件:

<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>

编译后,我在 db 中有五个表:Options、SEQUENCE、Users、Users_Polls 和 Votes。我不明白,投票表在哪里。抱歉写了这么多信,希望你能帮到我。谢谢。

4

2 回答 2

0

在Poll.java 中试试这个

@ManyToMany
@JoinTable(name = "poll_user", joinColumns =
@JoinColumn(name = "poll_id", referencedColumnName = "id"), inverseJoinColumns =
@JoinColumn(name = "user_id", referencedColumnName = "id"))
private List<User> users;

在你的User.java里面

@ManyToMany(mappedBy = "users")
private List<Poll> polls;

也不要忘记为你的变量设置 getter 和 setter

修订

首先我会改变所有的:

@GeneratedValue(strategy = GenerationType.SEQUENCE)

至:

@GeneratedValue(strategy = GenerationType.IDENTITY)

我会检查你在 Mysql 中没有保留字的变量。

例如:

Poll.java里面你有:

private boolean show;SHOW是 MySQL 中的保留字

于 2012-09-21T00:23:49.350 回答
0

我认为,EclipseLink JPA将对您的ID Generation问题有所帮助。如果你使用@GeneratedValue(strategy = GenerationType.AUTO)它取决于数据库。祝你好运!尽可能多的,GenerationType.TABLE独立的数据库

于 2012-09-21T04:27:45.420 回答