Hibernate 5.1.0 final SchemaExport求助

代码誊写工 2016-04-27 11:51:59
各位
我按照网上的资料和API,测试Hibernate 5.1.0,用SchemaExport.create创建表时,没有报错,但在数据库里并没有成功地创建表,因为网上关于Hibernate 5.10的资料太少,实在找不到原因,有没有用过hibernate 5.1的朋友,望不吝赐教。

贴上我的配置和代码:

hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin123</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.autocommit">true</property >
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="MyUser.hbm.xml"/>
<mapping class="com.example.hibernate.MyUser" />
</session-factory>
</hibernate-configuration>

MyUser.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.example.hibernate.MyUser" table="myuser">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username" length="20" column="username" type="string" not-null="true"></property>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>


ExportDB.java
package com.example.hibernate;

//import org.hibernate.cfg.Configuration;
import java.util.EnumSet;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;

public class ExportDB {
public static void main(String[] args){
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
}
}


运行:
D:\WWW>java -cp .\WEB-INF\classes;c:\apache-tomcat-9.0.0.M4\lib\hibernate-core-5.1.0.Final.jar;c:\apache-tomcat-9.0.0.M4\lib\jboss-logging-3.3.0.Final.jar;c:\apache-tomcat-9.0.0.M4\lib\geronimo-jta_1.1_spec-1.1.1.jar;c:\apache-tomcat-9.0.0.M4\lib\dom4j-1.6.1.jar;c:\apache-tomcat-9.0.0.M4\lib\hibernate-commons-annotations-5.0.1.Final.jar;c:\apache-tomcat-9.0.0.M4\lib\hibernate-jpa-2.1-api-1.0.0.Final.jar;c:\apache-tomcat-9.0.0.M4\lib\mysql-connector-java-6.0.2-bin.jar;c:\apache-tomcat-9.0.0.M4\lib\classmate-1.3.0.jar com.example.hibernate.ExportDB

运行结果:
四月 27, 2016 11:27:13 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
四月 27, 2016 11:27:13 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
四月 27, 2016 11:27:13 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
四月 27, 2016 11:27:13 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces m
ay be removed at any time.
四月 27, 2016 11:27:13 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
四月 27, 2016 11:27:13 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be remove
d at any time.
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/test]
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****, autocommit=true}
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: true
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Wed Apr 27 11:27:14 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if expl
icit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide tr
uststore for server certificate verification.
四月 27, 2016 11:27:14 上午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
四月 27, 2016 11:27:14 上午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: drop table if exists myuser
Hibernate: create table myuser (id integer not null auto_increment, username varchar(20) not null, password varchar(255), createTime datetime, expireTime datetime, primary key (id))
四月 27, 2016 11:27:14 上午 org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@2f16c6b3'


如果建立了表,插入数据还是可以成功的。

MyUser.java
package com.example.hibernate;
import java.util.Date;
public class MyUser {
private int Id;
private String username;
private String password;
private Date createtime;
private Date expiretime;
public int getId() {
return this.Id;
}
public void setId(int id) {
this.Id = id;
}
public String getusername() {
return this.username;
}
public void setusername(String username) {
this.username = username;
}
public String getpassword() {
return this.password;
}
public void setpassword(String password) {
this.password = password;
}
public Date getcreateTime() {
return createtime;
}
public void setcreateTime(Date createtime) {
this.createtime = createtime;
}
public Date getexpireTime() {
return expiretime;
}
public void setexpireTime(Date expiretime) {
this.expiretime = expiretime;
}
}


hibernateSession.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=utf-8"%>

<%@ page import="org.hibernate.Session" %>
<%@ page import="org.hibernate.SessionFactory" %>
<%@ page import="org.hibernate.service.ServiceRegistry" %>
<%@ page import="org.hibernate.boot.registry.StandardServiceRegistryBuilder" %>
<%@ page import="org.hibernate.boot.MetadataSources" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.example.hibernate.MyUser" %>

<%
ServiceRegistry sr = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sf = new MetadataSources(sr).buildMetadata().buildSessionFactory();

Session ss = null;
try {
ss = sf.openSession();

ss.beginTransaction();

MyUser u = new MyUser();
u.setId(1);
u.setusername("user01");
u.setpassword("passwd01");
u.setcreateTime(new Date());
u.setexpireTime(new Date());

ss.save(u);

ss.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
ss.getTransaction().rollback();
}finally {
if (ss != null) {
if (ss.isOpen()) {
ss.close();
}
}
if (sf != null) {
sf.close();
}
}
%>
...全文
837 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
灰白色可乐 2017-07-13
  • 打赏
  • 举报
回复
数据库方言使用MySQL5Dialect就可以,还要保证MySQL数据库中没有名称相同的表,即使大小写不同也不行,我测试的是Hibernate 5.2.10.Final
正气怪侠 2017-04-06
  • 打赏
  • 举报
回复
本来我也是菜鸟一个,我也遇到了这个问题。但无意中找到了解决办法,不知道有没有普遍的指导意义,拿出来参考吧。
我用的是hibernate5.2.9 ,原来的配置是这样的这里边有个很关键的配置,就是大神们所说的方言,<property name="dialect">org.hibernate.dialect.MySQLDialect</property>,这个参数是我用MyEclipse选择生成的。自动生成表格的代码是这样的:
public static void main(String[] args)
{
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport=new SchemaExport();
schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
}
运行后并没有报错,但是就是生不成表格。运行的信息提示是

我用到的数据库是mysql5.7,把刚才所说的方言改成<property name="dialect">org.hibernate.dialect.MySQL57InnoDBDialect</property>就搞定了,运行的信息提示是,它们的不同在最后一句,能生成表格的是 engine=InnoDB,不能生成表格的是type=MyISAM
zsilu 2017-03-02
  • 打赏
  • 举报
回复
遇到相同的问题,没有错误跑不出结果,数据库并没有建立新表。用的Hibernate 5.2。
「已注销」 2016-06-19
  • 打赏
  • 举报
回复
你的问题是数据库没有创建表,还时表创建了不能插入数据? 在你的hibernate.cfg.xml文件中,试着只写<mapping resource="MyUser.hbm.xml"/> 去掉<mapping class="com.example.hibernate.MyUser" />
代码誊写工 2016-05-15
  • 打赏
  • 举报
回复
有点儿郁闷,这么久没有人回答 求关注
代码誊写工 2016-04-27
  • 打赏
  • 举报
回复
顶起,希望能有人回答

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧