在Spring Hibernate中使用Session方法使用findByCriteria

时间:2019-07-18 09:37:29

标签: spring hibernate

@SuppressWarnings("unchecked")
    @Override
    public StudentCredential getStudentDetailsByUnameAndPassword(String uname,String password){
        Session session;
        DetachedCriteria detachedCriteria =  DetachedCriteria.forClass(StudentCredential.class);
        detachedCriteria.add(Restrictions.eq("uname", uname));
        detachedCriteria.add(Restrictions.eq("password", password));
        List<StudentCredential> findByCriteria = (List<StudentCredential>)Hibernate template .findByCriteria(detachedCriteria);
        if(findByCriteria !=null && findByCriteria.size()>0)
        return findByCriteria.get(0);
        else
            return null;
    }

我有此代码,并想使用HIbernateTemplate的findByCriteria对数据进行评估现在,我该如何使用会话条件查询或任何其他方法代替Hibernate Template来验证电子邮件和密码 此代码有助于保存验证数据库中的电子邮件和密码。

package com.infotech.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.infotech.dao.StudentCredentialDao;
import com.infotech.model.Student;
import com.infotech.model.StudentCredential;

@Repository("StudentCredentialDAO")
public class StudentCredentialDaoImpl implements StudentCredentialDao {

    @Autowired
    private SessionFactory sessionFactory;


    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public boolean saveStudentCred(StudentCredential stuCred) {
        Session session = this.sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(stuCred);
        tx.commit();
        session.close();
         return true;
    }

现在保存代码后使用Hibernate模板,但这对我不起作用,我可以做什么来验证代码

@SuppressWarnings("unchecked")
        @Override
        public StudentCredential getStudentDetailsByUnameAndPassword(String uname,String password){
            DetachedCriteria detachedCriteria =  DetachedCriteria.forClass(StudentCredential.class);
            detachedCriteria.add(Restrictions.eq("uname", uname));
            detachedCriteria.add(Restrictions.eq("password", password));
            List<StudentCredential> findByCriteria = (List<StudentCredential>)session.getIdentifier(detachedCriteria);
            if(findByCriteria !=null && Hibernate template .size()>0)
            return findByCriteria.get(0);
            else
                return null;
        }
    }

这是我的DAO文件的完整代码,我不想使用Hibernate Template,因为它与Oracle数据库有关。因此,使用会话。

0 个答案:

没有答案