gpt4 book ai didi

java - InvalidDataAccessApiUsageException : Parameter value [. ..] 与预期类型不匹配 [java.util.UUID (n/a)]

转载 作者:行者123 更新时间:2023-12-04 11:23:51 30 4
gpt4 key购买 nike

我正在使用 Criteria API 使用过滤器构建命名查询。它适用于普通的字符串比较,但在过滤 UUID 时会引发以下错误:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [67279329-5096-4196-9E73-748B33122CE2] did not match expected type [java.util.UUID (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [67279329-5096-4196-9E73-748B33122CE2] did not match expected type [java.util.UUID (n/a)]

有几个问题解决了这个问题,但都没有奏效,我尝试了以下方法:
  • 添加 @Column(name = "id", updatable = false, nullable = false)到实体字段
  • 添加 @Type(type="org.hibernate.type.UUIDCharType")到实体字段
  • 添加 @Type(type="uuid-char")到实体字段

  • Foo 实体:

    @Entity
    //lombok stuff
    public class Foo {

    @Id
    private UUID id;
    private String name;
    //...
    }

    SQL 变体:

    CREATE TABLE foo
    (
    id UUID NOT NULL PRIMARY KEY,
    name VARCHAR NOT NULL,
    ...
    );

    FooController:

    @GetMapping(value = "/foo")
    public ResponseEntity<List<Foo>> findFoos(@RequestParam Map<String, String> filterArguments) {
    FooFilter filter = filterMapper.map(filterArguments);
    FooSpecification spec = new FooSpecification(filter);

    List<Foo> foos = fooRepo.findAll(spec);
    //...
    }

    Foo规范:

    public class FooSpecification extends SpecificationHelper implements Specification<Foo> {
    private final FooFilter filter;

    public FooSpecification(FooFilter filter) {
    this.filter = filter;
    }

    @Override
    public Predicate toPredicate(Root<Foo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
    Predicate predicate = null;

    predicate = createIdPredicate(root, filter, criteriaBuilder, predicate);
    predicate = createNamePredicate(root, filter, criteriaBuilder, predicate);
    // ...

    return predicate;
    }

    private Predicate createIdPredicate(Root<Foo> foo, FooFilter filter, CriteriaBuilder cb, Predicate predicate) {
    Predicate returnPredicate = predicate;
    if (StringUtils.isNotBlank(filter.getId()))
    returnPredicate = addAndPredicate(cb, predicate, cb.like(cb.upper(foo.get("id")), "%" + filter.getId().toUpperCase() + "%"));
    return returnPredicate;
    }

    private Predicate createNamePredicate(Root<Foo> foo, FooFilter filter, CriteriaBuilder cb, Predicate predicate) {
    Predicate returnPredicate = predicate;
    if (StringUtils.isNotBlank(filter.getName()))
    returnPredicate = addAndPredicate(cb, predicate, cb.like(cb.upper(foo.get("name")), "%" + filter.getName().toUpperCase() + "%"));
    return returnPredicate;
    }
    }
    addAndPredicate是一个简单的辅助方法,它只使用criteriaBuilder.and(predicate,newPredicate)
    FooFilter只有字符串字段

    最佳答案

    问题是你的 FooFilter仅包含 String字段,而您正在尝试比较 String idUUID对象在 createIdPredicate() .这就是抛出异常的原因。有2种解决方案:

  • 或者,您应该替换 filter.getId().toUpperCase()参加 createIdPredicate()UUID.fromString(filter.getId())正如@tentacle
  • 所建议的那样
  • 或者,更改您的 FooFilter过滤器使 id类型为 java.util.UUID .

  • 顺便说一句,恕我直言,比较 UUIDslike不是一个好主意,因为一个 UUID永远不会像另一个 UUID , 每个 UUID对象是唯一的。您的谓词应该检查 FooFilter 传输的 ID 是否相等。还有一个来自 DB .

    关于java - InvalidDataAccessApiUsageException : Parameter value [. ..] 与预期类型不匹配 [java.util.UUID (n/a)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60617289/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com