【问题标题】:JPA failed to automatically generate a table from an entityJPA 无法从实体自动生成表
【发布时间】:2017-02-02 07:44:28
【问题描述】:

我正在使用带有 Derby 数据库的 Eclipselink 从 Entities 中自动生成一个数据库。

生成最初工作得很好,但是当我将用户实体添加到我的模型并尝试使用 JPA 工具从实体生成表时,除了用户表之外,所有表都生成了,并且在生成过程中出现以下错误

[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "USER" at line 1, column 14.
Error Code: 30000

这是用户实体

@NamedQuery( name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.username = :userneme AND u.password = :password" )
@Entity
public class User implements Serializable {

    @Transient
    private static final long serialVersionUID = 1L;

    public User() {
        super();
    }

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private Integer id;

    private String  username;
    private String  password;

    public String getUsername() {
        return username;
    }

    public void setUsername( String username ) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword( String password ) {
        this.password = password;
    }

谁能告诉我为什么会出现异常?

提前致谢

【问题讨论】:

    标签: jpa eclipselink derby


    【解决方案1】:

    这很可能是因为 USER 是 Derby 数据库中的一个函数(请参阅:http://db.apache.org/derby/docs/10.10/ref/rrefsqlj42476.html)。您可以将 JPA bean 设置为 User ,但使用 @Table 注释指定不同的表名

    【讨论】:

    • 我使用的是 Derby 而不是 H2 数据库,但它与 Derby 一起使用...谢谢
    【解决方案2】:

    您的表名是"USER",它是 SQL(和 Apache Derby)中的保留关键字。一些 JPA 提供程序(例如 DataNucleus JPA)会自动为您引用此类关键字,这意味着它可以正常工作。其他(例如 EclipseLink)没有,因此您必须使用引号 (') 显式设置表名,或者将表名设置为不是 SQL 关键字的内容

    @Table(name="'User'")
    public class User {...}
    

    【讨论】:

    • 加引号是不行的..完全改名了..谢谢回复
    猜你喜欢
    • 2011-06-09
    • 2017-05-06
    • 2013-01-31
    • 2013-05-10
    • 2020-07-27
    • 2020-11-16
    • 2017-08-06
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多