org.hibernate.HibernateException: /hibernate.cfg.xml not found问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/h273979586/article/details/79369014

首先我们介绍一下Maven资源文件相关配置

src/main/java和src/test/java 


这两个目录中的所有*.java文件会分别在comile和test-comiple阶段被编译,编译结果分别放到了target/classes和targe/test-classes目录中,但是这两个目录中的其他文件都会被忽略掉。

src/main/resources和src/test/resources

这两个目录中的文件也会分别被复制到target/classes和target/test-classes目录中。

target/classes

打包插件默认会把这个目录中的所有内容打入到jar包或者war包中。


了解目录结构说一下错误产生原因,不同的操作可能导致相同错误,小编只介绍自己遇到的情况。

项目运行时target\classes目录下编译不生成hibernate.cfg.xml文件,所以出现not found问题

解决方法:

我的目录是:src/main/resource和上面目录比较最后少一个“s“所以编译时出错。

一、手动把”src/main/resource“目录改成:”src/main/resources“

二、在pom.xml中”project“标签下面加

	<build>
		<resources>
			<resource>
				<directory>src/main/resource</directory>
				<filtering>false</filtering>
			</resource>
		</resources>
	</build>
注意”resource“这个是否和项目保持一致。

org.hibernate.HibernateException:Could not parse configuration/hibernate.cfg.xml

05-14

初学hibernate配置文件都是自己写的rn下面是错误信息:rn[color=#FF0000]org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xmlrn at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2216)rn at org.hibernate.cfg.Configuration.configure(Configuration.java:2128)rn at ch03.hibernate.Test.main(Test.java:11)rnCaused by: org.dom4j.DocumentException: Error on line 2 of document : 文档中根元素前面的标记必须格式正确。 Nested exception: 文档中根元素前面的标记必须格式正确。rn at org.dom4j.io.SAXReader.read(SAXReader.java:482)rn at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2208)rn ... 2 more[/color]rn下面是我运行的代码:rnpackage ch03.hibernate;rnimport org.hibernate.*;rnimport org.hibernate.cfg.*;rnpublic class Test rnrn /**rn * @param argsrn */rn public static void main(String[] args) rn tryrn SessionFactory sf=new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();rn rn Session session=sf.openSession();rn rn Transaction tx=session.beginTransaction();rn User user=new User();rn user.setUsername("Hibernate");rn user.setPassword("123");rn session.save(user);rn tx.commit();rn session.close();rn catch(HibernateException e)rn e.printStackTrace();rn rnrn rnrnrn下面是配置文件hibernate.cfg.xmlrnrnrnrnrn rnrn rn com.mysql.jdbc.Driverrn jdbc:mysql://localhost:3306/myprojectrn rn rootrn 0000rnrn rn 1rnrn myproject_rn org.hibernate.dialect.MySQLDialectrnrn threadrnrn org.hibernate.cache.NoCacheProviderrnrn truern rn creatern rn rn rn rnrnrn下面是配置文件User.hbm.xmlrnrnrnrnrnrnrnrnrnrnrnrnrnrn下面是类Userrnpackage ch03.hibernate;rnrnpublic class User rn private int id;rn private String username;rn private String password;rn private String email;rn public int getId() rn return id;rn rn public void setId(int id) rn this.id = id;rn rn public String getUsername() rn return username;rn rn public void setUsername(String username) rn this.username = username;rn rn public String getPassword() rn return password;rn rn public void setPassword(String password) rn this.password = password;rn rn public String getEmail() rn return email;rn rn public void setEmail(String email) rn this.email = email;rn rnrnrnch03.hibernate和hibernate.cfg.xml都在src目录下,User.hbm.xml在ch03.hibernate目录下rn添加的用户库是hibernate-distribution-3.6.0.Finalrn折腾了好久,终没解决问题。希望有经验或看出猫腻的大侠指点。

org.hibernate.HibernateException: No Session found for current thread

12-24

rn hibernate:4.1.6 spring:4.0.1 rn  rn 工程启动没错,进入getAllUser页面获得数据库用户信息时报如下错误: rn  rn 严重: Servlet.service() for servlet springMVC threw exceptionrn org.hibernate.HibernateException: No Session found for current threadrn at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)rn at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941)rn at com.mlf.web.controller.dao.UserDAO.getAllUser(UserDAO.java:27)rn at com.mlf.web.controller.service.UserManager.getAllUser(UserManager.java:28)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)rn at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)rn at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)rn at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)rn at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)rn at $Proxy19.getAllUser(Unknown Source)rn at com.mlf.web.controller.UserController.getAllUser(UserController.java:36)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)rn at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)rn at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)rn at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)rn at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)rn at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)rn at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)rn at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)rn at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)rn at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)rn at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:149)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)rn at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)rn at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)rn at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760)rn at java.lang.Thread.run(Thread.java:619)rn -----------------------------------------------------------------------------------------------------------------------------------------rn hibernate.cfg.test.xml(com.mlf.web.controller.hibernate)rn  rnrn  -----------------------------------------------------------------------------------------------------------------------------------------rn  rn applicationContext.xml(com.mlf.web.controller.spring)rn  rnrn   -----------------------------------------------------------------------------------------------------------------------------------------rn  rn spring-annotation.xml(包:config)rn  rnrn    -----------------------------------------------------------------------------------------------------------------------------------------rn  rn spring-core.xml(包:config)rn  rnrn     -----------------------------------------------------------------------------------------------------------------------------------------rn  rn spring-hibernate.xml(包:config)rn  rnrn      -----------------------------------------------------------------------------------------------------------------------------------------rn  rn UserController.java(包:com.mlf.web.controller)rn  rnrn  rn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试