当前位置: 代码迷 >> J2EE >> Could not read mappings from resource解决方案
  详细解决方案

Could not read mappings from resource解决方案

热度:648   发布时间:2016-04-22 03:30:25.0
Could not read mappings from resource
请问我编写好Product.hbm.xml并在hibernate.cfg.xml中添加,代码如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  "http://hibernate.sourceforge.net/hibernat-mapping-3.0.dtd">  
<hibernate-mapping>
<class name="petstore.dao.Product" table="products">
<id name="id" column="productId">
<generator class="native"/>
</id>
<property name="pName" column="name"/>
<property name="description" column="description"/>
</class>
</hibernate-mapping>
做junit测试时候 报错
%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Could not read mappings from resource: petstore/domain/Product.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
at petstore.util.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:33)
at petstore.domain.C02Test.testAddProduct(C02Test.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.MappingException: Could not parse mapping document in input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:431)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
... 25 more
Caused by: org.dom4j.DocumentException: http://hibernate.sourceforge.net/hibernat-mapping-3.0.dtd Nested exception: http://hibernate.sourceforge.net/hibernat-mapping-3.0.dtd
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:422)
... 26 more


Junit代码如下:
public class C02Test extends TestCase{
  public void testAddProduct()throws Exception{
  Session session=HibernateSessionFactory.getSession();
  Transaction tx=null;
  try{
  tx=session.beginTransaction();
  Product p=new Product();
  p.setName("hibernate");
  p.setDescription("ORM Framework");
  相关解决方案