9

我得到了错误Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [1] did not match expected type [java.lang.Boolean]; nested exception is java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [java.lang.Boolean]

我对此感到困惑,因为它来自下面显示的被注释掉的服务方法。当我将其注释掉时,可以避免错误。该active列是TINYINT(1)要么1要么0

实体:

@Entity
@NamedQueries({
        @NamedQuery(name="Workflow.findByUUID", query="SELECT w FROM Workflow w WHERE w.uuid = :uuid"),
        @NamedQuery(name="Workflow.findByActive", query="SELECT w FROM Workflow w WHERE w.active = :active ORDER BY id ASC")
})

我的仓库:

@Repository
public interface WorkflowRepository extends JpaRepository<Workflow, Integer> {
    List<Workflow> findByActive(@Param("active") Integer active);
}

我的服务:

@Service
public class WorkflowService {

    @Autowired
    WorkflowRepository workflowRepository;

    /**
     * Get active workflows
     */
    @Transactional(readOnly = true)
    public List<Workflow> findActive() {
        //return workflowRepository.findByActive(1);
        return null;
    }

当我取消注释时

4

1 回答 1

19

您似乎已将Workflow.active属性映射为Boolean. 所以你的存储库应该是这样的:

@Repository
public interface WorkflowRepository extends JpaRepository<Workflow, Boolean> {
    List<Workflow> findByActive(@Param("active") Boolean active);
}

呼叫workflowRepository.findByActive(true)应该按照您的意愿行事。

事实上,HQL 有时会在数据库和 Java 映射之间产生类型差异,因为在这些环境中类型不同。所以检查你Entityactive类型以做出适当的Repository

于 2013-04-03T15:20:43.067 回答