Receiving the org.hibernate.TypeMismatchException Exception [closed]

依然范特西╮ 提交于 2019-12-02 15:06:46

Your error is

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

In your stacktrace we can see that dbd.create call others methods

    at com.my.dao.DepartementImplDB.getDepartementByNom(DepartementImplDB.java:54)
at com.my.dao.DepartementImplDB.addDepartement(DepartementImplDB.java:29)

In your method

public Departement getDepartementByNom(String nomDepartement)

You try to load a departement with a name but you use session.load wich load by Id.

 Departement dept = (Departement) session.load(Departement.class, nomDepartement);

You have to make a Criteria request to load by name. Like

session.beginTransaction();
Criteria criteria = session.createCriteria(Departement.class);
criteria.add(Restrictions.eq("nomDepartement", nomDepartement).ignoreCase());

 result = (Departement) criteria.uniqueResult();
 session.getTransaction().commit();

ps: In getDepartementById and getDepartementByNom you never end your transaction. Please use pattern like:

 try {
        session.beginTransaction();

        // your code

        session.getTransaction().commit();
    } catch (HibernateException e) {
        LOGGER.error(e);
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        }
    }

Exception:-
Provided id of the wrong type. Expected: class java.lang.Integer, got class java.lang.String

Its very clear by seeing the above line, hibernate is expecting the id as an integer but receiving id in the String format. So it's throwing :-
org.hibernate.TypeMismatchException

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!