0

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"

  • 1
    Can you try using `entityManager.createQuery(query, User.class)`? Where do you deploy this? – Arturo Volpe May 12 '17 at 12:12
  • I have tried it, same result. Where I deploy? Not sure if I understand, it is in localhost and the method is being called in a userRepositoryCustom class – Marcel Caferati May 12 '17 at 12:18
  • 1
    What application server are you using, this seems a problem related to a classloader. Also try adding to your query "setMaxResults(1)". – Arturo Volpe May 12 '17 at 12:22
  • I am using Glassfish – Marcel Caferati May 12 '17 at 12:23
  • 1
    Can you add the log? – Arturo Volpe May 12 '17 at 12:29
  • This is the error I get: `Can not pass an instance of "class com.project.model.User (loaded by instance of sun.misc.Launcher$AppClassLoader(id=8597))" to an instance of "class com.project.model.User (loaded by instance of org.springframework.boot.devtools.restart.classloader.Restar‌​tClassLoader(id=1241‌​4))"` – Marcel Caferati May 14 '17 at 00:12

2 Answers2

1

You forgot to put the class type User.class:

entityManager.createQuery("SELECT u FROM User u where  u.deleted = :deleted", User.class)
             .setParameter("deleted", false).getSingleResult();

Your method:

 public User getActiveUser(User user) {
    EntityManager entityManager = getEntityManager();      
    try {
        User user = (User) = entityManager.createQuery
                           ("SELECT u FROM User u WHERE u.deleted = :deleted", User.class)
                           .setParameter("deleted", false).getSingleResult();
    } finally {
        entityManager.close();
    }
    return user;
}
  • Look on first example, you use Users class and on second User (without S in the end)
  • You need to be care with this query, because they may be returned more than one record
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
  • I have tried with both, casting (User) and adding User.class – Marcel Caferati May 12 '17 at 12:19
  • I have tried that, and this is the error I get `Can not pass an instance of "class com.project.model.User (loaded by instance of sun.misc.Launcher$AppClassLoader(id=8597))" to an instance of "class com.project.model.User (loaded by instance of org.springframework.boot.devtools.restart.classloader.RestartClassLoader(id=12414))"` – Marcel Caferati May 14 '17 at 00:06
1

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!

Community
  • 1
  • 1