如何使用 Spring Data JPA 查询 Map 值? [英] How to query for Map values with Spring Data JPA?

查看:30
本文介绍了如何使用 Spring Data JPA 查询 Map 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的数据库模型是这样的:我有 Store 并且每个 Store 都有一个本地化的名称.所以我选择将本地化名称表示为 Map,如下所示:

so my database model goes like this: I have Stores and every Store has a localized name. So I have chosen to represent the localized name as a Map like this:

public class Store {
   private Map<Locale,LocalizedValue> name;
}

如你所见,它是一个 的 Map,其中 LocalizedValue 是一个这样的类:

as you can see it's a Map of <Locale, LocalizedValue> where the LocalizedValue is a class like this:

@Embeddable
public class LocalizedValue {

   @Column(name = "value")
   private String value;
}

一切都很好.但是我遇到了一个问题,我想查询我的 Spring Data JPA 存储库并找到具有给定英文名称的所有商店.所以我的存储库方法如下所示:

and it all works great. However I get to a problem where I want to query my Spring Data JPA repository and find all the stores with a given english name. So my repository method looks like this:

Store findByName(Map.Entry<Locale, LocalizedValue> name);

但它抛出这个异常:

 2014-10-07 23:49:55,862 [qtp354231028-165] ERROR: Parameter value [en=Some Value] did not match expected type [com.test.LocalizedValue(n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [en=Some Value] did not match expected type [com.test.LocalizedValue (n/a)]
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [en=Some Value] did not match expected type [com.test.LocalizedValue (n/a)]; 
nested exception is java.lang.IllegalArgumentException: Parameter value [en=Some Value] did not match expected type [com.test.LocalizedValue (n/a)]
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:216)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)

然后我将我的存储库方法更改为如下所示:

So then I changed my repository method to be like this:

Store findByName(LocalizedValue name);

但后来我得到了这个例外:

but then I got this exception:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'name1_.pk' in 'where clause'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

我也尝试在查询方法中使用 Contains - 仍然没有运气.

I also tried using Contains in the query method - still no luck.

所以我的问题是:有没有办法查询英文名称为Some Value"的商店?

So my question is: Is there a way to query for the stores with English name 'Some Value'?

推荐答案

这需要一个手动定义的查询,如下所示:

This requires a manually defined query something like this:

interface StoreRepository extends Repository<Store, Long> {

  @Query("select s from Store s join s.map m where ?1 in (VALUE(m))"
  Optional<Store> findByName(String name);
}

它基本上告诉持久性提供者扩展映射值并检查给定参数是否在扩展值列表中.

It basically tells the persistence provider to expand the map values and check whether the given parameter is in the list of expanded values.

这篇关于如何使用 Spring Data JPA 查询 Map 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆