hibernate的一个问题

配置文件如下:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


sa

jdbc:microsoft:sqlserver://10.28.9.10:1433;databasename=OA


org.hibernate.dialect.SQLServerDialect

SQL Server
sa

com.microsoft.jdbc.sqlserver.SQLServerDriver

<property name="show_sql">true</property>

<!-- 在这来添加新的映射文件 -->
<mapping resource="org/dpca/entity/Role.hbm.xml" />

出错提示为:
14:46:13,562 ERROR XMLHelper:61 - Error parsing XML: /hibernate.cfg.xml(31) The element type "mapping" must be terminated by the matching end-tag "".
%%%% Error Creating SessionFactory %%%%
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml

新学hibernate,菜鸟一个,请各位帮我看看!
[b]问题补充:[/b]
我想可能其他的地方有问题,补充一下
package org.dpca.common.core;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**

  • Configures and provides access to Hibernate sessions, tied to the
  • current thread of execution. Follows the Thread Local Session
  • pattern, see {@link http://hibernate.org/42.html }.
    */
    public class HibernateSessionFactory {

    /**

    • Location of hibernate.cfg.xml file.
    • Location should be on the classpath as Hibernate uses
    • #resourceAsStream style lookup for its configuration file.
    • The default classpath location of the hibernate config file is
    • in the default package. Use #setConfigFile() to update
    • the location of the configuration file for the current session.
      */ private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; private static final ThreadLocal threadLocal = new ThreadLocal(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION;

    static {
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
    }
    private HibernateSessionFactory() {
    }

    /**

    • Returns the ThreadLocal Session instance. Lazy initialize
    • the SessionFactory if needed. *
    • @return Session
    • @throws HibernateException
      */
      public static Session getSession() throws HibernateException {
      Session session = (Session) threadLocal.get();

      if (session == null || !session.isOpen()) {
      if (sessionFactory == null) {
      rebuildSessionFactory();
      }
      session = (sessionFactory != null) ? sessionFactory.openSession()
      : null;
      threadLocal.set(session);
      }

      return session;
      }

    /**

    • Rebuild hibernate session factory * */ public static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } }

    /**

    • Close the single hibernate session instance. *
    • @throws HibernateException
      */
      public static void closeSession() throws HibernateException {
      Session session = (Session) threadLocal.get();
      threadLocal.set(null);

      if (session != null) {
      session.close();
      }
      }

    /**

    • return session factory * */ public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory; }

    /**

    • return session factory *
    • session factory will be rebuilded in the next call */ public static void setConfigFile(String configFile) { HibernateSessionFactory.configFile = configFile; sessionFactory = null; }

    /**

    • return hibernate configuration * */ public static Configuration getConfiguration() { return configuration; }

}
[b]问题补充:[/b]
再补充一下
15:20:11,843 ERROR XMLHelper:61 - Error parsing XML: /hibernate.cfg.xml(31) The element type "mapping" must be terminated by the matching end-tag "".
%%%% Error Creating SessionFactory %%%%
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1418)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
at org.dpca.common.core.HibernateSessionFactory.(HibernateSessionFactory.java:30)
at org.dpca.common.core.BaseHibernateDao.getSession(BaseHibernateDao.java:49)
at org.dpca.common.core.BaseHibernateDao.(BaseHibernateDao.java:25)
at org.dpca.dao.RoleDao.(RoleDao.java:7)
at org.dpca.biz.RoleService.createRole(RoleService.java:20)
at test.Test.main(Test.java:20)
Caused by: org.dom4j.DocumentException: Error on line 31 of document : The element type "mapping" must be terminated by the matching end-tag "". Nested exception: The element type "mapping" must be terminated by the matching end-tag "".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1408)
... 7 more
%%%% Error Creating SessionFactory %%%%
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1418)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
at org.dpca.common.core.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:69)
at org.dpca.common.core.HibernateSessionFactory.getSession(HibernateSessionFactory.java:53)
at org.dpca.common.core.BaseHibernateDao.getSession(BaseHibernateDao.java:49)
at org.dpca.common.core.BaseHibernateDao.(BaseHibernateDao.java:25)
at org.dpca.dao.RoleDao.(RoleDao.java:7)
at org.dpca.biz.RoleService.createRole(RoleService.java:20)
at test.Test.main(Test.java:20)
Caused by: org.dom4j.DocumentException: Error on line 31 of document : The element type "mapping" must be terminated by the matching end-tag "". Nested exception: The element type "mapping" must be terminated by the matching end-tag "".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1408)
... 8 more
15:20:11,859 ERROR XMLHelper:61 - Error parsing XML: /hibernate.cfg.xml(31) The element type "mapping" must be terminated by the matching end-tag "".
null

3个回答


是不是这个映射文件没有找到啊

DOCTYPE 头引用错了。
[code="java"]<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">[/code]

Error parsing XML: /hibernate.cfg.xml(31) The element type "mapping" must be terminated by the matching end-tag "".

看异常信息说得很清楚,mapping元素必须要有匹配的结束标记

[code="java"]
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


sa

jdbc:microsoft:sqlserver://10.28.9.10:1433;databasename=OA


org.hibernate.dialect.SQLServerDialect

SQL Server
sa

com.microsoft.jdbc.sqlserver.SQLServerDriver

true

[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!