当前位置: 首页 > 知识库问答 >
问题:

类 X 不能强制转换为类布尔值(X 位于加载程序'app'的未命名模块中;布尔值在模块 java.base 的加载程序 'bootstrap

曾翰飞
2023-03-14

我注意到,当其他人遇到这个问题时,那是因为他们试图投射对象。我没有试图投。此外,我试图询问一个对象是否存在于使用Spring Boot存储库的数据表中。出现以下问题:

问题到底是什么?

模型

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

如果相关,服务类:

@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:选择skillgroup0_。id为id1_7_,skillgroup0_。名称为name2_7_,skillgroup0_。从skillgroup0_中键入类型3_7_。名字=?而skillgroup0_ type=?

Java . lang . classcastexception:class hs . Leiden . competence app . skill _ related . skill _ groups。SkillGroup不能转换为Java . lang . boolean(hs . Leiden . competency app . skill _ related . skill _ groups)类。SkillGroup位于加载程序“app”的未命名模块中;java.lang.Boolean位于加载程序“bootstrap”的java.base模块中

在hs.leiden.CompetienceApp.skill_related.Skillr_groups.SkillGroupReportTest.findSkillGroupByNameAndType(未知来源)的jdk.proxy2/jdk.proxy1.$Proxy99.FindKillGroupByNameandType(SkillGroupRepoTest.java:43)

共有1个答案

邵阳辉
2023-03-14

< code>find…方法返回已找到的类型的实例。对于您的< code>SkillGroupRepo,它将返回一个< code>SkillGroup。如果您想要返回< code>boolean来指示是否可以在数据库中找到某些内容,则应该使用< code>exists…方法:

boolean existsByNameAndType(String name, Character type);

您可以在Spring Data JPA参考文档的查询创建部分了解更多信息。

 类似资料: