Class X 无法转换为 class 布尔值(X 在加载程序 'app' 的未命名模块中;布尔值在加载程序 java.base 的模块中 'bootstrap

Class X cannot be cast to class Boolean (X is in unnamed module of loader 'app'; Boolean is in module java.base of loader 'bootstrap

我注意到当其他人遇到这个问题时,那是因为他们试图投射对象。 我不是想施法。此外,我试图使用 Spring 引导存储库询问对象是否存在于数据 table 中。出现以下问题:

到底是什么问题?

型号:

@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;
}

存储库:

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

测试:

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

如果相关,服务 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);
    }

错误信息:

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)

find… 方法 return 已找到类型的实例。对于您的 SkillGroupRepo,它将 return 一个 SkillGroup。如果你想 return boolean 指示是否可以在数据库中找到某些东西,你应该使用 exists… 方法:

boolean existsByNameAndType(String name, Character type);

您可以在 Spring Data JPA 参考文档的 Query Creation 部分了解更多信息。