gpt4 book ai didi

java - JPA 未正确更新 SQL 数据库

转载 作者:行者123 更新时间:2023-11-29 17:00:46 26 4
gpt4 key购买 nike

我是使用 JPA 的新手,我有一个人实体类,我想在其中显示一些信息,首先当我创建该类时,我将 ID 设置为

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

生成的 ID 为 51,我的老师告诉我将其更改为:

    @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

所以它只是从 1 开始,然后为每个人累加。

现在我有一个结构java类,我想在其中删除运行该类的每个新实例的当前数据库,并且我有以下代码:

    public static void main(String[] args) 


{
HashMap<String, Object> puProperties = new HashMap<>();

puProperties.put("javax.persistence.sql-load-script-source", "Scripts/ClearDB.sql");
Persistence.generateSchema("jpadb", puProperties);


puProperties.remove("javax.persistence.sql-load-script-source");
Persistence.generateSchema("jpadb", puProperties);
}

使用这样的脚本:

drop database jpadb if exists;
create database jpadb;

这是我得到的错误:

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 'if exists' at line 1
Error Code: 1064
Call: drop database jpadb if exists;
Query: DataModifyQuery(sql="drop database jpadb if exists;")
[EL Warning]: 2018-09-11 16:14:41.361--ServerSession(2045766957)--
Exception [EclipseLink-4002] (Eclipse Persistence Services -
2.7.3.v20180807-4be1041):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Can't create database
'jpadb'; database exists
Error Code: 1007

我不知道语法错误指的是什么?

最佳答案

将 drop 语句更改为

drop database if exists jpadb;

错误信息很清楚你的语句哪里错了,

Call: drop database jpadb if exists;
Query: DataModifyQuery(sql="drop database jpadb if exists;")

关于java - JPA 未正确更新 SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52278032/

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