简体   繁体   中英

Hibernate query by example

I have class A that has some primitive attributes and also member of type B. type B has a map:

// 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:

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. 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.

any clue on what am I doing wrong?

Thanks, Ronen.

The Example restrictions ignore associations (and, although this is not documents, element collections). 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 ). You'll have to revert to HQL or SQL restrictions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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