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.