hibernate学习中的第一个程序
请大家帮忙看看如何解决这个问题...
错误信息:
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();
}
}
请大家帮忙解决一下。 hibernate
[解决办法]
错误信息说com/test/hibernate/User.hbm.xml第二行出错,应该就在<hibernate-configuration>之前的部分,建议你去找一个正确的xml映射文件然后复制头部过来,光是这么看很难看出错误在哪,也许就是一两个字符的问题,
[解决办法]
这不都写着:
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.
[解决办法]
你User.hbm.xml中
<class name="com.test.hibernate.User" table=User>
少了类名,你只写了包路径没有跟上类,hibernate内部反射的时候无法解析的