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

Hql技能积累之二:hbm.xml文件中经常犯的错

2013-09-11 
Hql技巧积累之二:hbm.xml文件中经常犯的错Hibernate使用了*.hbm.xml来进行数据库表与java中的实体类的映射

Hql技巧积累之二:hbm.xml文件中经常犯的错
Hibernate使用了*.hbm.xml来进行数据库表与java中的实体类的映射,例如:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    <hibernate-mapping><class name="com.liany.mis.perm.orgchart.model.OrgChart" table="ORG_CHART">    <id name="id" type="java.lang.String" column="ID"><generator />    </id>   <property        name="orgCode"        type="java.lang.String"        column="ORGCODE"        length="50"    />   <property        name="orgName"        type="java.lang.String"        column="ORGNAME"        length="200"    /></class><query name="getAllOrgCharts">from OrgChart c order by c.treeCode</query><query name="checkIsDuplicate"><![CDATA[select count(c.id) from OrgChart cwhere (:id is null or c.id<>:id)and c.orgCode=:orgCode]]></query></hibernate-mapping>




用<query>元素来声明hql语句,这外query中的name是全局性的,一旦别的query的name属性与之重名,tomcat服务器在启动时就会报错,这个错误是比较难发现的。 本人犯错的例子:

2011-05-15 15:02:13 ERROR ContextLoader:211 - Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dictService' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\liany\mis\common\dict\mis-spring-dict.xml]: Cannot resolve reference to bean 'dictServiceImpl' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dictServiceImpl' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\liany\mis\common\dict\mis-spring-dict.xml]: Cannot resolve reference to bean 'dictDAO' while setting bean property 'dictDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dictDAO' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\liany\mis\common\dict\mis-spring-dict.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory'defined in file [D:\projects2\mis\war\WEB-INF\classes\com\toone\ipms\ipms-spring-global.xml]: Initialization of bean failed; nested exception is org.hibernate.DuplicateMappingException: Duplicate query mapping checkIsDuplicateorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dictServiceImpl' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\liany\mis\common\dict\mis-spring-dict.xml]: Cannot resolve reference to bean'dictDAO' while setting bean property 'dictDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dictDAO' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\liany\mis\common\dict\mis-spring-dict.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\toone\ipms\ipms-spring-global.xml]: Initialization of bean failed; nested exception is org.hibernate.DuplicateMappingException: Duplicate query mapping checkIsDuplicateorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dictDAO' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\liany\mis\common\dict\mis-spring-dict.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\toone\ipms\ipms-spring-global.xml]: Initialization of bean failed; nested exception is org.hibernate.DuplicateMappingException: Duplicate query mapping checkIsDuplicateorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\toone\ipms\ipms-spring-global.xml]: Initialization of bean failed; nested exception is org.hibernate.DuplicateMappingException: Duplicate query mapping checkIsDuplicateorg.hibernate.DuplicateMappingException: Duplicate query mapping checkIsDuplicate        at org.hibernate.cfg.Mappings.checkQueryExist(Mappings.java:270)        at org.hibernate.cfg.Mappings.addQuery(Mappings.java:259)        at org.hibernate.cfg.HbmBinder.bindNamedQuery(HbmBinder.java:2596)        at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:160)        at org.hibernate.cfg.Configuration.add(Configuration.java:688)        at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:523)        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:683)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1099)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:400)        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:234)        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:144)        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:182)        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:105)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1054)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:863)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:382)        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:234)        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:144)        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:182)        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:105)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1054)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:863)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:382)        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:234)        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:144)        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:182)        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:105)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1054)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:863)        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:382)        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:234)        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:144)        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:277)        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:321)        at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139)        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:252)        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:190)        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3934)        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4429)        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:630)        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:556)        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:491)        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1206)        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:314)        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)        at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)        at org.apache.catalina.core.StandardService.start(StandardService.java:516)        at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)        at org.apache.catalina.startup.Catalina.start(Catalina.java:583)        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)        at java.lang.reflect.Method.invoke(Method.java:597)        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)2011-5-15 15:02:13 org.apache.catalina.core.StandardContext start严重: Error listenerStart2011-5-15 15:02:13 org.apache.catalina.core.StandardContext start



其中之一很多错误产生的stacktrace信息,基本是误导人的, 某段的最后一句才是有用的:
nested exce
ption is org.hibernate.DuplicateMappingException: Duplicate query mapping checkI
sDuplicate


因为这个,花了不少时间查问题, 所以有必要花点时间写这篇文字来记录一下,以免将来还陷进去。

查了一下,果然有多个同名的query声明:












我的异常网推荐解决方案:org.springframework.beans.factory.BeanCreationException:,http://www.myexception.cn/j2ee/10759.html

热点排行