GenerationTarget遇到异常接受命令:执行DDL时出错:独立JPA应用程序

时间:2019-01-12 20:40:00

标签: mysql hibernate jpa

我正在使用Linux上的MySQL编写一个简单的“ Hello world” JPA Java应用程序。

我正在尝试自动配置架构,从persist.xml调用脚本。

当我尝试实例化我的JPA EntityManagerFactory时出现此错误:

WARN: GenerationTarget encountered exception accepting command : Error executing DDL "CREATE TABLE task (" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "CREATE TABLE task (" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:440)
...
Caused by: java.sql.SQLSyntaxErrorException: 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 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:782)
    at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:666)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
    ... 15 more

我已经阅读了其他链接,我尝试显式设置“ MySQL57Dialect”(即使在添加属性之前它也会自动检测到“ MySQL57Dialect”),我尝试简化SQL脚本等。等等-全部无济于事。

版本信息:

Ubuntu 18.04.1 LTS
mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)

SQL脚本(当前的精简版本:从mysql CLI可以正常运行):

CREATE TABLE task (
  id BIGINT NOT NULL auto_increment,
  summary VARCHAR(20) NOT NULL,
  priority VARCHAR(10) NOT NULL DEFAULT 'NORMAL', 
  status VARCHAR(10) NOT NULL DEFAULT 'PENDING',
  created_on TIMESTAMP DEFAULT current_timestamp,
  PRIMARY KEY(id)
);

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="com.example.hellohibernate.jpa" transaction-type="RESOURCE_LOCAL">
        <class>com.example.hellohibernate.Task</class>      
        <class>com.example.hellohibernate.TaskUpdate</class>      
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" />
            <!-- Configuring The Database Connection Details -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test2" />
            <property name="javax.persistence.jdbc.user" value="test" />
            <property name="javax.persistence.jdbc.password" value="test123" /> 
            <!-- First time only,  create from schema: -->
            <property name="javax.persistence.schema-generation.database.action" value="create" />
            <property name="javax.persistence.schema-generation.create-source" value="script"/>
            <property name="javax.persistence.schema-generation.create-script-source" value="META-INF/mkdb.mysql.sql" />                 
        </properties>
    </persistence-unit>
</persistence>

控制台日志:

Jan 12, 2019 11:59:54 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000412: Hibernate Core {5.4.0.Final}
...
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
...
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "create table task (" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table task (" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
...
Caused by: java.sql.SQLSyntaxErrorException: 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 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:782)
    at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:666)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
    ... 15 more

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我尝试验证MySQL语法,尝试验证MySQL版本,尝试显式设置Hibernate MySql57Dialect,尝试“简化” SQL语法...无济于事。

我基本上尝试了此链接中的所有内容(以及更多):

GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement

然后,比尔·卡文(Bill Karwin)建议将每条陈述准确地放在一行上。

DID 有效。

最终-工作-语法:

Java应用程序:

public static final String PERSISTENCE_UNIT = "com.example.hellohibernate.jpa";

protected static EntityManagerFactory entityManagerFactory;

protected static EntityManagerFactory createEntityManagerFactory() {
    entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
    ...

persistence.xml:

   <persistence-unit name="com.example.hellohibernate.jpa" transaction-type="RESOURCE_LOCAL">
        <class>com.example.hellohibernate.Task</class>      
        <class>com.example.hellohibernate.TaskUpdate</class>      
        <properties>
            <!-- Configuring The Database Connection Details -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test2" />
            <property name="javax.persistence.jdbc.user" value="test" />
            <property name="javax.persistence.jdbc.password" value="test123" /> 
            <!-- First time only,  create from schema: -->
            <property name="javax.persistence.schema-generation.database.action" value="create" />
            <property name="javax.persistence.schema-generation.create-source" value="script"/>
            <property name="javax.persistence.schema-generation.create-script-source" value="META-INF/mkdb.mysql.sql" />
            ...

mkdb.mysql.sql:

drop table if exists task_update;
drop table if exists task;

-- priority: LOW, NORMAL, HIGH
-- status: PENDING, INPROGRESS, COMPLETED, BLOCKED, REOPENED
create table task (  id int NOT NULL auto_increment,  summary varchar(20) NOT NULL,  priority varchar(10) NOT NULL default 'NORMAL',  status varchar(10) NOT NULL default 'PENDING',  date timestamp default current_timestamp,  PRIMARY KEY(id));

create table task_update (  id int NOT NULL auto_increment,  taskid int NOT NULL,  text varchar(255) NULL,  date timestamp default current_timestamp,  PRIMARY KEY(id),  FOREIGN KEY fk_task(taskid)  REFERENCES task(id));

注意:

  • 注释和空行可以解析OK ...,但是使用Hibernate / JPA解析器(org.hibernate.tool.schema.internal)将语句拆分为多行似乎会失败。

  • 条例草案-再次感谢您。如果您想发表您的想法,我很乐意“接受”您的想法。否则,我想记录一下发现的结果。