Hibernate:如何通过参数获取预定义对象传递String值

时间:2016-09-15 09:37:07

标签: java hibernate

我使用以下方法获取预定义对象。

public Patient getUserNameAndPassword(String username, Session session) {
        Patient patient=(Patient)session.get(Patient.class,username);
        return patient;       
}

执行此方法后,生成以下异常。

org.hibernate.TypeMismatchException: Provided id of the wrong type for class beans.Patient. Expected: class java.lang.Integer, got class java.lang.String

PatientService.java

public class PatientService {

    private static PatientDAOInterface patientDAOInterface;

    public PatientService() {
        patientDAOInterface = new PatientDAOImpl();
    }

    public Session getSession() {
        Session session = patientDAOInterface.openCurrentSession();
        return session;
    }

    public Transaction getTransaction(Session session) {
        return patientDAOInterface.openTransaction(session);
    }

    public Patient getUserNameAndPassword(String username){
        Session session = patientDAOInterface.openCurrentSession();
        Transaction transaction = null;

        Patient patient=new Patient();
        try{
            transaction = patientDAOInterface.openTransaction(session);
            patient=patientDAOInterface.getUserNameAndPassword(username, session);
            transaction.commit();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            session.close();
        }        
        return patient; 
    }
}

PatientDAOInterface .java

public interface PatientDAOInterface 
{
    public Patient getUserNameAndPassword(String username,Session session);
    public Session openCurrentSession();
    public Transaction openTransaction(Session session);
}

PatientDAOImpl.java

public class PatientDAOImpl implements PatientDAOInterface {
    @Override
    public Patient getUserNameAndPassword(String username, Session session) {
        Patient patient=(Patient)session.get(Patient.class,username);
        return patient;       
    }    

    private static final SessionFactoryBuilder sessionFactoryBuilder = SessionFactoryBuilder.getInstance();

    @Override
    public Session openCurrentSession() {

         Session currentSession =   sessionFactoryBuilder.getSessionFactory().openSession();
    return currentSession;
    }

    @Override
    public Transaction openTransaction(Session session) {

        Transaction beginTransaction = session.beginTransaction();
        return beginTransaction;
    }

}

我已经提到了上面的作品。

实际上,我想通过参数传递一个String并获取一个Patient对象。 我熟悉传递Integer而不是String。但我对此一无所知。

有任何想法吗?

2 个答案:

答案 0 :(得分:3)

您想通过用户名找到病人吗?因为在这里你试图通过他的ID找到他并且你传递一个表示用户名的字符串。您应该编写自己的查询/标准,以便通过用户名获取患者。像这样:

Criteria criteria = session.createCriteria(Patient.class);
criteria.add(Restrictions.eq("username", username);
List<Patient> patients = criteria.list();

或查询版本:

String hql = "FROM Patient p WHERE p.username = "+username;
Query query = session.createQuery(hql);
List patients = query.list();

答案 1 :(得分:1)

我找到了问题的答案。

public Patient getUserNameAndPassword(String username, Session session) {
        Query query=session.createQuery("from Patient where user_name= :username");
        query.setParameter("username", username);
        List list = query.list();
        Patient patient=(Patient) list.get(0);
        return patient;
}

这对我有用。

感谢。