getHibernateTemplate().find("from ReplyUser r where r.username=?",new Object[] { userName });
方法里面userName的值始终传不到HQL语句中
然而使用不传递userName的值
getHibernateTemplate().find(from ReplyUser r where r.username=’user‘");
却能正常的查找出来库中的数据这是怎么回事啊,该怎么办
下面是DAO的实现类的代码:public ReplyUser getUser(String userName) {
                List users = getHibernateTemplate().find(HQL_GET_USER_BY_USRNAME,
new Object[] { userName });
return (ReplyUser) users.get(0);
}
其中的userName已经被赋值了但是传不进HQL中userName在表中不是主键

解决方案 »

  1.   

    个人认为"findByNamedQueryAndNamedParam"比较好用些HQL:
    <query name="find.permission.by.level"><![CDATA[
                FROM Permission p
                where p.level = :level
            ]]></query>public List<Permission> finPermissionByPermissionLevel(int level) 
    throws ObjectNotFoundException {
    return getHibernateTemplate().findByNamedQueryAndNamedParam(
    "find.permission.by.level", "level", level); 
    }
    其中'find.permission.by.level'是映射文件中的HQL名,第一个“level“是变量名,也就是HQL中的":level",地三个level是需要导入的变量值。
      

  2.   

    代码中的HQL_GET_USER_BY_USRNAME是
    private static final String HQL_GET_USER_BY_USRNAME = "from ReplyUser r where r.username=?";
      

  3.   


    public List<Permission> finPermissionByPermissionLevel(int level) 
            throws ObjectNotFoundException {
            return getHibernateTemplate().findByNamedQueryAndNamedParam(
                    "find.permission.by.level", "level", level); 
        }
    的话是不是只能传int的值要是我要传别的类型的值该怎么做
      

  4.   

    什么值都能传!单个数值不用说了,和上面例子中传int是一样的,任何object都可以,getHibernateTemplate().findByNamedQueryAndNamedParam 返回的是一个List<Object>。
    如果是多个数值可以这样:
    public List<Permission> findPermissionByDateAndLevel(Date date, int level) {

    return getHibernateTemplate().
    findByNamedQueryAndNamedParam("find.permission.by.date.and.level", 
    new String[] {"theDate", "permissionLevel"}, 
    new Object[] {date, new Integer(level)} ); 
    }
    根据这个例子可以看出,可以传入的数值甚至不用是同一数据类型。在这个例子中,HQL当然应该要求两个数值的输入,其中的变量分别命名为 ":theDate", ":permissionLevel"
      

  5.   

    谁知道为什么传值传不到HQL中,findPermissionByDateAndLevel也是同样的问题也传不进去
      

  6.   

    如果还是传不进去,那可能就是你的Hibernate设置有问题。如果可以的话把映射文件发上来看看。如果用Spring的话最好把applicationContext.xml也发上来看看。
      

  7.   

    映射文件是通过自动生成的,应该不能够改什么了吧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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
    <value>jdbc:oracle:thin:@neusoft:1521:MYORCL</value>
    </property>
    <property name="username">
    <value>reply</value>
    </property>
    <property name="password">
    <value>123456</value>
    </property>
    </bean> <!-- 配置SessionFactory -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <!-- 配置Hibernate的属性 -->
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.Oracle9Dialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <!-- 指定HIbernate映射文件的路径 -->
    <property name="mappingResources">
    <list>
    <value>/org/neo/reply/model/ReplyUser.hbm.xml</value>
    <value>/org/neo/reply/model/ReplyTeacher.hbm.xml</value>
    <value>/org/neo/reply/model/ReplyStudent.hbm.xml</value>
    <value>/org/neo/reply/model/ReplyLession.hbm.xml</value>
    <value>
    /org/neo/reply/model/ReplyDepartment.hbm.xml
    </value>
    <value>/org/neo/reply/model/ReplyClass.hbm.xml</value>
    <value>/org/neo/reply/model/ReplyAdmin.hbm.xml</value>
    </list>
    </property>
    </bean> <!-- USERDAO -->
    <bean id="replyUserDao"
    class="org.neo.reply.dao.hibernate.ReplyUserDaoHibernate">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean> <bean id="replyUserManager"
    class="org.neo.reply.service.impl.ReplyUserManagerImpl">
    <property name="replyUserDao">
    <ref bean="replyUserDao" />
    </property>
    </bean> <!-- TEACHERDAO -->
    <bean id="teacherDao"
    class="org.neo.reply.dao.hibernate.TeacherDaoHibernate">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean> <!-- STUDENTDAO -->
    <bean id="studentDao"
    class="org.neo.reply.dao.hibernate.StudentDaoHibernate">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="studentManager"
    class="org.neo.reply.service.impl.StudnetManagerImpl">
    <property name="studentDao">
    <ref bean="studentDao" />
    </property>
    </bean> <!-- UserBean  -->
    <bean id="loginViewBean"
    class="org.neo.reply.action.ReplyUserAction" scope="prototype">
    <property name="replyUserManager">
    <ref bean="replyUserManager" />
    </property>
    </bean> <bean id="getUser" class="org.neo.reply.action.ReplyUserAction"
    scope="prototype">
    <property name="replyUserManager">
    <ref bean="replyUserManager" />
    </property>
    </bean> <!-- STUDENTBEAN -->
    <bean id="getStudent" class="org.neo.reply.action.StudentAction"
    scope="prototype">
    <property name="studentManager">
    <ref bean="studentManager" />
    </property>
    </bean>
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    </bean> <!-- 配置事务特性,配置add,delete和update开始的方法, 事务传播特性为required-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="get*" propagation="REQUIRED" />
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="*" read-only="true" />
    </tx:attributes>
    </tx:advice> <!-- 配置那些类的方法进行事务管理,当前org.neo.reply.service包中的子包,类中所有方法需要,还需要参考tx:advice的配置 -->
    <aop:config>
    <aop:pointcut id="allManagerMethod"
    expression="execution(* org.neo.reply.service.*.*(..))" />
    <aop:advisor advice-ref="txAdvice"
    pointcut-ref="allManagerMethod" />
    </aop:config>
    </beans>
    struts.xml<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
    <!-- 将action托管给spring -->
    <constant name="struts.objectFactory" value="spring" />
    <package name="reply-default" extends="struts-default"> <action name="login_view" class="loginViewBean"
    method="loginView">
    <result type="redirect">login_view.jsp</result>
    </action> <action name="login" class="getUser" method="login">
    <result name="admin">admin/list_admin.jsp</result>
    <result name="teacher">teacher/list_teacher.jsp</result>
    <result name="student">student.action</result>
    <result name="register">register.jsp</result>
    </action>
    </package>
    <include file="struts/reply-admin.xml" />
    <include file="struts/reply-teacher.xml" />
    <include file="struts/reply-student.xml" /></struts>
      

  8.   

    我创建的是多表连接的产生了很多标的配置属性
    标的配置没有改变过REPLY_USER表的配置文件
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="org.neo.reply.model.ReplyUser" table="REPLY_USER" schema="REPLY">
            <id name="userid" type="java.lang.Long">
                <column name="USERID" precision="6" scale="0" />
                <generator class="increment"></generator>
            </id>
            <property name="username" type="java.lang.String">
                <column name="USERNAME" length="20" not-null="true" unique="true" />
            </property>
            <property name="userpassword" type="java.lang.String">
                <column name="USERPASSWORD" length="20" not-null="true" />
            </property>
            <property name="competence" type="java.lang.Long">
                <column name="COMPETENCE" precision="1" scale="0" not-null="true" />
            </property>
            <set name="replyTeachers" inverse="true">
                <key>
                    <column name="USERID" precision="6" scale="0" />
                </key>
                <one-to-many class="org.neo.reply.model.ReplyTeacher" />
            </set>
            <set name="replyStudents" inverse="true">
                <key>
                    <column name="USERID" precision="6" scale="0" />
                </key>
                <one-to-many class="org.neo.reply.model.ReplyStudent" />
            </set>
            <set name="replyAdmins" inverse="true">
                <key>
                    <column name="USERID" precision="6" scale="0" />
                </key>
                <one-to-many class="org.neo.reply.model.ReplyAdmin" />
            </set>
        </class>
    </hibernate-mapping>配置文件生成的代码
    package org.neo.reply.model;import java.util.HashSet;
    import java.util.Set;/**
     * ReplyUser entity.
     * 
     * @author MyEclipse Persistence Tools
     */public class ReplyUser implements java.io.Serializable { // Fields /**
     * 
     */
    private static final long serialVersionUID = 6658430530569766240L;
    private Long userid;
    private String username;
    private String userpassword;
    private Long competence;
    private Set replyTeachers = new HashSet(0);
    private Set replyStudents = new HashSet(0);
    private Set replyAdmins = new HashSet(0); // Constructors /** default constructor */
    public ReplyUser() {
    } /** minimal constructor */
    public ReplyUser(String username, String userpassword, Long competence) {
    this.username = username;
    this.userpassword = userpassword;
    this.competence = competence;
    } /** full constructor */
    public ReplyUser(String username, String userpassword, Long competence,
    Set replyTeachers, Set replyStudents, Set replyAdmins) {
    this.username = username;
    this.userpassword = userpassword;
    this.competence = competence;
    this.replyTeachers = replyTeachers;
    this.replyStudents = replyStudents;
    this.replyAdmins = replyAdmins;
    } // Property accessors public Long getUserid() {
    return this.userid;
    } public void setUserid(Long userid) {
    this.userid = userid;
    } public String getUsername() {
    return this.username;
    } public void setUsername(String username) {
    this.username = username;
    } public String getUserpassword() {
    return this.userpassword;
    } public void setUserpassword(String userpassword) {
    this.userpassword = userpassword;
    } public Long getCompetence() {
    return this.competence;
    } public void setCompetence(Long competence) {
    this.competence = competence;
    } public Set getReplyTeachers() {
    return this.replyTeachers;
    } public void setReplyTeachers(Set replyTeachers) {
    this.replyTeachers = replyTeachers;
    } public Set getReplyStudents() {
    return this.replyStudents;
    } public void setReplyStudents(Set replyStudents) {
    this.replyStudents = replyStudents;
    } public Set getReplyAdmins() {
    return this.replyAdmins;
    } public void setReplyAdmins(Set replyAdmins) {
    this.replyAdmins = replyAdmins;
    }
      

  9.   

    这是我建表的语句
    /数据库新建用户,用户通过normal的型式登录/
    create user reply identified by "123456"
    default tablespace users quota 10M on users account lock;grant create session,create table,create view to reply;
    /开始建的表/
    create table REPLY_USER(
    USERID number(6) constraint s_pk primary key,
    USERNAME char(20) not null,
    USERPASSWORD char(20) not null,
    COMPETENCE number(1) not null
    )alter table reply_user add constraint P_UK unique(username)create sequence SEQ_REPLY_USER; insert into REPLY_USER values(seq_REPLY_USER.nextval,'admin1',111,1);
    create table REPLY_ADMIN(
    USERID number(6) references reply_user(USERID),
    ADMINNAME char(20) not null
    )CREATE TABLE REPLY_TEACHER(
    USERID number(6) references reply_user(USERID),
    TEACHERNAME CHAR(20) NOT NULL,
    TEACHERSEX CHAR(2),
    TEACHERBIRTHDAY DATE,
    TEACHERWORKDAY DATE,
    TEACHERDEPARTMENTID number(6) primary key   //系别号
    );CREATE TABLE REPLY_DEPARTMENT(
    DEPARTMENTID number(6) references REPLY_TEACHER(TEACHERDEPARTMENTID),//系别号
    DEPARTMENTNAME CHAR(20) UNIQUE    //系名                   
    )CREATE TABLE REPLY_STUDENT(
    USERID number(6) references reply_user(USERID),
    STUDENTNUMBER number(6) primary key,
    NAME CHAR(20) NOT NULL,
    SEX CHAR(2),
    BIRTHDAY DATE,
    STUDENTDAY DATE,
    CLASSID NUMBER(6) ,
    REPLAYNUMBER NUMBER(6),
    TEACHERCOMMENT char,               //教师评语
    TEACHERID number(6)                //教师用户名ID
    )CREATE TABLE REPLY_CLASS(
    CLASSID NUMBER(6),                  //班级ID
    CLASSNAME CHAR(20) unique,          //班级名称
    DEPARTMENTID NUMBER(6) NOT NULL       //系别号
    )CREATE TABLE REPLY_LESSION(
    LESSIONID number(6) primary key,
    LESSIONNAME CHAR(20) unique                 
    )CREATE TABLE REPLY_RESULT(
    STUDENTNUMBER number(6) REFERENCES REPLY_STUDENT(STUDENTNUMBER),
    LESSIONID number(6) REFERENCES REPLY_LESSION(LESSIONID),
    RESULTNUMBER NUMBER(6)
    )
      

  10.   

    getHibernateTemplate().find("from ReplyUser r where r.username=?", userName );
      

  11.   

    getHibernateTemplate().find("from ReplyUser r where r.username=?", userName );
    也没用还是同样的问题
      

  12.   

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- spring params -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param> <!-- spring的监听器,以便在启动时就自动加载spring的配置 -->
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>

    <!-- OpenSessionInViewFilter过滤器 -->
    <filter>
    <filter-name>lazyLoadingFilter</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    </filter> <!-- struts2 过滤器 -->
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    </filter> <filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping> <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping> <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    </web-app>