相关疑难解决方法(0)

listOf()返回MutableList

在Kotlin中,使用该listOf()函数创建的列表(应该是不可变的)通过类型检查来反对MutableList使用is运算符.

例:

fun main(args: Array<String>) {
    val list = listOf("I'm immutable")

    println(list is MutableList)
}
Run Code Online (Sandbox Code Playgroud)

将打印

真正

有趣的是,使用的空列表listOf<String>()将使检查失败并在返回单例对象时打印falseEmptyList.

经过一番挖掘后,事实证明mutableListOf()创建一个java.util.ArrayListwhile会listOf()最终创建一个java.util.Arrays$ArrayList,但是没有一个类实现MutableList,所以为什么非空列表仍然通过类型检查呢?因此,有没有另一种方法可靠地检查列表是否可变,而不必检查它的实际实现(is ArrayList等)?

java kotlin

10
推荐指数
1
解决办法
737
查看次数

迁移到 Spring Boot 3 后请求未知的换行转换

我遇到以下 jpa 查询异常

org.springframework.orm.jpa.JpaSystemException: Unknown wrap conversion requested: java.util.ArrayList to java.lang.string: 'org.hibernate.type.descriptor.java.StringJavaType'
Run Code Online (Sandbox Code Playgroud)

Jpa 查询是

@Query(value ="select s from table s where (coalesce(:users,1)='1' or s.user in :users) and "
        + "(coalesce(:employees,1)='1' or s.employee in :employees) and "
        + "(:number=null or s.salary=:number)")
List<TableEntity> findTable(@Param("users") List<String> users,
        @Param("employees") List<String> employees,
        @Param("number" Long number),
        Pageable paging);
Run Code Online (Sandbox Code Playgroud)

其中users和可以为空employeesList<String>迁移到 Spring Boot 3.0.4 后我开始看到这个异常。

java hibernate jpa spring-data spring-boot

9
推荐指数
0
解决办法
3690
查看次数

Spring Boot 3升级后使用List参数的jpa查询抛出异常

我有一个帐户实体:

@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Account {
    @Id
    @GeneratedValue
    @JsonIgnore
    private Long id;
    private String accountName;
    private String supplierName;
    private String customerName;
    private Double amount;
Run Code Online (Sandbox Code Playgroud)

以及一个带有 jpa 查询的帐户存储库,该查询应按帐户名称列表过滤帐户,或者当列表为空时不过滤:

@RepositoryRestResource
public interface AccountRestResource extends PagingAndSortingRepository<Account, Long>, CrudRepository<Account, Long> {
    @Query("SELECT DISTINCT account.customerName " +
            "FROM Account account " +
            "WHERE UPPER(account.customerName) LIKE CONCAT('%',UPPER(:filter),'%') " +
            "AND ((:accountNames) IS NULL OR account.accountName IN (:accountNames)) " +
            "ORDER BY account.customerName")
    Page<String> findDistinctCustomerNames(List<String> accountNames, String filter, Pageable page);
}
Run Code Online (Sandbox Code Playgroud)

升级到最新的 Spring …

java hibernate spring-data-jpa spring-boot

8
推荐指数
1
解决办法
3132
查看次数