2013-04-20 51 views
4

当我运行该项目时,它发生如下异常。在Hibernate中添加一个视图表之前,每一个都包括Blame.hbm.xml等。它可以读取数据库中的数据。 session=HibernateSessionFactory.getSession();休眠错误:org.hibernate.InvalidMappingException:无法从资源解析映射文档

我例外

org.hibernate.InvalidMappingException: Could not parse mapping document from resource pojo/Blame.hbm.xml 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3415) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3404) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3392) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) 
    at session.factory.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:75) 
    at session.factory.HibernateSessionFactory.getSession(HibernateSessionFactory.java:57) 
    at dao.impl.ShowOrderDaoImpl.queryTradesByBid(ShowOrderDaoImpl.java:24) 
    at test.testHibernate.main(testHibernate.java:45) 
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping pojo.Blame 
ok 
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2580) 
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:174) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3412) 
    ... 8 more 
java.lang.NullPointerException 
    at dao.impl.ShowOrderDaoImpl.queryTradesByBid(ShowOrderDaoImpl.java:25) 
    at test.testHibernate.main(testHibernate.java:45) 

我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> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.password">root</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/disputesystem</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.show_sql">true</property> 
    <mapping resource="pojo/Blame.hbm.xml"/> 
    <mapping resource="pojo/Buyer.hbm.xml"/> 
    <mapping resource="pojo/Dispute.hbm.xml"/> 
    <mapping resource="pojo/Evidence.hbm.xml"/> 
    <mapping resource="pojo/Goods.hbm.xml"/> 
    <mapping resource="pojo/Message.hbm.xml"/> 
    <mapping resource="pojo/Record.hbm.xml"/> 
    <mapping resource="pojo/Seller.hbm.xml"/> 
    <mapping resource="pojo/Staff.hbm.xml"/> 
    <mapping resource="pojo/Trade.hbm.xml"/> 
    <mapping resource="pojo/Fund.hbm.xml"/> 
    <mapping resource="pojo/Showorderbybid.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

我ShowOrderDaoImpl.java文件

package dao.impl; 

import java.util.ArrayList; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.List; 

import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.Transaction; 

import pojo.Showorderbybid; 
import session.factory.HibernateSessionFactory; 
import dao.intf.ShowOrderDao; 

public class ShowOrderDaoImpl implements ShowOrderDao{ 

    @Override 
    public List<Showorderbybid> queryTradesByBid(String bid) throws Exception { 
     Session session=null; 
     Transaction transaction=null; 
     List<Showorderbybid> orders=new ArrayList<Showorderbybid>(); 
     try { 
      session=HibernateSessionFactory.getSession(); 
      transaction=session.beginTransaction(); 
      Query query=session.createQuery("from Showorderbybid where bid=?"); 
      query.setParameter(0, bid); 
      orders=(List<Showorderbybid>)query.list(); 
      List CountView = new ArrayList(); 
      Iterator it = orders.iterator(); 
      while (it.hasNext()) { 
       Object[] all = (Object[]) it.next(); 
       Showorderbybid countViewId = new Showorderbybid(); 
       countViewId.setAmount((Integer) all[0]); 
       countViewId.setBid((String) all[1]); 
       countViewId.setDetail((String) all[2]); 
       countViewId.setGid((String)all[3]); 
       countViewId.setGname((String)all[4]); 
       countViewId.setLogistics((String)all[5]); 
       countViewId.setSid((String)all[6]); 
       countViewId.setSname((String)all[7]); 
       countViewId.setStatus((String)all[8]); 
       countViewId.setTid((String)all[9]); 
       countViewId.setTotalmoney((Integer) all[10]); 
       countViewId.setTradetime((Date)all[11]); 
       orders.add(countViewId); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     }finally{ 
      HibernateSessionFactory.closeSession(); 
     } 
     return orders; 
    } 

} 
:在ShowOrderDaoImpl.java这句话出现异常

我的Blame.hbm.xml文件

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated 2013-4-19 23:02:05 by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping> 
    <class name="pojo.Blame" table="blame" catalog="disputesystem"> 
     <id name="id" type="java.lang.Integer"> 
      <column name="id" /> 
      <generator class="identity" /> 
     </id> 
     <many-to-one name="dispute" class="pojo.Dispute" fetch="select"> 
      <column name="disputeid" length="20" not-null="true" /> 
     </many-to-one> 
     <property name="blametime" type="timestamp"> 
      <column name="blametime" length="19" not-null="true" /> 
     </property> 
     <property name="content" type="string"> 
      <column name="content" length="1000" /> 
     </property> 
    </class> 
</hibernate-mapping> 
+0

异常overrided等号/的toString和哈希methodes ShowOrderDaoImpl.java这句话:session = HibernateSessionFactory.getSession(); – MindHacks 2013-04-20 08:49:58

+0

Showorderbybid是视图 – MindHacks 2013-04-20 08:51:21

+0

您的原始xml文件在<?xml version =“1.0”?>之前是否包含额外的空格?它不应该在那里。 – 2013-04-20 08:56:00

回答

-2

检查session的原因= HibernateSessionFactory.getSession();变得空。这就是空指针的原因

+0

如何检查? – MindHacks 2013-04-20 09:06:51

3

看起来好像还有另一个映射pojo.Blame。你可能有一个copy/paste错误:

<class name="pojo.Blame" 

在另一个hbm文件。

Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping pojo.Blame 

指出,当达到hbm文件时,它已经有一个名为pojo.Blame类的定义。

看起来像在一些本文件:

<mapping resource="pojo/Blame.hbm.xml"/> 
<mapping resource="pojo/Buyer.hbm.xml"/> 
<mapping resource="pojo/Dispute.hbm.xml"/> 
<mapping resource="pojo/Evidence.hbm.xml"/> 
<mapping resource="pojo/Goods.hbm.xml"/> 
<mapping resource="pojo/Message.hbm.xml"/> 
<mapping resource="pojo/Record.hbm.xml"/> 
<mapping resource="pojo/Seller.hbm.xml"/> 
<mapping resource="pojo/Staff.hbm.xml"/> 
<mapping resource="pojo/Trade.hbm.xml"/> 
<mapping resource="pojo/Fund.hbm.xml"/> 
<mapping resource="pojo/Showorderbybid.hbm.xml"/> 

是放错地方<class name="pojo.Blame"

+0

你的意思是我应该把class name =“pojo.Blame”粘贴到。或者应该替换哪一句话? – MindHacks 2013-04-20 09:13:23

+1

是的,这个答案似乎是最合乎逻辑的'重复类/实体映射pojo.Blame'。在某个hbm文件中有一个错字。你有复制粘贴你的HBM文件,并忘记更改映射/表。 – Siddharth 2013-04-20 09:26:36

+0

谢谢,但是我已经检查了所有的hbm文件,有正确的类名以及表名。因此,我猜这不是复制/粘贴问题。在向Hibernate添加视图之前,我的项目运行良好。我猜在视图表“Showorderbybid”中的问题。 – MindHacks 2013-04-20 12:51:33

相关问题