刚开始学习Hibernate框架,在测试自动生成数据库表的时候,出现无法自动创建表的问题
hibernate.cfg.xml配置文件的部分内容:
<!--
数据库方言
不同的数据库中,sql语法略有区别,指定方言可以让hibernate框架生成sql语句时,针对数据库的方言生成===如:mysql的limit(分页用的)
sql99标准:
DDL 定义语言 库、表的增删改查
DCL 控制语言 事务 权限
DML 操纵语言 增删改查
注意:MySQL在选择方言时,选择最短的这个方言(所有引擎的数据库都通用)
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--
## auto schema export 自动导出表结构 自动建表
#hibernate.hbm2ddl.auto create 自动建表,每次框架运行都会创建新的表,旧表将会被覆盖,表数据会丢失(开发环境中测试使用
#hibernate.hbm2ddl.auto create-drop 自动建表.每次框架运行结束都会将所有表删除
#hibernate.hbm2ddl.auto update 自动生成表,如果已经存在不会再生成.如果表又变动,则自动更新表(不会删除任何数据)
#hibernate.hbm2ddl.auto validate 校验,不自动生成表,每次启动会校验数据库中表是否正确(如:表不存在,则报错)
-->
<property name="hibernate.hbm2ddl.auto">update</property>
运行下面代码时,报错
public class Demo1 {
@Test
public void fun1() {
//保存客户
Configuration conf=new Configuration().configure();
SessionFactory sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
// ------------------------------------------------------------------
//在打开事务和提交事务之间,对数据库进行操作
Customer c=new Customer();
c.setCust_name("Google");
session.save(c);//执行保存
// ------------------------------------------------------------------
tx.commit();
session.close();
sessionFactory.close();
}
}
报错信息如下:
九月 18, 2019 5:03:19 下午 org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "
create table cst_customer (
cust_id bigint not null auto_increment,
cust_name varchar(255),
cust_source varchar(255),
cust_industry varchar(255),
cust_level varchar(255),
cust_linkman varchar(255),
cust_phone varchar(255),
cust_mobile varchar(255),
primary key (cust_id)
) type=MyISAM" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
create table cst_customer (
cust_id bigint not null auto_increment,
cust_name varchar(255),
cust_source varchar(255),
cust_industry varchar(255),
cust_level varchar(255),
cust_linkman varchar(255),
cust_phone varchar(255),
cust_mobile varchar(255),
primary key (cust_id)
) type=MyISAM" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:315)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at cn.ggs.a_generator.Demo1.fun1(Demo1.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.sql.SQLSyntaxErrorException: 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 11
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:782)
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:666)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
... 36 more
原因:
hibernate里的dialect和Mysql的版本不匹配
解决方法:
本萌新使用的数据库版本为MySQL8.0.16
修改hibernate.cfg.xml文件
MySql5.0之前的配置
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
MySql5.0之后的配置
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
也就加多个数字5,修改后就能正常的建表插入数据啦!!!
参考的博客:https://www.cnblogs.com/chashaotree/p/11093121.html