2

I am trying to integrate Spring Boot and Hibernate and I want to access database without using repositories (I succeed to make all the CRUD operation using database configuration inside application.properties and getting all I need by repositories). Basically I have a ClassCastException in the following code:

 Session session = factory.openSession();
        Transaction tx = null;
        List<User> appUsers =null;
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery("FROM User WHERE id = :id");   
            query.setParameter("id", id);  //the id is given by parameter
            appUsers = query.list();
        } catch (Exception e) {
        } finally {
            session.close();
        } 
        User user = appUsers.get(0);

At the final line I'm getting this exception telling me it can't cast from User to User (same class). I know Query is deprecated an I have tried alternative but I'm still getting the same result. Even dto (data transfer object) haven't helped me out. Also I have run in debugger and I'm getting the correct result from database (MySql) but when I try to get the first object (User) which exist throws me this exception. When I verify User.class.isInstance(appUsers.get(0)) the result is false. I know the User class is correctly written according to the hibernate mapping practice.

What have I done wrong?

I have searched online for other "solutions" to solve this issue, but none worked for me or relates with this exception.

Any help would be greatly appreciated.

Juniar
  • 1,269
  • 1
  • 15
  • 24
S. Florin
  • 375
  • 3
  • 15
  • check your import for the `User` class in your class. is it the same as the one expected? – pleft Dec 07 '18 at 15:11
  • I think you should do en explicit cast :User user = (User) appUsers.get(0); – hasnae Dec 07 '18 at 15:16
  • What's the exact message of the exception you're getting? Also, check your imports as @pleft suggested – tom Dec 07 '18 at 15:24
  • I've tried explicit cast. The import is correct and in debugger attributes are correctly correlated. – S. Florin Dec 07 '18 at 15:25
  • _classCastException_ .User cannot be cast to .User . The package is the same. – S. Florin Dec 07 '18 at 15:29
  • did you tried this: User user = (User)query.uniqueResult(); – Mohsen Dec 07 '18 at 15:33
  • Not working. In the result list (after database query) i have an object that have same structure as class User (_hibernate entity class_) but somehow it is not the same instance. – S. Florin Dec 07 '18 at 15:44
  • I mean instead of `appUsers = query.list();` just use `User user = (User)query.uniqueResult();` because you are querying over id and it is unique – Mohsen Dec 07 '18 at 15:46
  • Share the stack trace. – Christopher Schneider Dec 07 '18 at 15:46
  • Then delete `User user = appUsers.get(0);` and you will not get classCastException – Mohsen Dec 07 '18 at 15:47
  • I've tried `(User)query.uniqueResult();` and I have the same exception. When I try the same piece of code outside spring specific classes (_a separate main method_ that is not running with spring configuration) everything works fine and the cast is done without any problem. – S. Florin Dec 07 '18 at 15:54
  • did you tried to remove `tx = session.beginTransaction();` ? because it's not needed here – Mohsen Dec 07 '18 at 16:01
  • I've tried alternative that don't use Transaction at all. But thanks for observation. – S. Florin Dec 07 '18 at 16:07
  • I think that Spring influences to the class User. Different class-loaders, proxy or something else. Thats why all works fine outside spring. Maybe [this will help](https://stackoverflow.com/questions/43865259/java-lang-classcastexception-with-the-same-class-object). – Anatoly Samoylenko Dec 07 '18 at 17:37
  • I know that Spring influence somehow my class. When I've tried with dto i separate the entities so one class for API and JPA repository and one for hibernate. Same cast exception for the hibernate entity class. As your suggestion, I also tried to serialize the object and to create a _hash_ method inside User class. – S. Florin Dec 07 '18 at 17:57

0 Answers0