jpa:无法解析到对象

huwehgph  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(251)

我以前的代码是:

@Query(value = "select * " +
            "from role r " +
            "where r.company_code = :companyCode " +
            "and r.feature_category = :featureCategory " +
            "and (:roleName is null or r.role_name ilike concat('%', :roleName, '%'))", nativeQuery = true)
    List<Role> findAll(String companyCode, Long featureCategory, String roleName);

一切正常,直到我把它改成:

@Query(value = "select * " +
            "from role r " +
            "where r.company_code = :companyCode " +
            "  and r.feature_category = :featureCategory " +
            "  and (:roleName is null or r.role_name ilike concat('%', :roleName, '%')) " +
            "  and (:roleIds is null or r.role_id in (:roleIds))", nativeQuery = true)
    List<Role> findAll(String companyCode, Long featureCategory, String roleName, List<UUID> roleIds);

我的dto是:

@Data
@Entity
public class Role {

    @Id
    @Column(name = "role_id", nullable = false)
    @Type(type = "pg-uuid")
    private UUID roleId = null;

    @Column(name = "company_code", length = 10, nullable = false)
    private String companyCode;

    @Column(name = "feature_category", nullable = false)
    private Long featureCategory;

    @Column(name = "create_user")
    private String createUser;

    @Column(name = "create_date")
    private Date createDate = null;

    @Column(name = "update_user")
    private String updateUser;

    @Column(name = "update_date")
    private Date updateDate = null;

    @Column(name = "role_name")
    private String roleName = null;

}

引发异常:

org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = bytea
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
  Position: 170
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2578)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2313)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369)

我不知道为什么它不能解析对象后,我添加了列表roleids过滤器。你能解释一下原因并给我一个解决办法吗?拜托!

3htmauhk

3htmauhk1#

经过一天的思考:v
我已经解决了这个问题。
如果roleids为null,则无法绑定。所以你要给它分配一个空列表。代码为:

roleIds = roleIds == null ? new ArrayList<>() : roleIds;
List<Role> roleList = roleRepository.findAll(companyCode, featureCategory, roleName, roleIds);

相关问题