spring-data-jpa
spring-data-jpa copied to clipboard
List parameter type in findBy functions [DATAJPA-1736]
Mihály Mikó opened DATAJPA-1736 and commented
The main issue is that I have an entity with a field of List type (with Converter to de/serialize) and I would like to write a findByBlist(List<B> bList) function to it. The parameter of the function is list type, but it seems behave not like a list, but separated parameters in the sql query. According the logs, the serialize converter successfully ran, but only for the list elements, not for the list as an object.
Currently i am creating BListWrapper class to wrap this list and serialize it in the converter to single list object. This is working, but this Wrapper only a workaround and seems a boilerplate code.
It there any way to tell the findBy method to handle a list (or any complex type) as a single object, and not to spit it for several parameters in sql query? (If I use list at the IN clause, the functionality working perfectly, but not this time). I can write example codes if needed.
Thank you!
No further details from DATAJPA-1736
Jens Schauder commented
Thanks for raising the issue MandM.
What is Blist in your entity? And how is it represented in the database?
Mihály Mikó commented
Thanks for the fast reply! So I created a basic example with the necessary data to show the problem.
First of all, the entity has the following fields, with the proper getter/setter functions:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(columnDefinition = "clob")
@Convert(converter = StringListConverter.class)
private List<String> myStringListField;
So the database representation is a simple clob column. In the clob the data is in json format, handling it with the following converter:
StringListConverter implements AttributeConverter<List<String>, String>
I can share the converter implementation too, but i think it is not necessary from the point of view of the problem.
For this entity I have created the following repository:
public interface MyEntityRepository extends CrudRepository<MyEntity, Long> {
Optional<MyEntity> findByMyStringListField(List<String> myStringList);
}
The build is completed successfully, and I tested the simple findById function is working perfectly. So lets test the following:
repository.findByMyStringListField(asList("element1", "element2", "element3"))
.orElseThrow(() -> new AssertionError("Missing entity"));
When I try to run this, i got the following error:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [element1] did not match expected type [java.util.List (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [element1] did not match expected type [java.util.List (n/a)]
I think the problem is, that somehow the logic behind the function splitting the list, and only after that using the StringListConverter for each element. And the algorithm wanted to create a query with not 1, but 3(size of the list) parameters. My goal is not to split the list, just use the annotated converter for the list and handle it as 1 parameter.
Thanks for your help!
Mihály Mikó commented
Hi,
The day before yesterday i got an email about a new comment on this ticket, but the body of the email was empty. I see my last comment, but not any other comment on it. Is there any, what I can't see for some strange reason?
Jens Schauder commented
I changed the status of the issue to open, since you provided the requested information. This might have triggered an email. You can see status changes and similar when you select the "All" tab under "Activity" (at the top of the comment section).