属性hibernate.hbm2ddl.auto未按预期工作(mysql 5.7)

wdebmtf2  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(393)

我想要达到的目标:
编写一个小类,在启动期间创建一个模式。如果架构存在,应该删除它并重新创建。
我不得不说我对冬眠还很陌生,所以很可能错误就在我这边。
程序第一次运行正常,但第二次我得到一个异常(见下文),表已经存在(这是真的,但我希望hibernate删除然后重新创建它)。
我的配置:
hibernate 5.4.20、mysql 5.7.9、myisam(与innodb上的结果相同),ms windows 10(64位)。
已经试过了所有的mysqldialect,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)

任何提示都非常感谢!

rqdpfwrv

rqdpfwrv1#

你在打电话吗 createOnly 它只创建模式,并且表已经存在

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'caseandresult' already exists

如果要先删除模式,应该调用 SchemaExport.execute 采取行动 BOTH

5w9g7ksd

5w9g7ksd2#

你得到你的代码如下 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

相关问题