LIKE query on values of Map ElementCollection in entity in HQL

跟風遠走 提交于 2019-12-25 16:38:30

问题


I have an entity named « Form » that contains a Map « details » of (key/value)=(language code/translation) :

@Entity
class Form {
:
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "language", length = 50, nullable = false)
@MapKeyEnumerated(EnumType.STRING)
@Column(name = "value", length = 150)
private final Map<Language, String> details = new HashMap<Language, String>  ();

: }

I would like to retrieve all Form records having a translation containing (!) « xxx ».

The following clause retrieves « Form » where the map contains a « value » whose value is exactly equals the search stream. I would like however apply a LIKE condition.

@Query("SELECT f FROM Form f JOIN f.details d WHERE KEY(d) = :language AND :search IN elements(d) ")

Thanks for your help!


回答1:


In SQL, % is a wildcard used in the LIKE operator. Your query would be something like:

@Query("SELECT f FROM Form f JOIN f.details d 
WHERE KEY(d) = :language 
AND VALUE(d) LIKE '%' + :search + '%'") 

If this doesn't work, you might have to append % to the beginning and end of you variable :search



来源:https://stackoverflow.com/questions/32382842/like-query-on-values-of-map-elementcollection-in-entity-in-hql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!