无法执行JPA模式生成create命令

And*_*aru 3 java mysql hibernate jpa jdbc

我有一个JPA应用程序使用Hibernate而不是JDBC和MySQL,没有任何应用程序服务器.

在我的persistence.xml中,我已经定义了从脚本创建,加载和删除数据库的规则:

<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/scripts/create.sql" />
<property name="javax.persistence.sql-load-script-source" value="META-INF/scripts/load.sql" />
<property name="javax.persistence.schema-generation.drop-source" value="script" />
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/scripts/drop.sql" />
Run Code Online (Sandbox Code Playgroud)

但是,当Maven测试目标执行时,我收到以下错误:

无法执行JPA模式生成create命令[CREATE TABLE country(]

堆栈跟踪看起来像这样:

Caused by: 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 '' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2504)
at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:840)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:740)
at org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase.acceptCreateCommands(GenerationTargetToDatabase.java:44)
... 34 more
Run Code Online (Sandbox Code Playgroud)

加载脚本以创建国家/地区表开始:

DROP DATABASE IF EXISTS bookstore;

CREATE DATABASE bookstore;

USE bookstore;

CREATE TABLE country (
    id          INT(10) UNSIGNED AUTO_INCREMENT NOT NULL,
    name        VARCHAR(50) NOT NULL,
    code        VARCHAR(2) NOT NULL UNIQUE,
    description VARCHAR(1000),
    KEY (id)
);
ALTER TABLE country ADD CONSTRAINT pk_country_id PRIMARY KEY (id);
Run Code Online (Sandbox Code Playgroud)

Country类映射在persistence.xml文件中,其源代码如下.

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "country")
public class Country extends ro.pub.cs.aipi.lab02.entities.Entity {

    protected Long id;
    protected String name;
    protected String code;
    protected String description;

    public Country() {
    }

    public Country(String name, String code, String description) {
        this.name = name;
        this.code = code;
        this.description = description;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "code")
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }   

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
Run Code Online (Sandbox Code Playgroud)

persistence.xml文件中的Hibernate属性如下:

     <property name="hibernate.connection.pool_size" value="1"/>
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
     <property name="hibernate.format_sql" value="true" />
     <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
     <property name="hibernate.id.new_generator_mappings" value="false"/>
     <property name="hibernate.hbm2ddl.auto" value="true"/>
Run Code Online (Sandbox Code Playgroud)

我已经尝试将hibernate.dialect更改为org.hibernate.dialect.MySQL5InnoDBDialect,并从Entity类中删除@Table注释,但我没有注意到行为的任何变化.

And*_*aru 5

我最终设法搞清楚了.似乎hibernate每行只需要一个语句,因为在我的情况下脚本是格式化的,所以它只占用了一个语句的第一行,因而不完整.

重新格式化脚本并将命令放在一行上解决了问题.

  • 建议你报告这是hibernate中的一个错误,因为其他JPA实现处理这样的脚本延续很好 (2认同)