Spring Hibernate Integration命名查询问题

时间:2015-12-04 08:47:57

标签: java spring hibernate spring-mvc

我是Spring的新手。我坚持以下问题。请帮帮我。 这是我的实体类(使用Hibernate):

package com.demo.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.NamedQuery;


@Entity
@Table(name = "myuser")
@NamedQuery(name="getPassword",query="SELECT login from Login login WHERE login.userName=:username")
public class Login {
    @Id
    @GeneratedValue
    @Column(name = "username")
    private String userName;

    @Column(name = "password")
    private String password;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

这是我的DAO。

package com.demo.dao;

import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.demo.model.Login;

public class LoginDAO {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }
    public boolean validateLogin(Login login){
        System.out.println("login object");
        String userName = login.getUserName();
        String password = login.getPassword();

        System.out.println("user name is" + userName);
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Query query1 = session.getNamedQuery("getPassword");
        query1.setString("username", userName);

        ArrayList<Login> loginList = (ArrayList)query1.list();
        System.out.println("list is" + loginList);

        String pwdfrmdb = null;
        for(Login l:loginList){
            System.out.println("Inside loop");
            pwdfrmdb = l.getPassword();
        }
        System.out.println("password from database" + pwdfrmdb + "password entered" + password);
        if(password.equals(pwdfrmdb)){
            return true;
        }
        return false;
    }
}

this is my configuration.

        <context:component-scan base-package="com.demo"/>
        <!-- Define Datasource -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
            <property name="username" value="xxx"/>
            <property name="password" value="yyy"/>
        </bean>

        <!-- Define SessionFactory -->
        <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect
                    </prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
            <property name="annotatedClasses">
                <list>
                      <value>com.demo.model.Login</value>
                 </list>
        </property>
        </bean>


        <bean id="logindao" class="com.demo.dao.LoginDAO">
            <property name="sessionFactory" ref="hibernateSessionFactory"/>
        </bean>

        <bean id="login" class="com.demo.model.Login"/>

我的DAO无法获取密码值。即使我为命名查询提供了用户名的值,它显示为?。请让我知道我做错了什么。

这是输出:

before validation
login object
user name is a
Hibernate: select login0_.username as username1_0_, login0_.password as password2_0_ from myuser login0_ where login0_.username=?
list is[]
password from databasenullpassword enteredb

1 个答案:

答案 0 :(得分:0)

对于使用命名参数时的命名查询,请尝试按以下方式设置参数:

Query query1 = session.getNamedQuery("getPassword");
query1.setParameter("username", userName);

这应该有帮助