Hibernate5.2.6使用schemaExport报错以及创建表后数据库中不显示

主要是hibernate的更新速度已经超出了我的现象,今天测试schemaExport的时候直接懵逼了,为啥子不能创建表了呢,还报了很多错误,查看文档知道hibernate5的schemaExport方法跟4的有一定差别。不扯淡了….
直接进入正题!hibernate4的方法我就不累述了,网上特别多。。。。我用的是5.2.6版本的hibernate, mysql5.7。
1、首先把需要的包导进来(很多然后Build Path…….)
2、配置hibernate.cfg.xml

<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: 
    GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the 
    lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- 数据库的链接 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">数据库密码</property>
        <!--hibernate可选项 -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="format_sql">true</property>
        <property name="show_sql">true</property>
        <!--hbm文件 -->
        <mapping resource="com/siggy/pojo/Score.hbm.xml"/>

    </session-factory>
</hibernate-configuration>
``

3、接着开始配置个Scor.japackage com.siggy.pojo;

public class Score {
private int Id;
private String stuId;//学生编号
private String subjectId;//学科编号
private String result;//成绩
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getSubjectId() {
return subjectId;
}
public void setSubjectId(String subjectId) {
this.subjectId = subjectId;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}


}

4、配置Score.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.siggy.pojo.Score" table="score">
    <id name="id" >
    <!-- 生成策略 -->
    <generator class="native"></generator>
    </id>
    <!-- 类属性 -->
    <property name="Id" />
    <property name="stuId" />
    <property name="subjectId" />
    <property name="result" />
    </class>
</hibernate-mapping>

5、编写

package com.siggy.test;

import java.util.EnumSet;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.Test;

public class Hibernate {
    @Test
    public void testCreateDB(){
    //注意这里的写法跟hibernate4.x的写法有很大的不同
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();  
        Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();  
        SchemaExport schemaExport = new SchemaExport();  
        schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata); 

    }
}

看图
结果已经很明了了

总结:经过查找发现是hibernate里的dialect和Mysql的版本不匹配,SQL语句里的type=InnoDB使用在MySQL5.0之前,5.0之后就要是使用engine=InnoDB。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值