我的 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
这是休眠传递的查询:
Hibernate: create table EMPLOYEE (emp_id integer not null, FNAME varchar(255), LNAME varchar(255), primary key (emp_id)) type=MyISAM
我已经查看了与此错误相关的所有问题。但在所有这些问题中,用户本身正在传递查询“ type = MyISAM
”,因此他们可以将“ type
”更改为“ engine
”,但这里负责hi-ber-nate创建表,所以我不明白错误在哪里,以及如何解决它。
这是我的配置文件:
<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>
这是我的映射文件:
<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>
这是我的类文件:
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();
}
}
原文由 yogesh meghnani 发布,翻译遵循 CC BY-SA 4.0 许可协议
问题是 - 在 Hibernate 5.x 和更早版本中 - 方言
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
org.hibernate.dialect.MariaDB106Dialect
)如果您使用的是 MySQL,或者如果您的 Hibernate 版本中不存在上述两种 MariaDB 方言:
org.hibernate.dialect.MySQL5Dialect
org.hibernate.dialect.MySQL55Dialect
org.hibernate.dialect.MySQL57Dialect
org.hibernate.dialect.MySQL8Dialect
org.hibernate.dialect.MySQL57InnoDBDialect
)