0

This is my Entity Class.

@Entity
public class Profile {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long profileId;

@Transient
private String name;

@ElementCollection(fetch = FetchType.EAGER)
private Map<String, String> trNames;     //Key: LanguageCode, Value: translated text
...
}

ProfileRepository Class

public interface ProfileRepository extends JpaRepository<Profile, Long> {

//This method should go through the Map Collection and return a profile whose name matches the given parameter value.
Profile findByTrNames(String code, String name);

}

I created a JUnit Test Class to test this method. This line of code

Profile found = repository.findByTrNames("en", "Interview IT");

throws an exception.

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [en] did not match expected type [java.util.Map]; nested exception is java.lang.IllegalArgumentException: Parameter value [en] did not match expected type [java.util.Map]
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:301)
    at etc..

Could anyone please help me?

Thank you very much.

1 Answer 1

2

This should work

@Query("select p from Profile p where p.trNames[?1] = ?2")
Profile findByTrNames(String code, String name);

UPDATE

JIRA ticket is created to support this feature: https://jira.spring.io/browse/DATAJPA-643

Sign up to request clarification or add additional context in comments.

4 Comments

great.. It does indeed. :) Could you please perhaps explain why it threw an exception? Why did it expect a type of java.util.Map?
spring-data constructs the query based on your declared method name which is findByTrNames. Since trNames is of type Map, it expects the parameter to be also a Map. I agree it could be a little smarter when it comes to Map fields in entities, since they are somewhat specific and it makes sense that two params in the method could be interpreted as key and value.
@Pedrag - Any chance you open a ticket in the JIRA for improved Map binding? I am sure we can sketch something out.
@OliverGierke Thanks for the suggestion, I've opened a ticket for this, updated my answer with issue reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.