User.hbm.xml not found

andylauxing 2011-09-09 10:39:35
新手请教,我的User.hbm.xml文件该放在哪?
如下,
我的项目结构如下:

myHibernate
|--src
|--entity
|--User.java
|--User.hbm.xml
|--test
|--UserTest.java
|--hibernate.cfg.xml
|--log4j.properties

我现在运行UserTest.java报如下错误:
-----------------------------------------------------------------------------------------------------------------------
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: myHibernate/entity/User.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:563)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at test.UserTest.main(UserTest.java:14)
...全文
839 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
壮壮装壮 2013-03-06
  • 打赏
  • 举报
回复
User.hbm.xml配置文件的头应该是<!DOCTYPE hibernate-mapping PUBLIC......
ys_wangtao 2011-09-10
  • 打赏
  • 举报
回复
User.hbm.xml文件的根元素不是hibernate-configuration,最好把User.hbm.xml文件内容贴出来
andylauxing 2011-09-10
  • 打赏
  • 举报
回复
风尘中国:改了还是一样。
风尘中国 2011-09-10
  • 打赏
  • 举报
回复
第一个测试类用的Session不对,要用org.hibernate.Session而不是jms那个
迷茫 2011-09-10
  • 打赏
  • 举报
回复
org.hibernate.classic.Session session=sf.openSession();


Sesssion 的包不对

org.hibernate.Session session=sf.openSession();
风尘中国 2011-09-10
  • 打赏
  • 举报
回复
项目发我邮箱吧gaoyong2658376@gmail.com ,我看看
andylauxing 2011-09-10
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ioe_gaoyong 的回复:]
第一个测试类用的Session不对,要用org.hibernate.Session而不是jms那个
[/Quote]

风尘中国,源码已发,thanks.
andylauxing 2011-09-10
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 ys_wangtao 的回复:]
User.hbm.xml文件的根元素不是hibernate-configuration,最好把User.hbm.xml文件内容贴出来
[/Quote]

代码如下:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-mapping>
<class name="myHibernate.entity.User" table="users">
<id name="id" type="java.lang.Interger">
<column name="id"/>
</id>

<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true"/>
</property>

<property name="password" type="java.lang.String">
<column name="password" length="10" not-null="true"/>
</property>
</class>

</hibernate-mapping>

andylauxing 2011-09-09
  • 打赏
  • 举报
回复
是这个呀,
我的测试类如下:
package test;

import javax.jms.Session;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import entity.User;

public class UserTest {
public static void main(String[] args) {
User user=new User();
Configuration cf=new Configuration().configure();
SessionFactory sf=cf.buildSessionFactory();
org.hibernate.classic.Session session=sf.openSession();

Transaction tr=session.beginTransaction();
session.save(user);
tr.commit();


}
}

其中还有一个类:HibernateSessionFactory是在MyEclipse下加入Hibernate功能自动生成的,代码如下:
package test;


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 <code>SessionFactory</code> 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;
}

}


风尘中国 2011-09-09
  • 打赏
  • 举报
回复
你确定这个配置文件出现异常了么,从XML内容看没有问题,现在还有这个异常抛出?

你确定项目里面用的是这个XML?

[Quote=引用 9 楼 andylauxing 的回复:]

引用 7 楼 ioe_gaoyong 的回复:

这次是你的hibernate.cfg.xml文件的标签不匹配导致的,把这个XML文件贴出来,就能看到问题了。

错误信息说hibernate-mapping跟hibernate-configuration不匹配

引用 5 楼 andylauxing 的回复:

去掉了还是报错:

22:57:30,034 ERROR XML……
[/Quote]
andylauxing 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ioe_gaoyong 的回复:]

这次是你的hibernate.cfg.xml文件的标签不匹配导致的,把这个XML文件贴出来,就能看到问题了。

错误信息说hibernate-mapping跟hibernate-configuration不匹配

引用 5 楼 andylauxing 的回复:

去掉了还是报错:

22:57:30,034 ERROR XMLHelper:61 - Error parsing X……
[/Quote]

我的配置如下:

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

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:sqlserver://localhost:1030;databaseName=flight</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="myeclipse.connection.profile">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>

<mapping resource="entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
风尘中国 2011-09-09
  • 打赏
  • 举报
回复
这次是你的hibernate.cfg.xml文件的标签不匹配导致的,把这个XML文件贴出来,就能看到问题了。

错误信息说hibernate-mapping跟hibernate-configuration不匹配

[Quote=引用 5 楼 andylauxing 的回复:]

去掉了还是报错:

22:57:30,034 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(6) Document root element "hibernate-mapping", must match DOCTYPE root "hibernate-configuration".
22:57:30,040 ERROR X……
[/Quote]
hepeng_8 2011-09-09
  • 打赏
  • 举报
回复
路径问题把
andylauxing 2011-09-09
  • 打赏
  • 举报
回复
去掉了还是报错:

22:57:30,034 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(6) Document root element "hibernate-mapping", must match DOCTYPE root "hibernate-configuration".
22:57:30,040 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(6) Element type "hibernate-mapping" must be declared.
22:57:30,041 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(7) Element type "class" must be declared.
22:57:30,042 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(8) Element type "id" must be declared.
22:57:30,042 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(9) Element type "column" must be declared.
22:57:30,043 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(12) Attribute "type" must be declared for element type "property".
22:57:30,043 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(13) Element type "column" must be declared.
22:57:30,044 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(14) The content of element type "property" must match "null".
22:57:30,045 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(16) Attribute "type" must be declared for element type "property".
22:57:30,045 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(17) Element type "column" must be declared.
22:57:30,046 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(18) The content of element type "property" must match "null".
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource entity/User.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at test.UserTest.main(UserTest.java:14)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:502)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 7 more
Caused by: org.xml.sax.SAXParseException: Document root element "hibernate-mapping", must match DOCTYPE root "hibernate-configuration".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
... 8 more
风尘中国 2011-09-09
  • 打赏
  • 举报
回复
把前面的myHibernate/去掉试试

[Quote=引用 3 楼 andylauxing 的回复:]

我是这样写的:

<mapping resource="myHibernate/entity/User.hbm.xml"/>
[/Quote]
andylauxing 2011-09-09
  • 打赏
  • 举报
回复
我是这样写的:

<mapping resource="myHibernate/entity/User.hbm.xml"/>
风尘中国 2011-09-09
  • 打赏
  • 举报
回复
User.hbm.xml放在哪里关键是你hibernate.cfg.xml文件里面怎么写的User.hbm.xml路径,看你的目录结构,你试试在hibernate.cfg.xml里面更改路径 为entity/User.hbm.xml
gentleboy2009 2011-09-09
  • 打赏
  • 举报
回复
myHibernate/entity/ 放这个下面

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧