Model cannot be cast - internal error in spring boot and Criteria API Join

Jacob

I am trying to join and retrieve joined result using spring boot and criteria API join. Here I am trying to retrieve role id from second table where a user id match found by joining with first table. When I am running my query I am getting the error like,

Error Update

{
"timestamp": "2018-09-10T10:54:01.163+0000",
"status": 500,
"error": "Internal Server Error",
"message": "com.example.model.Roles cannot be cast to 

}

Here I did not used metamodel generated classes. Without using that I implemented. But still getting the same error.

Mạnh Quyết Nguyễn

Two things are wrong:

  1. multiselect() takes a list of selected fields. You must not pass a javax.persistence.criteria.Join to its parameter.
  2. Your query condition is not related to join at all, since the condition on field nuserId can full-filled by Users table only.

If you just want to get both User and its UserRoleMapping at the same time, you can try:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Users> cq = cb.createQuery(Users.class);
Root<Users> rootRoles = cq.from(Users.class);
rootRoles.fetch("roleUserRoleMappingMappingJoin", JoinType.LEFT);

cq.select(rootRoles).where(cb.equal(rootRoles.get("nuserId"),nuserID));  
List<Users> roleJoinResultObj = em.createQuery(cq).getResultList();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

java.lang.Object; cannot be cast to model class: error in Spring Boot

Spring Boot JSON parse error: Cannot construct instance of model

Spring Boot AMQP class java.util.LinkedHashMap cannot be cast to class com.example.model.X

Java Spring Boot REST API - Patch - "java.util.LinkedHashMap cannot be cast to xxx"

500 Internal Server Error Spring boot & JWT

spring boot on azure Internal Server Error

Spring boot on Azure produces internal server error

Error of "Cannot deserialize" in Spring Boot

JPA Criteria API join

Criteria API join with filter

Spring boot JPA & Criteria API - Select single column

Using Criteria API + JPA Specifications with IN Expression and Spring Boot

Spring Boot war file on tomcat: errorPageFilter cannot be cast to TomcatEmbeddedServletContainerFactory

Model cannot be cast to ModelRealmProxyInterface

Spring Boot - There was an unexpected error (type=Internal Server Error, status=500)

DBFlow(Model Cannot be cast To Model)

(Kotlin) com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.phonetest2.model.HallData

Gradle 8 plugin-publish fails with "class org.gradle.api.internal.provider.DefaultSetProperty cannot be cast to class java.util.Collection" error

Cannot load configuration class error spring boot

Cannot parse constant error in SQL and Spring Boot

Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

500 Internal Server Error instead of 404 in Spring Boot

Spring-boot stop application when internal error occurs

"cannot be cast" in Spring

org.gradle.initialization.DefaultSettings_Decorated cannot be cast to org.gradle.api.internal.project.ProjectInternal

Cannot cast object 'org.gradle.api.internal... after Update Android Studio

stream API collect method compiler error - Object cannot be cast to List

JPA criteria API function convert or cast

Error while consuming a REST API in Spring Boot