Home > Net >  Class X cannot be cast to class Boolean (X is in unnamed module of loader 'app'; Boolean i
Class X cannot be cast to class Boolean (X is in unnamed module of loader 'app'; Boolean i

Time:05-17

I noticed that when others are getting this problem, it is because they are trying to cast objects. I am not trying to cast. Furthermore, I am trying to ask if an object exists in a data table using Spring Boot repository. The following issue is occurring:

What is the issue exactly?

The model:

@Entity
@Table(name = "skillGroup")
@Data
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SkillGroup {
    @Id
    @Column(name = "id")
    @SequenceGenerator(
            name = "skillGroup_sequence",
            sequenceName = "skillGroup_sequence",
            allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "skillGroup_sequence")
    private Long id;

    @Column(name = "name", columnDefinition = "TEXT", unique = true, nullable = false)
    private String name;

    @Column(name = "type", columnDefinition = "CHAR(2)")
    private char type;
}

The repository:

@Repository
public interface SkillGroupRepo extends
        JpaRepository<SkillGroup, Long> {
    List<SkillGroup> findSkillGroupByType(Character type);
    SkillGroup findSkillGroupByName(String name);
    boolean findSkillGroupByNameAndType(String name, Character type);
}

The Test:

@Rollback(value = false)
@SpringBootTest
class SkillGroupRepoTest {
    @Test
    public void findSkillGroupByNameAndType() {
        boolean skillGroup = skillGroupRepo.findSkillGroupByNameAndType("Gebruikersinteractie", 'B');
        System.out.println("skillGroup = "   skillGroup);
        Assertions.assertTrue(skillGroup);
    }
}

If relevant, the Service class:

@Service
@Transactional
@RequiredArgsConstructor
@AllArgsConstructor
public class SkillGroupService {

    @Autowired
    private SkillGroupRepo skillGroupRepo;

    private SkillGroupMapper skillGroupMapper;

    public void AddSkillGroup(String name, Character type) {
        if(skillGroupRepo.findSkillGroupByNameAndType(name, type)) {
            throw new ResponseStatusException(
                    BAD_REQUEST,
                    "Skill group with name: "   name   " and type: "   type   " already exists.");
        }
        SkillGroup skillGroup = SkillGroup.builder()
                .name(name)
                .type(type)
                .build();
        skillGroupRepo.save(skillGroup);
    }

Error message:

Hibernate: select skillgroup0_.id as id1_7_, skillgroup0_.name as name2_7_, skillgroup0_.type as type3_7_ from skill_group skillgroup0_ where skillgroup0_.name=? and skillgroup0_.type=?

java.lang.ClassCastException: class hs.leiden.competenceApp.skill_related.skill_groups.SkillGroup cannot be cast to class java.lang.Boolean (hs.leiden.competenceApp.skill_related.skill_groups.SkillGroup is in unnamed module of loader 'app'; java.lang.Boolean is in module java.base of loader 'bootstrap')

at jdk.proxy2/jdk.proxy2.$Proxy99.findSkillGroupByNameAndType(Unknown Source) at hs.leiden.competenceApp.skill_related.skill_groups.SkillGroupRepoTest.findSkillGroupByNameAndType(SkillGroupRepoTest.java:43)

CodePudding user response:

find… methods return an instance of the type that has been found. In the case of your SkillGroupRepo, it will return a SkillGroup. If you want to return boolean that indicates whether or not something can be found in the database, you should use an exists… method instead:

boolean existsByNameAndType(String name, Character type);

You can learn more in the Query Creation section of the Spring Data JPA reference documentation.

  • Related