Spring + Hibernate,嵌套异常是org.hibernate.QueryParameterException:超出声明的ordinal参数数量的位置

时间:2011-12-12 12:16:48

标签: hibernate spring

此代码无效:

getHibernateTemplate()
                .find("from elephant.model.Setup s where s.settingName = ?",setupName);

错误:位置超出声明的序数参数的数量。请记住,序数参数是基于1的!位置:1;嵌套异常是org.hibernate.QueryParameterException:超出声明的ordinal参数数量的位置。请记住,序数参数是基于1的!位置:1

如果我将代码更改为:

getHibernateTemplate()
                .find("from elephant.model.Setup s where s.settingName = :setupName",setupName);

我有同样的错误。我怎么解决它?

...后

我会改变这样的查询:

.find("from elephant.model.Setup s where s.settingName = \"AutoAccessControl\"")

现在我们还有其他错误:

2563 [main] WARN org.hibernate.hql.QuerySplitter - no persistent classes found for query class: from elephant.model.Setup s where s.settingName = "AutoAccessControl"

我的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/hibernate.cfg.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Office -->
    <bean id="OfficeDao" class="elephant.dao.impl.OfficeDao">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="OfficeService" class="elephant.service.impl.OfficeService">
        <property name="dao" ref="OfficeDao" />
    </bean>
    <!-- Office -->
    <!-- XMLFile -->
    <bean id="XMLFileDao" class="elephant.dao.impl.XMLFileDao">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="XMLFileService" class="elephant.service.impl.XMLFileService">
        <property name="dao" ref="XMLFileDao" />
    </bean>
    <!-- XMLFile -->
    <!-- AppointmentCategory -->
    <bean id="AppointmentCategoryDao" class="elephant.dao.impl.AppointmentCategoryDao">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="AppointmentCategoryService" class="elephant.service.impl.AppointmentCategoryService">
        <property name="dao" ref="AppointmentCategoryDao" />
    </bean>
    <!-- AppointmentCategory -->
    <!-- Setup -->
    <bean id="SetupDao" class="elephant.dao.impl.SetupDao">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="SetupService" class="elephant.service.impl.SetupService">
        <property name="dao" ref="SetupDao" />
    </bean>
    <!-- Setup -->
</beans>

我的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.use_query_cache">false</property>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

        <property name="hibernate.transaction.flush_before_completion">true</property>
        <property name="hibernate.transaction.auto_close_session">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.use_sql_comments">true</property>
        <property name="hibernate.connection.charSet">true</property>

    </session-factory>
</hibernate-configuration>

我的jdbc.properties

jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/hd_test
jdbc.username=musr
jdbc.password=mpwd
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

1 个答案:

答案 0 :(得分:0)

您确定此查询是否失败 - 您是否可以尝试连接查询中的值以进行检查。

第一个版本应该有效 - 第二个版本不行 - 你需要使用findByNamedParam。