Search code examples
hibernatespring-data-jpahibernate-mappinghibernate-criteria

Hibernate custom class mapping


When I fech users using this code, it works as desired.

 public List<Users> getActiveUsers(User user) {
    EntityManager entityManager = getEntityManager();
   List<User> Users = new ArrayList();
    try {
        users = entityManager.createQuery("select e from User e where  e.deleted = :deleted", User.class)
                .setParameter("deleted", false)                 
                .getResultList();
    } finally {
        entityManager.close();
    }
    return users;
}

But if I try to get only one user using th following code, it fails.

User user = (User) entityManager.createQuery("select e from User e where  e.deleted = :deleted")
                .setParameter("deleted", false)+                 
                .getSingleResult();

I can't get a mapped user, the excepction is: "can't convert com.project.model.User to com.project.model.User"


Solution

  • Apparently there was nothing wrong with the code. What I found out to be the problem was the same as this guy:

    A classloader proplem related to spring-boot-devtools

    however, the .properties file with the dozer reference did not work for me. I managed to get my code up and running by removing spring-devtools from the project. Guess I can live without live reload!