0

I have 2 tables:

  1. a master table with single attributes of some objects (id, name, title, ...)
  2. a table with repeating attributes (master_id, attribute_name, attribute_value)

Example data for #2:

 - 10, "authors", "John Bill"
 - 10, "authors", "Merry J"
 - 10, "owners", "Chris O."
 - 11, "authors", "Andrew K."

It's a one-to-many relation:

<set name="repeating" table="xxx" cascade="none" mutable="false" lazy="true"    fetch="join">
<key column="...."/>
<one-to-many class="...." />
</set>

I want to find the master object (id=10) where "authors" = "John Bill" and "authors" = "Merry J" and "owners" = "Chris O."

For the first condition I can do it in that way:

session.createCriteria(Master.class)
.createCriteria("repeating")
.add(Restrictions.eq("attributeName", "authors"))
.add(Restrictions.eq("attributeValue", "John Bill"))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

How can I add other conditions using Criteria?

Thanks

Zini
  • 909
  • 7
  • 15

1 Answers1

0

I guess what you want is detached criteria

DetachedCriteria dc = DetachedCriteria.forEntityName("repeating")
    .add(Restrictions.eq("attributeName", "authors"))
    .add(Restrictions.eq("attributeValue", "John Bill"))
    // other rows
    .setProjection(Projections.distinct(Property.forName("masterId")));

session.createCriteria(Master.class)
    .add(Property.forName("id").in(dc))
    .list();
hsluo
  • 1,652
  • 15
  • 20
  • Thanks but it's not OK. "owners" is a value for "attributeName". It means that it should be done in that way: `.add(Restrictions.eq("attributeName", "owners")) .add(Restrictions.eq("attributeValue", "Chris O.")) ` But in that case it will not return any results. – user3494426 Apr 03 '14 at 17:18
  • sorry for my mistake. then I suggest you use `Restrictions.disjunction()` to build you criteria, or in this case, you may find HQL easier to write. – hsluo Apr 04 '14 at 02:22