gpt4 book ai didi

java - 无法执行 JPA 架构生成创建命令

转载 作者:可可西里 更新时间:2023-11-01 07:56:05 25 4
gpt4 key购买 nike

我有一个在 JDBC 和 MySQL 上使用 Hibernate 的 JPA 应用程序,没有任何应用程序服务器。

在我的 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" />

但是,当执行 Maven 测试目标时,出现以下错误:

无法执行 JPA 架构生成创建命令 [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

加载脚本从创建国家表开始:

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

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

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"/>

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

最佳答案

我终于弄明白了。 hibernate 似乎每行只需要一个语句,因为在我的例子中,脚本是格式化的,它只占用了语句的第一行,因此不完整。

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

关于java - 无法执行 JPA 架构生成创建命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com