Hibernate生成的DDL中语法错误"type = MyISAM"无效

yog*_*ani 30 java mysql hibernate mariadb

我的Java代码出现此错误

   Caused by :`com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException`: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB` server version for the right syntax to use near 'type = `MyISAM`' at line 1
Run Code Online (Sandbox Code Playgroud)

这是hibernate传递的查询:

Hibernate: create table EMPLOYEE (emp_id integer not null, FNAME varchar(255), LNAME varchar(255), primary key (emp_id)) type=MyISAM
Run Code Online (Sandbox Code Playgroud)

我查看了与此错误相关的所有问题.但是在所有问题中,用户本身都在传递查询" type = MyISAM",因此他们可以将" type"改为" engine",但是这里hibernate负责创建表,所以我不明白错误在哪里,以及我如何解决它.

这是我的配置文件:

<hibernate-configuration>
 <session-factory >
 <property name="hibernate.hbm2ddl.auto">create</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/servletcheck</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password"> </property>
  <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <mapping resource="Employee.hbm.xml"/>
 </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

这是我的映射文件:

<hibernate-mapping>
    <class name="first.Employee" table="EMPLOYEE">
        <id name="id" type="int">
            <column name="emp_id" />
            <generator class="assigned" />
        </id>
        <property name="fname" type="java.lang.String">
            <column name="FNAME" />
        </property>
        <property name="lname" type="java.lang.String">
            <column name="LNAME" />
        </property>
    </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

这是我的类文件:

public class StoreData {
    public static void main(String[] args) {
        Configuration cfg=new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory factory=cfg.buildSessionFactory();
        Session session=factory.openSession();
        org.hibernate.Transaction t=session.beginTransaction();
        Employee e=new Employee();
        e.setId(1);
        e.setFname("yogesh");
        e.setLname("Meghnani");
        session.persist(e);
        t.commit();

    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*eel 99

问题是方言org.hibernate.dialect.MySQLDialect适用于MySQL 4.x或更早版本.TYPE=MYISAM此方言生成的片段在MySQL 4.0中已弃用,在5.5中已删除.

鉴于您使用MariaDB,您需要使用(取决于MariaDB的版本和 - 可能 - Hibernate的版本)之一:

  • org.hibernate.dialect.MariaDBDialect
  • org.hibernate.dialect.MariaDB53Dialect

如果您使用的是MySQL,或者您的Hibernate版本中不存在MariaDB的上述两种方言:

  • org.hibernate.dialect.MySQL5Dialect
  • org.hibernate.dialect.MySQL55Dialect
  • org.hibernate.dialect.MySQL57Dialect


Bra*_*esh 14

它是一个方言相关的问题,而不是org.hibernate.dialect.MySQLDialect你可以去org.hibernate.dialect.MySQL5Dialect.它会愉快地工作.


Cam*_*zie 5

随着越来越多的组织开始安装 MySQL 8.x,而他们的 Hibernate 代码仍在使用旧版本 5 方言,此错误可能会越来越频繁地出现。

截至 2020 年 6 月 6 日的最新 Hibernate 5.4 发行版包括以下 MySQL 方言:

  • MySQL55方言
  • MySQL57方言
  • MySQL57InnoDB方言
  • MySQL5方言
  • MySQL8方言
  • MySQL方言
  • MySQLInnoDB方言
  • MySQLISAMD方言

当前的 Hibernate 5.4 MySQL 方言

这是为 MySQL 过滤的核心 jar 文件中的方言。使用正确的type=MyISAM MySQL异常将成为过去。

在此处输入图片说明