首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

Could not read mappings from resource解决方案

2012-02-01 
Could not read mappings from resource请问我编写好Product.hbm.xml并在hibernate.cfg.xml中添加,代码如

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");


  session.save(p);
  tx.commit();
  }catch(Exception e){
  if(tx!=null)
  tx.rollback();
  throw e;
  }finally{
  session.close();
  }  
  }
}  

请问是和原因?(Product.hbm.xml路径是和Product.java放在同一个petstore.domain包中,且hibernate.cfg.xml在web-inf的class下) 


[解决办法]
<class name="petstore.dao.Product" table="products"> 

...
Product.hbm.xml路径是和Product.java放在同一个petstore.domain包中
(@#&%(@#&%(@#&%(@&
行 就 有鬼了 。。。
[解决办法]
<class name="petstore.dao.Product" table="products">
[解决办法]
<class name="petstore.dao.Product" table="products"> 
改成
petstore.domain.Product

改下啊。。。
[解决办法]
dao?domain?
唉,都被lz搞糊涂了
[解决办法]

探讨
<class name="petstore.dao.Product" table="products">

...
Product.hbm.xml路径是和Product.java放在同一个petstore.domain包中
(@#&%(@#&%(@#&%(@&
行 就 有鬼了 。。。

热点排行