JPQL - 我如何搜索键和值的实体 (Hibernate OGM (MongoDB) - Map<String, String>)
JPQL - How can i search entities for key and value (Hibernate OGM (MongoDB) - Map<String, String>)
我将 Hibernate OGM 用于 MongoDB,并使用 Map 映射简单的 ShopItem 实体:
@NamedQueries({
@NamedQuery(name = ShopItem.GET_BY_NAME,
query = "select items from ShopItem items where
items.name=:name"),
@NamedQuery(name = ShopItem.GET_BY_OPTION,
query = "select items from ShopItem items
join items.options map
where ( KEY(map) = :optionKey and map = :optionValue )")
})
@Entity
public class ShopItem {
public static final String GET_BY_NAME = "ShopItem.getByName";
public static final String GET_BY_OPTION = "ShopItem.getByOption";
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
private String name;
@ElementCollection(fetch = FetchType.EAGER)
private Map<String, String> options = new HashMap<>();
//...etc
}
这个贴图不错。我尝试为我的 ShopItemRepository 编写 JPQL 命名查询。我按字段 'name' 搜索了 NamedQuery,它工作正常。但是我需要按 'key' 和 'value' 这两个字段进行搜索,我无法为此编写正确的 JPQL。
例如我在数据库中有实体:
{
"_id":"df111b8a-b831-4200-95b3-f80576cd7273",
"name":"Example",
"description":"My description",
"options":
{
"weight":"60 kg",
"age":"22"
}
}
我想找到选项具有键 'age' 且值为“22”的所有实体。
我试着写:
select items from ShopItem items join items.options map where ( KEY(map) = :optionKey and map = :optionValue )
或
select items from ShopItem items join items.options map where ( KEY(map) = :optionKey and :optionValue in (VALUE(map))
或
select items from ShopItem items where ( KEY(items.options) = :optionKey and items.options = :optionValue )
或
select items from ShopItem items where ( KEY(items.options) = :optionKey and :optionValue in (VALUE(items.options))
并且有这个错误
org/hibernate/hql/ast/origin/hql/resolve/GeneratedHQLResolver.g: node from line 1:85 no viable alternative at input '('
can't look backwards more than one token in this stream
我在 repo 中的代码:
List<ShopItem> result = entityManager.createNamedQuery(ShopItem.GET_BY_OPTION, ShopItem.class)
.setParameter("optionKey", key)
.setParameter("optionValue", value)
.getResultList();
你能帮我写出正确的 JPQL 查询吗?如果这是不可能的,那么建议如何用另一种方式来做。
我无法使用 Spring 数据 JPA。
目前,查询此案例的唯一方法是使用本机查询:
List<ShopItem> results = session
.createNativeQuery( "{ 'options.Weight': '60 Kg' }", ShopItem.class )
.getResultList();
您仍然可以使用 @NamedNativeQuery
(而不是 @NamedQuery
)声明查询,但 MongoDB.
不支持参数
我将 Hibernate OGM 用于 MongoDB,并使用 Map 映射简单的 ShopItem 实体:
@NamedQueries({
@NamedQuery(name = ShopItem.GET_BY_NAME,
query = "select items from ShopItem items where
items.name=:name"),
@NamedQuery(name = ShopItem.GET_BY_OPTION,
query = "select items from ShopItem items
join items.options map
where ( KEY(map) = :optionKey and map = :optionValue )")
})
@Entity
public class ShopItem {
public static final String GET_BY_NAME = "ShopItem.getByName";
public static final String GET_BY_OPTION = "ShopItem.getByOption";
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
private String name;
@ElementCollection(fetch = FetchType.EAGER)
private Map<String, String> options = new HashMap<>();
//...etc
}
这个贴图不错。我尝试为我的 ShopItemRepository 编写 JPQL 命名查询。我按字段 'name' 搜索了 NamedQuery,它工作正常。但是我需要按 'key' 和 'value' 这两个字段进行搜索,我无法为此编写正确的 JPQL。 例如我在数据库中有实体:
{
"_id":"df111b8a-b831-4200-95b3-f80576cd7273",
"name":"Example",
"description":"My description",
"options":
{
"weight":"60 kg",
"age":"22"
}
}
我想找到选项具有键 'age' 且值为“22”的所有实体。
我试着写:
select items from ShopItem items join items.options map where ( KEY(map) = :optionKey and map = :optionValue )
或
select items from ShopItem items join items.options map where ( KEY(map) = :optionKey and :optionValue in (VALUE(map))
或
select items from ShopItem items where ( KEY(items.options) = :optionKey and items.options = :optionValue )
或
select items from ShopItem items where ( KEY(items.options) = :optionKey and :optionValue in (VALUE(items.options))
并且有这个错误
org/hibernate/hql/ast/origin/hql/resolve/GeneratedHQLResolver.g: node from line 1:85 no viable alternative at input '('
can't look backwards more than one token in this stream
我在 repo 中的代码:
List<ShopItem> result = entityManager.createNamedQuery(ShopItem.GET_BY_OPTION, ShopItem.class)
.setParameter("optionKey", key)
.setParameter("optionValue", value)
.getResultList();
你能帮我写出正确的 JPQL 查询吗?如果这是不可能的,那么建议如何用另一种方式来做。
我无法使用 Spring 数据 JPA。
目前,查询此案例的唯一方法是使用本机查询:
List<ShopItem> results = session
.createNativeQuery( "{ 'options.Weight': '60 Kg' }", ShopItem.class )
.getResultList();
您仍然可以使用 @NamedNativeQuery
(而不是 @NamedQuery
)声明查询,但 MongoDB.