Reputation: 31
In project I have multiple tables with same structure (numbers and types of columns). This tables are independent, I need hibernate criteria object that gives me all values from all tables. Something like in SQL:
select * from TABLE1, TABLE2, TABLE3;
I know that I can merge results from multiple criterias in list etc. but I need one Criteria object because we are using it later in code in many places.
Upvotes: 2
Views: 7384
Reputation: 28569
You can use build criteria query from multiple roots, from the docs
Roots define the basis from which all joins, paths and attributes are available in the query. A root is always an entity type. Roots are defined and added to the criteria by the overloaded from methods on javax.persistence.criteria.CriteriaQuery
an addapted example from the docs
CriteriaQuery query = builder.createQuery();
Root<Men> men = query.from( Men.class );
Root<Women> women = query.from( Women.class );
Predicate menRestriction = builder.and(
builder.equal( men.get( Men_.gender ), Gender.MALE ),
builder.equal( men.get( Men_.relationshipStatus ), RelationshipStatus.SINGLE )
);
Predicate womenRestriction = builder.and(
builder.equal( women.get( Women_.gender ), Gender.FEMALE ),
builder.equal( women.get( Women_.relationshipStatus ), RelationshipStatus.SINGLE )
);
query.where( builder.and( menRestriction, womenRestriction ) );
Upvotes: 8