criteria

JPA CriteriaBuilder conjunction criteria into a disjunction criteria

☆樱花仙子☆ 提交于 2019-12-04 15:09:00
I need to replicate this query into JPA CriteriaBuilder code: .... where article.client_id = 1 and article.price > 0 and ( article.code like '%this is statement%' or article.oem_code like '%this is statement%' or ( article.description like '%this%' and article.description like '%is%' and article.description like '%statement%' ) ) and here's my code: ... Root<Article> article = cq.from(Article.class); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.equal(article.get(Article_.clientId), filter.getClientId())); predicates.add(cb.greaterThan(article.get(Article_.price),

Hibernate criteria for many-to-many entity property

荒凉一梦 提交于 2019-12-04 13:42:54
@Entity class A { @ManyToMany private List<B> list; ... } @Entity class B { ... } I'd like to get list from A class using criteria (not sql query). Is it posible to do this? Projection in this case does not work. Unfortunately, The Criteria only allows selecting the root entity, and not any joined entity. It would thus be easier if your ManyToMany was bidirectional. You could the use the criteria equivalent of select b from B b inner join b.as a where a = :theA If that's not an option, I think the only way is to use a subquery, and thus code the criteria equivalent to select b from B b where b

Hibernate Criteria for different field value length

ε祈祈猫儿з 提交于 2019-12-04 12:48:37
I've got an Entity which have a (string) field of size = 10 @Column(length = 10) String code; However, field's value length could be 5 or 10. I would like to create a hibernate criteria matching that field's value according to its length, like: final List<String> codesLong= new ArrayList<String>(); final List<String> codesShort= new ArrayList<String>(); ... criteria.add(Restrictions.or( Restrictions.and(Restrictions.in("code", codesShort), Restrictions.eq("code.length", 5)), Restrictions.and(Restrictions.in("code", codesLong), Restrictions.eq("code.length", 10))) ); However... the above looks

Hibernate Criteria n+1 issue with maxresults

 ̄綄美尐妖づ 提交于 2019-12-04 12:17:17
问题 Using hibernate ctiteria I want to select an object and it's associated oneToMany list of objects. I want to paginate through this list avoiding the dreaded hibernate n+1 select issue Here's a working solution which requires 11 trips to the database for 10 parent objects. Criteria criteria = this.getSession().createCriteria(Mother.class); criteria.addOrder(Order.asc("title")) .setMaxResults(details.getMaxRows()) .setFirstResult(details.getStartResult()) .setFetchMode("kittens", FetchMode

Hibernate Child Collection Limited When Using Left Join in Criteria

人盡茶涼 提交于 2019-12-04 11:09:30
When using hibernate criteria just altering the join type affects the results of the child collections of the root domain class. For instance, having class Parent have a one-to-many relationship with class Child with the following data: Parent | id | Name | | 1 | Parent 1 | Child | id | parent_id | Name | | 1 | 1 | Child1 | | 2 | 1 | Child2 | Using the following hibernate criteria returns the 1 parent row, and accessing the child collection results in the two rows being returned: session.createCriteria(Parent.class) .createCriteria('child', CriteriaSpecification.INNER_JOIN) .add( Restrictions

How can I write a Hibernate Criteria query, for a super-class, and check for a certain sub-class?

冷暖自知 提交于 2019-12-04 07:22:11
How can I write a Hibernate Criteria query, for a super-class, and check for a certain sub-class? Let's imagine we have the following classes all mapped up with Hibernate-JPA: @Entity @Inheritance(strategy = InheritanceType.JOINED) public class Bar { @Id @Column(name = "id") private Long id; } @Entity @PrimaryKeyJoinColumn(name="bar_id") public class Foo extends Bar { } @Entity @PrimaryKeyJoinColumn(name="bar_id") public class Goo extends Bar { } When writing a Criteria query like this, I would like, for performance, to use a left-join with the sub-class: getSession() .createCriteria(Bar.class

Remove dynamically an ordering to the result set in org.hibernate.Criteria

China☆狼群 提交于 2019-12-04 06:57:57
I have a Criteria with: Criteria criteria= session.createCriteria(Libro.class).addOrder( Order.asc("ID") ); However, when I want to get the rowcount fail: criteria.setProjection(Projections.rowCount()); because there is an order by in the query. How to remove dynamically the ordering in the Criteria? I mean, I am looking for like criteria.removeOrder("ID") . Most Criteria are actually instances of CriteriaImpl. If you cast your Criteria to CriteriaImpl and get the iterator for orders, you can remove them that way. Criteria criteria= session.createCriteria(Libro.class).addOrder( Order.asc("ID")

How to convert a JPQL with subquery to Criteria API equivalent?

ぐ巨炮叔叔 提交于 2019-12-04 06:54:59
问题 Have a simple object model made up of 5 entities: Company Organization Address Club Group A Company is associated with a single Organization. (Group and Club are also associated with a single Organization - these are unidirectional, meaning the Organization does not contain a reference to its owner). An Organization may have 0 or more Address(es). A subquery can be used to access Company objects based on a specific zipcode, which is an attribute of an Address. Here is a JPQL query that can

How can I express joining to a grouped subquery using NHibernate?

筅森魡賤 提交于 2019-12-04 06:51:06
I'm trying to express a SQL query using NHibernate's Criteria API, and I'm running into difficulty because I'm thinking in a database-centric way while NHibernate is object-centric. SQL (works great): select outerT.id, outerT.col1, outerT.col2, outerT.col3 from tbl outerT inner join (select max(innerT.id) from tbl innerT group by innerT.col1) grpT on outerT.id = grpT.id Essentially, this is a self-join of a table against a subset of itself. I suppose I could try turning the self-join into a restriction: select outerT.id, outerT.col1, outerT.col2, outerT.col3 from tbl outerT where outerT.id in

How to avoid unnecessary selects and joins in HQL and Criteria

假如想象 提交于 2019-12-04 05:32:18
I have been trying different combinations of HQL and Criteria and I haven't been able to avoid some unnecessary joins (in both) and some unnecessary selects (in Criteria). In our scenario, we have a @ManyToMany relationship between Segment and Application entities (navigation is from Segment to Applications). First I tried this Criteria : Application app = ... List<Segment> segments = session.createCriteria(Segment.class) .createCriteria(Segment.APPLICATIONS) .add(Restrictions.idEq(app.getId())) .list(); Wich produces this SQL: select this_.id as id1_1_, this_.description as descript2_1_1_,