hibernate学习中的第一个程序

Max7Chou 2013-09-10 12:28:45
请大家帮忙看看如何解决这个问题...
错误信息:
2013-9-10 0:18:51 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
2013-9-10 0:18:51 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
2013-9-10 0:18:51 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2013-9-10 0:18:51 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2013-9-10 0:18:51 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2013-9-10 0:18:51 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2013-9-10 0:18:51 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!
2013-9-10 0:18:51 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/test/hibernate/User.hbm.xml
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:488)
at org.hibernate.cfg.Configuration.add(Configuration.java:484)
at org.hibernate.cfg.Configuration.add(Configuration.java:657)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2160)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2140)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2093)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at TestMain.main(TestMain.java:19)
Caused by: org.dom4j.DocumentException: Error on line 2 of document : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 11 more



程序目录结构:



User.hbm.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-mapping >
<class name="com.test.hibernate" table=User>
<id name="id" >
<generator class="native"/>
</id>
<property name="name"></property>
<property name="age"></property>
</class>

</hibernate-mapping>


User类:

package com.test.hibernate;

public class User {

private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


}





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>
<!-- 连接数据库的URL -->
<property name="connection.url">jdbc:mysql://localhost:3306/test_hibernate?characterEncoding=UTF-8</property>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>

<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

<property name="connection.password">123456</property>

<property name="show_sql">true</property>


<mapping resource="com/test/hibernate/User.hbm.xml"/>


</session-factory>

</hibernate-configuration>




TestMain文件:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.test.hibernate.User;


public class TestMain {

public static void main(String[] args){
User mUser = new User();
mUser.setId(1);
mUser.setAge(123);
mUser.setName("fucker");

Configuration conf = new Configuration();
@SuppressWarnings("deprecation")
SessionFactory sessionFactory = conf.configure().buildSessionFactory();

Session session = sessionFactory.openSession();


Transaction transaction = session.beginTransaction();
session.save(mUser);
transaction.commit();
session.close();
sessionFactory.close();
}
}
请大家帮忙解决一下。
...全文
264 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Max7Chou 2013-09-10
  • 打赏
  • 举报
回复
一楼和三楼都说到了,我改正后就可以运行了,谢谢。 二楼说的这个我也看到了,只是看不懂,同样感谢大家的回答。
suciver 2013-09-10
  • 打赏
  • 举报
回复
你User.hbm.xml中 <class name="com.test.hibernate.User" table=User> 少了类名,你只写了包路径没有跟上类,hibernate内部反射的时候无法解析的
失落夏天 2013-09-10
  • 打赏
  • 举报
回复
这不都写着: Caused by: org.dom4j.DocumentException: Error on line 2 of document : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
stonespace 2013-09-10
  • 打赏
  • 举报
回复
错误信息说com/test/hibernate/User.hbm.xml第二行出错,应该就在<hibernate-configuration>之前的部分,建议你去找一个正确的xml映射文件然后复制头部过来,光是这么看很难看出错误在哪,也许就是一两个字符的问题,

81,122

社区成员

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

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