刚学hibernate出错.

JPF1024 2013-09-01 03:09:20
是一个手动创建数据表的小程序.

[ShowDB.java]

package com.hibernate;


import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
* 手动导出数据表
*
* @author puruidong
* @version 2013.8.30
*/

public class ShowDB {

public static void main(String[]args){
Configuration cfg = new Configuration().configure() ;//加载配置信息
SchemaExport export = new SchemaExport(cfg); //实例化SchemaExport.
System.out.println(export);
export.create(true,true);//到处数据表



}
}


[User.java]

package com.hibernate;

public class User {

private int num;
private String name;
private int age ;
private String address;
//省略set/get方法

}


[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"><!-- dtd无法访问. -->
<hibernate-configuration>
<session-factory>
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库连接 -->
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/Test</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 打印SQL语句 -->
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/hibernate/User.hbm.xml" />

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

[User.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.hibernate.User" table="Studentss" >
<id name="num" column="num" type="int">
<generator class="native"></generator>
</id>
<property name="name" type="string" length="20">
<column name="name"></column>
</property>
<property name="age" type="int">
<column name="age"></column>
</property>
<property name="address" type="string" length="200">
<column name="address"></column>
</property>
</class>
</hibernate-mapping>

运行之后:
Sep 01, 2013 3:06:48 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Sep 01, 2013 3:06:49 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Sep 01, 2013 3:06:49 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
Sep 01, 2013 3:06:49 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 01, 2013 3:06:49 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 01, 2013 3:06:49 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Sep 01, 2013 3:06:49 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2096)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at com.hibernate.ShowDB.main(ShowDB.java:17)
Caused by: org.dom4j.DocumentException: null Nested exception: null
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2088)
... 3 more

跟dtd不能访问有关系吗?
...全文
192 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
找到hibernate-configuration-3.0.dtd源文件,去这个文件里查看一下开头怎么写的,直接复制过来就行,好像是在这个文件的注释部分里。。。
  • 打赏
  • 举报
回复
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 改为 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
JPF1024 2013-09-01
  • 打赏
  • 举报
回复
引用 2 楼 fangmingshijie 的回复:
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 改为 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
后来找了一个程序,在windows下面用MyEclipse执行没有任何问题,而到了Eclipse上面就报错了。没理解。。。
JPF1024 2013-09-01
  • 打赏
  • 举报
回复

我的目录结构,等高手/...

81,092

社区成员

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

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