使用Hibernate Criteria在Map中过滤键和值 [英] Use Hibernate Criteria for filtering keys and values in Map

查看:111
本文介绍了使用Hibernate Criteria在Map中过滤键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下持久化类:

  public class Code {

@ElementCollection(targetClass = CodeValue.class)
@MapKeyClass(CodeProperty.class)
@JoinTable(name =code_properties)
@CreateIfNull(value = false)
私人地图< CodeProperty,CodeValue> ; propertiesMap =
new HashMap< CodeProperty,CodeValue>();

...
}

公共类CodeProperty {
私人字符串名称;
...
}

公共类CodeValue {
私有字符串值;
...
}

我想要列出一个列表在 propertiesMap 中包含的某些属性过滤的代码(例如,名为color的属性的值为green的代码。



我使用以下基本条件:

$ p $ Criteria criteria = currentSession()
.createCriteria(Code.class, code)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

当我尝试执行一个收集过滤器(如建议这里):

  criteria.createAlias(code.properties,p); 
criteria.add(Restrictions.eq( p.foo,test1));
criteria.setFetchMode(code.properties,FetchMode.JOIN);
criteria.list();
  


> org.hibernate.QueryExceptio n:无法解析属性:foo of:com.example.CodeValue

这意味着,我不会真的不明白为什么,hibernate正在考虑 code.properties CodeValue 而不是 map !!!

我也尝试访问此字段而不创建别名,这样hibernate似乎可以访问正确的 Code 类。我使用 properties.indeces (建议此处 ):

  criteria.add(Restrictions.eq(properties.indeces,foo)); 
criteria.list();

但是,我得到以下错误:

 无法解析属性:properties.indeces:com.example.Code 

有人能帮我理解最新的错误吗?正确的Criteria查询会用绿色找到代码是什么?



您可以结帐

谢谢

在您的项目中观看MapTest文件夹github我建议您始终将变量名作为列名称调用,并在其中使用相同的名称标准。
标准是地图,其第一个参数是属性名称,第二个参数是属性值。
对于多对多关系,您可以尝试使用:

  @JoinTable(
name =docente_classe ,
joinColumns =
@JoinColumn(name =id_dipendente,referencedColumnName =id_dipendente),
inverseJoinColumns =
@JoinColumn(name =id_classe,referencedColumnName = id_classe)

这是JPA注解,我不知道这个编译您的项目。



https://github.com/tredue1/School_Manager/blob/master/code/SM/src/main/java/agc2/it/sm / b

对于多对多关系的查询来看这篇文章:



jpa多对多关系的标准

I have the following persisted class:

public class Code {

  @ElementCollection(targetClass = CodeValue.class)
  @MapKeyClass(CodeProperty.class)
  @JoinTable(name="code_properties")
  @CreateIfNull( value = false )
  private Map<CodeProperty,CodeValue> propertiesMap =
      new HashMap<CodeProperty, CodeValue>();

  ...
}

public class CodeProperty {
    private String name;
    ...
}

public class CodeValue {
    private String value;
    ...
}

And I'm trying to get a list of Code filtered by some properties I have in propertiesMap (e.g. the codes where the property named "color" has the value "green".

I'm using the following base criteria:

Criteria criteria = currentSession()
    .createCriteria(Code.class, "code")
    .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

When I try to perform a collection filter (as suggested here):

criteria.createAlias("code.properties", "p");
criteria.add(Restrictions.eq("p.foo", "test1"));
criteria.setFetchMode("code.properties", FetchMode.JOIN);
criteria.list();

I get the following error:

org.hibernate.QueryException: could not resolve property: foo of: com.example.CodeValue

Which means that, I don't really understand why, hibernate is considering that code.properties is a CodeValue instead of a map!!!

I also tried to access this field without creating the alias, and this way hibernate seems to access the correct Code class. I used properties.indeces (as suggested here):

criteria.add(Restrictions.eq("properties.indeces", "foo"));
criteria.list();

But with this I get the following error:

could not resolve property: properties.indeces of: com.example.Code

Can someone help me out understanding whats wrong? What would the correct Criteria query be to find the Codes with color green?

You can checkout the Github project that demonstrates this problem.

Thanks

解决方案

There are various problems with your approach: Watching MapTest folder in your project github I can advise you to always call the variable names as column names, and to use those same names in the Criteria. The Criteria are maps, whose first argument is the attribute name and the second argument is the value of attribute. For many to many relationship you can try to use :

  @JoinTable(
      name="docente_classe",
      joinColumns=
          @JoinColumn(name="id_dipendente", referencedColumnName="id_dipendente"),
      inverseJoinColumns=
          @JoinColumn(name="id_classe", referencedColumnName="id_classe")
  )

This is JPA annotation , I don't know if this compile on your project.

https://github.com/tredue1/School_Manager/blob/master/code/SM/src/main/java/agc2/it/sm/gestioneDocente/DocenteJpa.java

For query on many to many relationship watch this post :

jpa criteria for many to many relationship .

这篇关于使用Hibernate Criteria在Map中过滤键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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