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 class="uuid.hex" />
    </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 c
	where (: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 failed
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'dictService' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\l
iany\mis\common\dict\mis-spring-dict.xml]: Cannot resolve reference to bean 'dic
tServiceImpl' while setting bean property 'target'; nested exception is org.spri
ngframework.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' de
fined in file [D:\projects2\mis\war\WEB-INF\classes\com\liany\mis\common\dict\mi
s-spring-dict.xml]: Cannot resolve reference to bean 'sessionFactory' while sett
ing bean property 'sessionFactory'; nested exception is org.springframework.bean
s.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.D
uplicateMappingException: Duplicate query mapping checkIsDuplicate
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'dictServiceImpl' defined in file [D:\projects2\mis\war\WEB-INF\classes\c
om\liany\mis\common\dict\mis-spring-dict.xml]: Cannot resolve reference to bean
'dictDAO' while setting bean property 'dictDAO'; nested exception is org.springf
ramework.beans.factory.BeanCreationException: Error creating bean with name 'dic
tDAO' 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' wh
ile setting bean property 'sessionFactory'; nested exception is org.springframew
ork.beans.factory.BeanCreationException: Error creating bean with name 'sessionF
actory' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\toone\ipms\ipm
s-spring-global.xml]: Initialization of bean failed; nested exception is org.hib
ernate.DuplicateMappingException: Duplicate query mapping checkIsDuplicate
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h 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 'session
Factory' while setting bean property 'sessionFactory'; nested exception is org.s
pringframework.beans.factory.BeanCreationException: Error creating bean with nam
e 'sessionFactory' defined in file [D:\projects2\mis\war\WEB-INF\classes\com\too
ne\ipms\ipms-spring-global.xml]: Initialization of bean failed; nested exception
 is org.hibernate.DuplicateMappingException: Duplicate query mapping checkIsDupl
icate
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'sessionFactory' defined in file [D:\projects2\mis\war\WEB-INF\classes\co
m\toone\ipms\ipms-spring-global.xml]: Initialization of bean failed; nested exce
ption is org.hibernate.DuplicateMappingException: Duplicate query mapping checkI
sDuplicate
org.hibernate.DuplicateMappingException: Duplicate query mapping checkIsDuplicat
e
        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.afterPrope
rtiesSet(LocalSessionFactoryBean.java:683)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1099)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.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.AbstractAutowireCapableBean
Factory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1054)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.populateBean(AbstractAutowireCapableBeanFactory.java:863)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.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.AbstractAutowireCapableBean
Factory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1054)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.populateBean(AbstractAutowireCapableBeanFactory.java:863)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.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.AbstractAutowireCapableBean
Factory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1054)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.populateBean(AbstractAutowireCapableBeanFactory.java:863)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.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.refres
h(AbstractApplicationContext.java:321)
        at org.springframework.web.context.support.AbstractRefreshableWebApplica
tionContext.refresh(AbstractRefreshableWebApplicationContext.java:139)
        at org.springframework.web.context.ContextLoader.createWebApplicationCon
text(ContextLoader.java:252)
        at org.springframework.web.context.ContextLoader.initWebApplicationConte
xt(ContextLoader.java:190)
        at org.springframework.web.context.ContextLoaderListener.contextInitiali
zed(ContextLoaderListener.java:49)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
t.java:3934)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
429)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:77
1)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)

        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.ja
va:630)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.j
ava: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(Lifecycl
eSupport.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:5
16)
        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(DelegatingMethodAcces
sorImpl.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 listenerStart
2011-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声明:

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









你可能感兴趣的:(Hibernate)