Java Hibernate 生成的 DDL 中的无效语法错误“type= MyISAM”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43716068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid syntax error "type= MyISAM" in DDL generated by Hibernate
提问by yogesh meghnani
I am getting this error for my Java code
我的 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
This is the query passed by hibernate:
这是 hibernate 传递的查询:
Hibernate: create table EMPLOYEE (emp_id integer not null, FNAME varchar(255), LNAME varchar(255), primary key (emp_id)) type=MyISAM
I have looked at all questions related to this error. But in all that questions the user itself is passing query "type = MyISAM
" so they can change "type
" to "engine
", but here hibernate is responsible for creating table, so I don't understand where the mistake is, and how I can fix it.
我已经查看了与此错误相关的所有问题。但是在所有这些问题中,用户本身正在传递查询“ type = MyISAM
”,因此他们可以将“ type
”更改为“ engine
”,但是这里 hibernate 负责创建表,所以我不明白错误在哪里,以及如何修复它。
This is my configuration file:
这是我的配置文件:
<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>
This is my mapping file:
这是我的映射文件:
<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>
This is my class file:
这是我的类文件:
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();
}
}
采纳答案by Mark Rotteveel
The problem is that the dialect org.hibernate.dialect.MySQLDialect
is for MySQL 4.x or earlier. The fragment TYPE=MYISAM
that is generated by this dialect was deprecated in MySQL 4.0 and removed in 5.5.
问题是方言org.hibernate.dialect.MySQLDialect
适用于 MySQL 4.x 或更早版本。TYPE=MYISAM
由这种方言生成的片段在 MySQL 4.0 中已弃用,并在 5.5 中删除。
Given that you use MariaDB, you need to use (depending on the version of MariaDB and - maybe - the version of Hibernate) one of:
鉴于您使用 MariaDB,您需要使用(取决于 MariaDB 的版本和 - 也许 - Hibernate 的版本)以下之一:
org.hibernate.dialect.MariaDBDialect
org.hibernate.dialect.MariaDB53Dialect
org.hibernate.dialect.MariaDBDialect
org.hibernate.dialect.MariaDB53Dialect
If you are using MySQL, or if the above two dialects for MariaDB don't exist in your version of Hibernate:
如果您使用的是 MySQL,或者如果您的 Hibernate 版本中不存在上述两种 MariaDB 方言:
org.hibernate.dialect.MySQL5Dialect
org.hibernate.dialect.MySQL55Dialect
org.hibernate.dialect.MySQL57Dialect
org.hibernate.dialect.MySQL5Dialect
org.hibernate.dialect.MySQL55Dialect
org.hibernate.dialect.MySQL57Dialect
回答by Brajesh
Its a Dialect related issue, instead of org.hibernate.dialect.MySQLDialect
you can go with org.hibernate.dialect.MySQL5Dialect
. It will work happily.
它是一个与方言相关的问题,而不是org.hibernate.dialect.MySQLDialect
你可以使用org.hibernate.dialect.MySQL5Dialect
. 它会愉快地工作。
回答by Vaibhav Jain
As dialect change is not working for me when I am using MySql database. Easiest way is to download and use standard version of Sql connector. Worked absolutely fine.
当我使用 MySql 数据库时,方言更改对我不起作用。最简单的方法是下载和使用标准版本的 Sql 连接器。工作得很好。
http://www.java2s.com/Code/Jar/m/Downloadmysqlconnectorjar.htm
http://www.java2s.com/Code/Jar/m/Downloadmysqlconnectorjar.htm
回答by Brajesh
Before MySQL 4.x.x version,TYPE MYISAM engine is used to store tables but in MySQL 5.x.x or later version MySQL is used ENGINE = MYISAM to store tables. e.g
在 MySQL 4.xx 版本之前,TYPE MYISAM 引擎用于存储表,但在 MySQL 5.xx 或更高版本中 MySQL 使用 ENGINE = MYISAM 来存储表。例如
In MySQL 4.x.x or < 4.x.x
在 MySQL 4.xx 或 < 4.xx
CREATE TABLE t (i INT) TYPE = MYISAM;
In MySQL 5.x.x or > 5.x.x
在 MySQL 5.xx 或 > 5.xx 中
CREATE TABLE t (i INT) ENGINE = MYISAM;
Now lets talk about hibernate,
现在让我们谈谈休眠,
In hibernate you must use given below dialect
在休眠中,您必须使用以下方言
MySQL <= 4.x.x
MySQL <= 4.xx
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
MySQL>=5.x.x.
MySQL>=5.xx
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
It will work fine.
它会正常工作。
回答by Janac Meena
MySQL dropped MyISAM as the default engine. Now it uses InnoDB. You can verify this using SequelPro by clicking on any table, and then looking in the bottom quadrant for the Table Information:
MySQL 放弃了 MyISAM 作为默认引擎。现在它使用 InnoDB。您可以使用 SequelPro 通过单击任何表格来验证这一点,然后在底部象限中查看表格信息:
And at the top of your window to see the MySQL Version:
在窗口顶部查看 MySQL 版本:
Using the above information, I know that I need the dialect:
使用以上信息,我知道我需要方言:
org.hibernate.dialect.MySQL5InnoDBDialect
org.hibernate.dialect.MySQL5InnoDBDialect