简体   繁体   English

休眠查询示例

[英]Hibernate query by example

I have class A that has some primitive attributes and also member of type B. type B has a map: 我有一个具有一些原始属性的类A,并且还具有类型B的成员。类型B具有一个映射:

// mapping name to number //将名称映射到数字

private Map<String, Double> myMap   = null;

@ElementCollection(fetch=FetchType.EAGER)
@MapKeyColumn(name = "NAME")
@Column(name = "NUMBER")
@CollectionTable(name = "NAME_MAPPING", uniqueConstraints = { @UniqueConstraint(columnNames = { "NAME", "NUMBER" }) })
public Map<String, Double> getMyMap()
{
  return this.myMap;
}

Snippet of A: A的摘要:

private String name = null;

private B b = null;

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "B_FK")
public B getB()
{
  return b;
}

Now I want to find A by Example. 现在,我想通过示例查找A。 I defined the following: 我定义了以下内容:

public List<A> findByExample(A a) 
{
  Session session = getSession();

  Criteria criteria = session.createCriteria(A.class);
  Example example = Example.create(a);
  Criteria bCriteria = criteria.createCriteria("b");
  B b = material.getB();
  bCriteria.add(Example.create(b));

  criteria = criteria.add(example);
  criteria = criteria.setFetchMode("b", FetchMode.JOIN);

  return criteria.list();   
}

I tried all kinds of variations but with no success. 我尝试了各种变化,但都没有成功。 the method returns all DB entries with the same A.name and ignore the equality of the Map in B. 该方法返回所有具有相同A.name的数据库条目,并忽略B中Map的相等性。

any clue on what am I doing wrong? 关于我在做什么错的任何线索?

Thanks, Ronen. 谢谢罗尼

The Example restrictions ignore associations (and, although this is not documents, element collections). Example限制将忽略关联(尽管不是文档,但包括元素集合)。 Even if you used just the Criteria API (without using Example), element collections can't be queries with Criteria (se https://hibernate.onjira.com/browse/HHH-869 ). 即使仅使用Criteria API(不使用Example),也不能使用Criteria查询元素集合(例如https://hibernate.onjira.com/browse/HHH-869 )。 You'll have to revert to HQL or SQL restrictions. 您必须恢复为HQL或SQL限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM