0

eclipselink jpaを使用してテストプロジェクトを機能させようとしていますが、データベースにPollオブジェクトを永続化しようとするたびに厄介な問題が発生します。たとえば、Userクラスオブジェクトで正常に機能します。多対多の注釈に問題があるのではないかと思います。したがって、4つのエンティティクラスがあります。

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

私が受け取っているエラーがあります:

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に5つのテーブルがあります: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;

また、変数用のゲッターとセッターを用意することを忘れないでください

リビジョン

まず、すべてを変更します:

@GeneratedValue(strategy = GenerationType.SEQUENCE)

に:

@GeneratedValue(strategy = GenerationType.IDENTITY)

そして、Mysqlに予約語である変数がないことを確認します。

例えば:

Poll.java内には、次のものがあります。

private boolean show;しかし、SHOWはMySQLの予約語です

于 2012-09-21T00:23:49.350 に答える
0

EclipseLinkJPAが問題の解決に役立つと思いますID Generation。使用する場合@GeneratedValue(strategy = GenerationType.AUTO)はデータベースに依存します。幸運を!可能な限り、、、GenerationType.TABLEそれは独立したデータベースになります

于 2012-09-21T04:27:45.420 に答える