zoukankan      html  css  js  c++  java
  • 【原创】Hibernate通过实体类自动建表时type=MyISAM的问题

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载。

    当使用的mysql数据库为5.5版本时,方言需要设置为

    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    

     而非

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    两者设置方式的主要差别在于,Hibernate自动生成的sql 建表语句中,对数据库存储引擎的设置不同。

    对于实体类User来说:

    public class User {
        private int id;
        private String username;
        private String password;
        private String sex;
        private String address;
            .......
    }

    采用第一种设置,Hibernate生成的sql建表语句为:

    Hibernate: 
        
        create table tuser (
           id integer not null auto_increment,
            username varchar(255),
            password varchar(255),
            sex varchar(255),
            address varchar(255),
            primary key (id)
        ) engine=MyISAM
    

    通过Hibernate自动建表成功!

     采用第二种设置,Hibernate生成的sql建表语句为:

    Hibernate: 
        
        create table tuser (
           id integer not null auto_increment,
            username varchar(255),
            password varchar(255),
            sex varchar(255),
            address varchar(255),
            primary key (id)
        ) type=MyISAM

    通过Hibernate自动建表失败,且报错:

    .......
    org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
     ........
    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 'type=MyISAM' at line 8
    ........

    报错原因:

    虽然TYPE=MyISAM 和 ENGINE=MyISAM 都是设置数据库存储引擎的语句 。

    但是老版本的Mysql使用Type,新版本的Mysql使用ENGINE。

    虽然MySQL 5.1为向下兼容而支持这个语法,但TYPE现在已经被轻视,ENGINE才是首选的用法。

    一般情况下我们是无需考虑ENGINE的,除非你的mysql的默认数据库存储引擎为非ENGINE了。

     

  • 相关阅读:
    docker 启动redis
    mysql主从库搭建
    云镜象下载地址整理
    linux 命令积累
    canal 踩坑实录---这可能是你看到的最全最简单的canal教程
    数据库查询超级慢,数据库死锁的查看与解决
    微信公众平台开发---建立服务器与微信公众平台的链接
    使用sql更改表的列的数据类型和添加新列和约束
    Mac安装、配置MongoDB
    shell 变量
  • 原文地址:https://www.cnblogs.com/mengyi/p/7389720.html
Copyright © 2011-2022 走看看