(初学)hibernate问题,急救! Exception in thread "main" org.hibernate.InvalidMappingException: Un
Exceptioninthread"main"org.hibernate.InvalidMappingException:UnabletoreadXMLatorg.hib...
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:478)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:647)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
at Test.Test.main(Test.java:18)
Caused by: org.dom4j.DocumentException: Error on line 16 of document : Content is not allowed in trailing section. Nested exception: Content is not allowed in trailing section.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 11 more
hibernate4.14版本的
hibernate.cfg.xml如下
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_4.1.4_tset</property>
<property name="hibernate.connection.username">jiebo</property>
<property name="hibernate.connection.password">1234</property>
<mapping resource="moder/po/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.hbm.xml如下:
<!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="moder.po.User" table="Users">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"/>
<property name="password" column="password"/>
</class>
</hibernate-mapping>>
Test类
package Test;
import moder.po.User;
import org.hibernate.Session;import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test { public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
User user=new User();
user.setName("dsd");
user.setPassword("454");
session.save(user);
tx.commit();
}
} 展开
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:478)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:647)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
at Test.Test.main(Test.java:18)
Caused by: org.dom4j.DocumentException: Error on line 16 of document : Content is not allowed in trailing section. Nested exception: Content is not allowed in trailing section.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 11 more
hibernate4.14版本的
hibernate.cfg.xml如下
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_4.1.4_tset</property>
<property name="hibernate.connection.username">jiebo</property>
<property name="hibernate.connection.password">1234</property>
<mapping resource="moder/po/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.hbm.xml如下:
<!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="moder.po.User" table="Users">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"/>
<property name="password" column="password"/>
</class>
</hibernate-mapping>>
Test类
package Test;
import moder.po.User;
import org.hibernate.Session;import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test { public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
User user=new User();
user.setName("dsd");
user.setPassword("454");
session.save(user);
tx.commit();
}
} 展开
4个回答
展开全部
hbm.xml文件有问题呗,你把那个多余的空格什么的去掉,整理好那映射文件看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我的也是这个问题正在努力解决呢 命苦啊
Hibernate 4.1.4
Configuration config = new Configuration().configure();
ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory sf = config.buildSessionFactory(registry);
Hibernate 4.1.4
Configuration config = new Configuration().configure();
ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory sf = config.buildSessionFactory(registry);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
缺少文件包
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询