Unknown wrap conversion requested after migrating to spring boot 3
Asked Answered
T

1

9

I am getting below exception for a jpa query

org.springframework.orm.jpa.JpaSystemException: Unknown wrap conversion requested: java.util.ArrayList to java.lang.string: 'org.hibernate.type.descriptor.java.StringJavaType'

The Jpa query is

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

where users and employees are List<String> which can be null. I started seeing this exception after migrating to spring boot 3.0.4.

Tenorrhaphy answered 14/6, 2023 at 9:14 Comment(6)
I think it should be @Param("clients") List<String> clients instead of @Param("clients" List<String> clients)Reprint
Also, I see a parameter users in the query which is not supplied as the argument, and a parameter clients supplied in the method which is not used in the query. Can you share the actual code?Reprint
@ArunSudhakaran That was a typo while adding the question. In the actual code I have written it properly.Tenorrhaphy
yes, i suggest to improve the query ... minimal reproducible example would be helpful :)Maite
Does this answer your question? Hibernate randomly throws error: Cannot coerce value `[]` [java.util.ArrayList] as LongAcaricide
@AndreyB.Panfilov I tried all the solutions but it did not work . I am getting the same exceptionTenorrhaphy
C
0

Using Spring Boot 3.2.4

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.4</version>
    <relativePath/>
</parent>

I was able to successfully adapt the query in the question, both with null and non-null parameters, after identifying and fixing some apparent typos.

Having the entity class defined as follows (getters, setters omitted)

@Table
@Entity
public class TableEntity {

    @Id
    Long id;

    @Column
    String employee;

    @Column(name = "usr") //user is a reserved SQL word
    String user;

    @Column
    Long salary;

}

and having defined the repository as follows:

@Repository
public interface TestRepository extends JpaRepository<TableEntity, Long> {

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

}

the following repository invocations execute without errors and retrieve the results expected.

List<TableEntity> entities = testRepository.findTable(
        Arrays.asList("a", "b", "c"),
        Arrays.asList("e1", "e2", "e3"),
        15L,
        Pageable.ofSize(100));


List<TableEntity> allEntities = testRepository.findTable(
        null,
        null,
        null,
        Pageable.ofSize(100));

Note the following:

  • table s has been rewritten as TableEntity s
  • user is a reserved word in SQL, so the respective column has been named usr
  • @Param("number" Long number) has been written as @Param("number") Long number
  • coalesce(:users,1)='1' has been rewritten as coalesce(:users,'1')='1'
  • coalesce(:employees,1)='1' has been rewritten as coalesce(:employees,'1')='1'
  • :number=null has been rewritten as :number is null
Chomp answered 23/5, 2024 at 9:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.