【问题标题】:eclipselink jpa entity is not presented as a table in dbeclipselink jpa 实体未在 db 中显示为表
【发布时间】:2012-09-21 00:13:27
【问题描述】:

我正在尝试使用 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;
}

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

投票类

@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 中有五个表: 选项、序列、用户、用户投票和投票。我不明白,投票表在哪里。 抱歉写了这么多信,希望你能帮到我。谢谢。

【问题讨论】:

    标签: java jpa eclipselink


    【解决方案1】:

    在你的 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 中的保留字

    【讨论】:

    • 我已经实现了所有的 getter、setter、hash()、equals() 和 toString() 方法,只是没有写在这里以节省空间。我按照你说的做了,现在我有以下错误:内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'eclipselink_example.SEQUENCE'不存在错误代码:1146调用:UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ?哪里 SEQ_NAME = ? bind => [2 个参数绑定] 查询:DataModifyQuery(name="SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?") 我不知道,为什么程序根本需要这个 SEQUENCE 表
    • 嗯,错误是关于您的表 SEQUENCE not Existing 尝试进行更新时,我在您的 persistence.xml 中看不到它
    • 我每次使用 jpa 时都有表 SEQUENCE,它是存储具有 @GeneratorValue id 或其他东西的实体的 id 键的表。这个表是eclipselink生成的,不是我自己生成的。
    • 嗯?我从未见过 JPA 创建一个我没有在持久性或注释上指定的 TABLE @GeneratedValue(strategy = GenerationType.AUTO) 基本上是告诉实体 id 的注释,不应该创建一个单独的 id 表?
    • 尝试使用@GeneratedValue(strategy = GenerationType.IDENTITY) 而不是GenerationType.AUTO
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      相关资源
      最近更新 更多