2012-09-21 30 views
0

我正在嘗試使用eclipselink jpa做一個測試項目工作,但每次我試圖在數據庫中持久存儲Poll對象時遇到了一個尷尬的問題。它適用於例如User類對象。我想,這可能是我對ManyToMany註釋有問題。 所以,我有四個實體類:eclipselink jpa實體沒有作爲db表提供

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

有一個錯誤,我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]) 

這裏是我的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五個表: 選項,SEQUENCE,用戶,用戶投票和投票。我不明白,投票表在哪裏。 對不起,有很多信件,希望你能幫助我。謝謝。

回答

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

+0

我已經有了所有的getter,setter,hash(),equals()和toString()方法實現,只是沒有寫在這裏保存位置。 我照你說的,現在我已經以下錯誤: 內部異常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表「eclipselink_example.SEQUENCE」不存在 錯誤代碼:1146 召喚:更新序列SET SEQ_COUNT = SEQ_COUNT +? WHERE SEQ_NAME =? \t綁定=> [2個參數約束] 查詢:DataModifyQuery(NAME = 「序列」 SQL = 「?更新序列SET SEQ_COUNT = SEQ_COUNT + WHERE SEQ_NAME =」) 我不知道,爲什麼程序需要這個SEQUENCE表在所有 – braveleg

+0

那麼錯誤是關於你的表SEQUENCE不存在,當試圖做一個更新,我沒有看到它在你的persistence.xml – gtgaxiola

+0

我有表SEQUENCE每次我與jpa工作時,它是表在哪裏存儲ID爲@GeneratorValue ID或某物的實體的ID鍵。此表由eclipselink生成,而不是由我生成。 – braveleg

0

我想保留字,EclipseLink JPA將有助於你ID Generation問題。如果您使用@GeneratedValue(strategy = GenerationType.AUTO)它取決於數據庫。祝你好運! 儘可能多,它將是獨立的數據庫

相關問題