属性 休眠。hbm2ddl.auto 未按预期工作 (MySQL 5.7)
Property hibernate.hbm2ddl.auto not working as expected (MySQL 5.7)
我想达到的目标:
写一点 class 在启动期间创建模式。如果架构存在,则应将其删除并重新创建。
我不得不说我是 Hibernate 的新手,所以错误很可能在我这边。
程序第一次运行良好,但第二次我得到一个异常(见下文)table 已经存在(这是真的,但我希望 Hibernate 删除然后重新创建它。
我的配置:
Hibernate 5.4.20,MySQL 5.7.9,MyISAM(InnoDB 上的结果相同),MS Windows 10(64 位)。
已经尝试了每个 MySQL方言,MYSQL5Dialect 和 MySQL57Dialect - 结果相同。
代码如下:
public class HibernateCreateRunner {
public static void main(String[] args) {
Map<String, String> settings = new HashMap<>();
settings.put("connection.driver_class", "com.mysql.jdbc.Driver");
//settings.put("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect");
settings.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
settings.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/stats_test?serverTimezone=UTC");
settings.put("hibernate.connection.username", "root");
settings.put("hibernate.connection.password", "root");
settings.put("hibernate.hbm2ddl.auto", "create");
settings.put("show_sql", "true");
MetadataSources metadataSources = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySettings(settings)
.build());
metadataSources.addAnnotatedClass(CaseAndResult.class);
metadataSources.addAnnotatedClass(CaseObject.class);
metadataSources.buildMetadata();
Metadata metadata = metadataSources.buildMetadata();;
SchemaExport schemaExport = new SchemaExport();
schemaExport.setHaltOnError(true)
.setFormat(true)
.setDelimiter(";")
.setOutputFile("db-schema.sql")
.createOnly(EnumSet.of(TargetType.DATABASE, TargetType.STDOUT), metadata);
}
}
这是我遇到的异常:
Exception in thread "main" org.hibernate.tool.schema.spi.SchemaManagementException: Halting on error : Error executing DDL "
create table CASEANDRESULT (
CASEID varchar(255) not null,
ASSESSMENTDATE datetime,
CARRESULTLEVELID integer,
CARRESULTLEVELNAME varchar(255),
CASEMODE varchar(255),
COMPLETE TINYINT,
LOCALE varchar(255),
NUMBEROFCHANGEASSESSCYCLES varchar(255),
NUMBEROFSOURCECHANGES integer,
PATCHNAME varchar(255),
POLICYCANBEISSUED TINYINT,
VERSIONNAME varchar(255),
primary key (CASEID)
) engine=MyISAM" via JDBC Statement
at org.hibernate.tool.schema.internal.ExceptionHandlerHaltImpl.handleException(ExceptionHandlerHaltImpl.java:27)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:443)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlStrings(SchemaCreatorImpl.java:423)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromMetadata(SchemaCreatorImpl.java:314)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:166)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:135)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:121)
at org.hibernate.tool.hbm2ddl.SchemaExport.doExecution(SchemaExport.java:296)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:249)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:228)
at org.hibernate.tool.hbm2ddl.SchemaExport.createOnly(SchemaExport.java:224)
at com.genre.star.database.HibernateCreateRunner.main(HibernateCreateRunner.java:46)
Caused by: org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
create table CASEANDRESULT (
CASEID varchar(255) not null,
ASSESSMENTDATE datetime,
CARRESULTLEVELID integer,
CARRESULTLEVELNAME varchar(255),
CASEMODE varchar(255),
COMPLETE TINYINT,
LOCALE varchar(255),
NUMBEROFCHANGEASSESSCYCLES varchar(255),
NUMBEROFSOURCECHANGES integer,
PATCHNAME varchar(255),
POLICYCANBEISSUED TINYINT,
VERSIONNAME varchar(255),
primary key (CASEID)
) engine=MyISAM" 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:439)
... 10 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'caseandresult' already exists
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:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2768)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:894)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:732)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
非常感谢任何提示!
您的代码遵循 Error Code: 1071. Specified key was too long; max key length is 1000 bytes
所以只使用以下长度
create table CASEANDRESULT8 (
CASEID varchar(250) not null,
ASSESSMENTDATE datetime,
CARRESULTLEVELID integer,
CARRESULTLEVELNAME varchar(255),
CASEMODE varchar(255),
COMPLETE TINYINT,
LOCALE varchar(255),
NUMBEROFCHANGEASSESSCYCLES varchar(255),
NUMBEROFSOURCECHANGES integer,
PATCHNAME varchar(255),
POLICYCANBEISSUED TINYINT,
VERSIONNAME varchar(255),
primary key (CASEID)
) engine=MyISAM
您正在调用仅创建架构的 createOnly
,并且 table 已经存在
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'caseandresult' already exists
如果您想先删除架构,您应该调用 SchemaExport.execute
并执行操作 BOTH
我想达到的目标:
写一点 class 在启动期间创建模式。如果架构存在,则应将其删除并重新创建。
我不得不说我是 Hibernate 的新手,所以错误很可能在我这边。
程序第一次运行良好,但第二次我得到一个异常(见下文)table 已经存在(这是真的,但我希望 Hibernate 删除然后重新创建它。
我的配置:
Hibernate 5.4.20,MySQL 5.7.9,MyISAM(InnoDB 上的结果相同),MS Windows 10(64 位)。
已经尝试了每个 MySQL方言,MYSQL5Dialect 和 MySQL57Dialect - 结果相同。
代码如下:
public class HibernateCreateRunner {
public static void main(String[] args) {
Map<String, String> settings = new HashMap<>();
settings.put("connection.driver_class", "com.mysql.jdbc.Driver");
//settings.put("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect");
settings.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
settings.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/stats_test?serverTimezone=UTC");
settings.put("hibernate.connection.username", "root");
settings.put("hibernate.connection.password", "root");
settings.put("hibernate.hbm2ddl.auto", "create");
settings.put("show_sql", "true");
MetadataSources metadataSources = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySettings(settings)
.build());
metadataSources.addAnnotatedClass(CaseAndResult.class);
metadataSources.addAnnotatedClass(CaseObject.class);
metadataSources.buildMetadata();
Metadata metadata = metadataSources.buildMetadata();;
SchemaExport schemaExport = new SchemaExport();
schemaExport.setHaltOnError(true)
.setFormat(true)
.setDelimiter(";")
.setOutputFile("db-schema.sql")
.createOnly(EnumSet.of(TargetType.DATABASE, TargetType.STDOUT), metadata);
}
}
这是我遇到的异常:
Exception in thread "main" org.hibernate.tool.schema.spi.SchemaManagementException: Halting on error : Error executing DDL "
create table CASEANDRESULT (
CASEID varchar(255) not null,
ASSESSMENTDATE datetime,
CARRESULTLEVELID integer,
CARRESULTLEVELNAME varchar(255),
CASEMODE varchar(255),
COMPLETE TINYINT,
LOCALE varchar(255),
NUMBEROFCHANGEASSESSCYCLES varchar(255),
NUMBEROFSOURCECHANGES integer,
PATCHNAME varchar(255),
POLICYCANBEISSUED TINYINT,
VERSIONNAME varchar(255),
primary key (CASEID)
) engine=MyISAM" via JDBC Statement
at org.hibernate.tool.schema.internal.ExceptionHandlerHaltImpl.handleException(ExceptionHandlerHaltImpl.java:27)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:443)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlStrings(SchemaCreatorImpl.java:423)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromMetadata(SchemaCreatorImpl.java:314)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:166)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:135)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:121)
at org.hibernate.tool.hbm2ddl.SchemaExport.doExecution(SchemaExport.java:296)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:249)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:228)
at org.hibernate.tool.hbm2ddl.SchemaExport.createOnly(SchemaExport.java:224)
at com.genre.star.database.HibernateCreateRunner.main(HibernateCreateRunner.java:46)
Caused by: org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
create table CASEANDRESULT (
CASEID varchar(255) not null,
ASSESSMENTDATE datetime,
CARRESULTLEVELID integer,
CARRESULTLEVELNAME varchar(255),
CASEMODE varchar(255),
COMPLETE TINYINT,
LOCALE varchar(255),
NUMBEROFCHANGEASSESSCYCLES varchar(255),
NUMBEROFSOURCECHANGES integer,
PATCHNAME varchar(255),
POLICYCANBEISSUED TINYINT,
VERSIONNAME varchar(255),
primary key (CASEID)
) engine=MyISAM" 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:439)
... 10 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'caseandresult' already exists
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:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2768)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:894)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:732)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
非常感谢任何提示!
您的代码遵循 Error Code: 1071. Specified key was too long; max key length is 1000 bytes
所以只使用以下长度
create table CASEANDRESULT8 (
CASEID varchar(250) not null,
ASSESSMENTDATE datetime,
CARRESULTLEVELID integer,
CARRESULTLEVELNAME varchar(255),
CASEMODE varchar(255),
COMPLETE TINYINT,
LOCALE varchar(255),
NUMBEROFCHANGEASSESSCYCLES varchar(255),
NUMBEROFSOURCECHANGES integer,
PATCHNAME varchar(255),
POLICYCANBEISSUED TINYINT,
VERSIONNAME varchar(255),
primary key (CASEID)
) engine=MyISAM
您正在调用仅创建架构的 createOnly
,并且 table 已经存在
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'caseandresult' already exists
如果您想先删除架构,您应该调用 SchemaExport.execute
并执行操作 BOTH