24

I am quite new to Spring and Java as a whole and I am currently working on an API. I've got this code which accesses MSSQL database, retrieves the results and IS SUPPOSED TO visualize it, but it doesn't work. I mean, if I would only call the procedure and visualize the result set, it would work normally. The problem is that I can't work with the "RatingProcedure" object. When I try get some object's value, it shows me following error:

Hibernate: {call dis_entity.spdcmpracovnici(?,?,?,?)} 2019-09-02 01:56:01.355 WARN 8720 --- [nio-8050-exec-3] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class com.pproi.dcm.ratingprocedure.RatingProcedure ([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; >com.pproi.dcm.ratingprocedure.RatingProcedure is in unnamed module of loader >org.springframework.boot.devtools.restart.classloader.RestartClassLoader @7193df92)]

The code:

@GetMapping("/replacements/{sourceId}/workers")
    public ResponseEntity<List<PossibleReplacementResponseModel>> getReplacements(@PathVariable Integer sourceId,
            @RequestParam(required = false) Integer targetWorkerId,
            @RequestParam(required = false) Integer targetLineId,
            @RequestParam(required = false) String targetWorkplaces) {

        List<PossibleReplacementResponseModel> response = new ArrayList<>();

        // 400 Bad Request
        if ((targetLineId != null && targetWorkerId != null) || (targetWorkplaces != null && targetLineId == null))
            return ResponseEntity.badRequest().build();

        StoredProcedureQuery ratingProcedure = entityManager.createNamedStoredProcedureQuery("dcmrating");

        StoredProcedureQuery storedProcedure = ratingProcedure.setParameter("pk_target_worker", targetWorkerId)
                .setParameter("pk_target_sdl", targetLineId).setParameter("pk_target_workplaces", targetWorkplaces)
                .setParameter("pk_source_sdl", sourceId);

        List<RatingProcedure> ratings = storedProcedure.getResultList();

        for(RatingProcedure rating : ratings) {
            PossibleReplacementResponseModel responseModel = new PossibleReplacementResponseModel();
            Optional<Worker> _worker = workerRepository.findById(rating.getPersonalId());

            if(_worker.isPresent()) {
                Worker worker = _worker.get();
                responseModel.setId(worker.getId());
                responseModel.setName(worker.getFullName());
                responseModel.setSkills(findWorkersSkills(worker));
                responseModel.setPhysicalExamination(findWorkersMedicalExaminationEndDate(worker));
                responseModel.setExams(findWorkersExams(worker));
                responseModel.setRestrictions(findWorkersRestrictions(worker));
                responseModel.setRating(rating.getRating());
            }
            response.add(responseModel);
        }

        return ResponseEntity.ok(response);
    }

I probably even have an idea about the solution! I probably should customize my Restart Classloader as seen in here, but there my skills end and I have no idea what I should specify in the file.

Any ideas?

Thank you a lot.

ddavidik
  • 241
  • 1
  • 2
  • 6

8 Answers8

23

A simple workaround is to disable the Restart by setting the system property:

spring.devtools.restart.enabled = false

For example, when using spring-boot maven plugin, launch the app this way:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.devtools.restart.enabled=false"
Benoit
  • 5,118
  • 2
  • 24
  • 43
  • 5
    I spent 8 hours pulling my hair, till I saw this answer. After I fixed the removed the dev tools jar the issue went away. – Rahul Khimasia Oct 22 '20 at 01:30
  • 5
    @RahulKhimasia Glad to contribute to saving your hair ;-) – Benoit Oct 22 '20 at 09:48
  • 2
    in deed this is just a workaround and not a solution. you are disabling the automatic restart and so then you have to restart manually after each change. Like "my car has a little malfunction, so I don't use my care anymore.". – Robert Fornesdale May 14 '23 at 14:03
15
java.lang.ClassCastException: class <Class_Name> cannot be cast to class <Class_Name> (<Class_Name> is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader <Class_Name> is in unnamed module of loader 'app')

This is related to spring dev tools package. Try removing the package if it is not required.

5

Maybe what is happening here is that: Reference is being created of the base class and then casting is happening for the inherited class.

Let me give an example. Assume that we have a base class named "BaseClass" and one class "ChildClass" is extending "BaseClass".

BaseClass bClass = new BaseClass(); // NOTICE HERE -> new BaseClass();


// Somewhere in next code lines
ChildClass childClass = (ChildClass) bClass;

This will throw the error which you are mentioning.

DO this:

BaseClass bClass = new ChildClass(); // NOTICE HERE -> new ChildClass();


// Somewhere in next code lines
ChildClass childClass = (ChildClass) bClass;

I hope this will solve the problem as it solved mine

Brijesh Lakkad
  • 611
  • 10
  • 13
0

I was having the same problem when I am connecting to the mysql db via persistence.xml:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_1_0.xsd"
             version="2.2">
    <!-- the name="pu" will be used in MainApplicationClass -->
    <persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/spring_hibernate_jpa_db?useSSL=false&amp;serverTimezone=UTC&amp;useLegacyDatetimeCode=false"/>
            <property name="hibernate.connection.username" value="hilal" />
            <property name="hibernate.connection.password" value="hilal" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.id.new_generator_mappings" value="true" />
        </properties>
    </persistence-unit>
</persistence>

the only solution for me is to start mvn with the command:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="Dspring.devtools.restart.enabled=false"
Eddie
  • 15
  • 6
Hilal Aissani
  • 709
  • 8
  • 3
0

I tried this and it's working in my case :

mvn dependency:purge-local-repository
Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
-1

This might work : If you are using springboot devtools in your project (pom/gradle), then make sure your class that is using @SpringBootApplication is "public".

@SpringBootApplication
public class MyApp {
  public static void main(String ... args) {
        SpringApplication.run(MyApp.class,args);
    }
}
Anver Sadhat
  • 3,074
  • 1
  • 25
  • 26
-4

removing the dependency of devtools from pom.xml will help

<!--    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
-->
-4

You should be main class

public static void main(String[] args) {

    System.setProperty("spring.devtools.restart.enabled", "false");

    SpringApplication.run(MyApp.class, args);
}
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 16 '21 at 10:15