jpa未正确更新sql数据库

m4pnthwp  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(513)

我对使用jpa还比较陌生,我有一个person实体类,我想在其中显示一些信息,首先当我创建这个类时,我将id设置为

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

它生成了一个51的id,我的老师告诉我把它改成:

@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

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

smtd7mpg

smtd7mpg1#

将放弃声明更改为

drop database if exists jpadb;

错误信息很清楚你的陈述哪里是错的,

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

kq0g1dla2#

使用此脚本;

DROP DATABASE IF EXISTS jpadb;

您的snytax错误已写入 drop database jpadb if exists; . 你的问题其实是关键字的顺序。查看更多mysql drop文档

相关问题