hibernate-criteria

Hibernate Criteria Restrictions AND / OR combination

时光怂恿深爱的人放手 提交于 2019-11-27 10:31:31
How would I achieve this using Hibernate Restrictions? (((A='X') and (B in('X',Y))) or ((A='Y') and (B='Z'))) think works Criteria criteria = getSession().createCriteria(clazz); Criterion rest1= Restrictions.and(Restrictions.eq(A, "X"), Restrictions.in("B", Arrays.asList("X",Y))); Criterion rest2= Restrictions.and(Restrictions.eq(A, "Y"), Restrictions.eq(B, "Z")); criteria.add(Restrictions.or(rest1, rest2)); For the new Criteria since version Hibernate 5.2: CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder(); CriteriaQuery<SomeClass> criteriaQuery = criteriaBuilder.createQuery

Hibernate Group by Criteria Object

与世无争的帅哥 提交于 2019-11-27 07:33:55
I would like to implement the following SQL query with Hibernate Criteria: SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name <operator> value GROUP BY column_name I have tried to implement this with Hibernate Criteria but it didn't work out. Can anyone give me an example how this can be done with Hibernate Criteria? Thanks! Ken Chan Please refer to this for the example .The main point is to use the groupProperty() , and the related aggregate functions provided by the Projections class. For example : SELECT column_name, max(column_name) , min (column_name) ,

Hibernate Criteria With Property Not In (Subquery)

ぃ、小莉子 提交于 2019-11-27 06:32:30
问题 I want to execute query something like Select id, name from information where name not in (select firstname from contact where id = 1) Information Id Name 1 Test Contact id firstname 1 name 2 Test If I am using neProperty() function, it will returns records as name != Test. How can I implement using hibernate criteria? Thanks 回答1: Create a select-all criteria: Criteria cr = session.createCriteria(Your.class); List list = cr.list(); Then you can add you restriction to it, i.e. where column 1=8

Hibernate criteria query for Collection Table?

ⅰ亾dé卋堺 提交于 2019-11-27 03:28:05
问题 I have following Entity @Entity @Table(name = "rule") public class Rule implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "rule_id") private Long id; @ElementCollection(targetClass = Action.class) @CollectionTable(name = "rule_action", joinColumns = @JoinColumn(name = "rule_id")) @Enumerated(value = EnumType.STRING) @Column(name = "action") private Set<Action> actions; //After editing as per jbrookover's suggestion adding a new mapping @OneToMany

Hibernate Criteria vs HQL: which is faster? [closed]

爷,独闯天下 提交于 2019-11-26 23:50:26
问题 I have been reading some anwers, but i'm still confused. ¿Why? because the differences that you have mentioned do not relate with the performance. they are related with easy use.(Objetc(criteria) and SQL(hql)). But I would like to know if "criteria" is slower than hql for some reason. I read this in another anwers "There is a difference in terms of performance between HQL and criteriaQuery, everytime you fire a query using criteriaQuery, it creates a new alias for the table name which does

How to transform a flat result set using Hibernate

百般思念 提交于 2019-11-26 23:09:46
Is it possible to map result of SQL to not flat object? List<Customer> customers = hibernateSession().createCriteria(CustomerDetailsView.class) .add(Restrictions.in("userName", userName)) .setProjection(buildProjection()) .setResultTransformer(Transformers.aliasToBean(Customer.class)) .list(); In my case CustomerDetailsView has flat structure. But I need to map it to object like this: public class Customer { private String userName; private String title; private String firstName; private String lastName; private String type; private String companyName; private AddressDetails addressDetails; }

Pagination with Hibernate Criteria and DISTINCT_ROOT_ENTITY

五迷三道 提交于 2019-11-26 19:38:14
问题 I've have already implemented pagination using the following code: public Paginacao<Anuncio> consultarPaginado(int pagina, Integer cidadeId) { Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Anuncio.class); criteria.add(Restrictions.eq("ativo", true)); criteria.add(Restrictions.eq("statusLiberacao", AnunciosUtil.STATUS_ANUNCIO_LIBERADO)); criteria.add(Restrictions.eq("statusVendaAnuncio", AnunciosUtil.STATUS_VENDA_ANUNCIO_DISPONIVEL)); if (cidadeId != null) {

Hibernate Criteria Restrictions AND / OR combination

只愿长相守 提交于 2019-11-26 17:57:18
问题 How would I achieve this using Hibernate Restrictions? (((A='X') and (B in('X',Y))) or ((A='Y') and (B='Z'))) 回答1: think works Criteria criteria = getSession().createCriteria(clazz); Criterion rest1= Restrictions.and(Restrictions.eq(A, "X"), Restrictions.in("B", Arrays.asList("X",Y))); Criterion rest2= Restrictions.and(Restrictions.eq(A, "Y"), Restrictions.eq(B, "Z")); criteria.add(Restrictions.or(rest1, rest2)); 回答2: For the new Criteria since version Hibernate 5.2: CriteriaBuilder

Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct

你。 提交于 2019-11-26 14:28:59
I am pretty new to Hibernate. I found out that we can get distinct result using following two different ways. Could any one tell me what is the difference between them? When to use one over other? Projections.distinct(Projections.property("id")); vs criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); While similar names, the usage is different. I. Projections.distinct(Projections.property("id")); this statement would be translated into SQL Statement. It will be passed to DB Engine and executed as a SQL DISTINCT . See: 17.9. Projections, aggregation and grouping so e.g. this example:

JPA and Hibernate - Criteria vs. JPQL or HQL

 ̄綄美尐妖づ 提交于 2019-11-26 12:34:05
What are the pros and cons of using Criteria or HQL ? The Criteria API is a nice object-oriented way to express queries in Hibernate, but sometimes Criteria Queries are more difficult to understand/build than HQL. When do you use Criteria and when HQL? What do you prefer in which use cases? Or is it just a matter of taste? I mostly prefer Criteria Queries for dynamic queries. For example it is much easier to add some ordering dynamically or leave some parts (e.g. restrictions) out depending on some parameter. On the other hand I'm using HQL for static and complex queries, because it's much