Search code examples
javaspringspring-boothibernatespring-mvc

springboot class org.hibernate.mapping.Bag cannot be cast to class org.hibernate.mapping.SimpleValue


I've been working on a Springboot API, using MySQL as database. For deployment I'm using a docker-compose

version: "3.7"
services:
  backend:
    build: ./backend
    networks:
      - shared-net
    environment:
      - spring.datasource.url=jdbc:mysql://application-db:3306/db?allowPublicKeyRetrieval=true
    ports:
      - 82:8080
    depends_on:
      - application-db

  application-db:
    image: "mysql:latest"
    restart: always
    ports:
      - 3306:3306
    networks:
      - shared-net
    environment:
      MYSQL_ROOT_PASSWORD: '' 
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes" 
      MYSQL_DATABASE: db 
    volumes:
      - ./db:/var/lib/mysql

networks:
  shared-net:

On startup, I get the following error in logs: https://pastebin.com/zNq6JKs3

I've been debugging this & looking around for days & didn't find a fix. What I'm suspecting is an inheritance issue in these classes:

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Etape {
    @Id
    @GeneratedValue
    private long id;

    @OneToMany(mappedBy = "etape", cascade = CascadeType.REMOVE)
    private List<Remarque> remarques;
    
    @ManyToOne()
    @JoinColumn(name = "idProjet")
    private Project project;
    
    @Column()
    private int termine;
}
package com.application.model.etapes.Etape1;

import com.application.model.etapes.Etape;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@Entity
public class Etape1  extends Etape {

    @Column(columnDefinition = "TEXT")
    private String attr1;
    
    @Column()
    private String attr2;
    
    @Column()
    private boolean attr3;

    @OneToMany(mappedBy = "etape1", cascade = CascadeType.REMOVE)
    private List<Beneficiare> beneficiares;
    
    @OneToMany(mappedBy = "etape1", cascade = CascadeType.REMOVE)
    private List<Objectif> objectifs;
    
    @OneToMany(mappedBy = "etape1", cascade = CascadeType.REMOVE)
    private List<VeilleGlobal> veilleGlobals;
    
    @OneToOne(mappedBy ="etape1" ,  cascade = CascadeType.REMOVE)
    private Mission mission;
}

Any help in debuging/fixing this issue is much appreciated and needed.

NOTE:

In the logs, there are 2 interesting errors:

  • java.lang.ClassCastException: class org.hibernate.mapping.Bag cannot be cast to class org.hibernate.mapping.SimpleValue
  • PostInitCallbackEntry - Entity(com.model.etapes.Etape) sqmMultiTableInsertStrategy interpretation

The second one made me think of the inheritance issue, the first probably a relation issue? (ManyToOne/OneToMany)

I tried using @Inheritance(strategy = InheritanceType.SINGLE_TABLE) but I kept getting the same error so, I'm a bit lost now


Solution

  • Had to update Hibernate to latest version to solve the issue, in case anyone faces this someday.