hibernate-criteria

Hibernate criteria query for Collection Table?

老子叫甜甜 提交于 2019-11-28 10:10:12
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(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "rule_id") private Set<RuleAction>

Using Hibernate's Criteria and Projections to Select Multiple Distinct Columns

丶灬走出姿态 提交于 2019-11-28 07:25:26
Using Hibernate's Criteria, I want to execute the equivalent of: select distinct uspscity, state from citycomplete where USPSCITY = 'HOUSTON' I thought doing the following would yield the results I wanted: ProjectionList projList = new ProjectionList(); projList.add(Projections.distinct(Projections.property("id.state"))); projList.add(Projections.distinct(Projections.property("id.uspsCity"))); criteria.setProjection(projList); But, what this actually does is execute something like: select distinct uspscity, distinct state from citycomplete where USPSCITY = 'HOUSTON' Which throws an error,

Please provide example for an If statement in hibernate criteria

此生再无相见时 提交于 2019-11-28 04:26:17
问题 How do you write criteria on if condition using hibernate criteria. My query that needs to be transformed is SELECT date, product, IF(type = 'msrp', amount, 0) price, IF(type != 'msrp', amount, 0) tax FROM productdetail group by date, product; Please provide the right usage of if statement in hibernate criteria. 回答1: As @JBnizet said, This is not supported by the criteria API. unless you are using NHibernate if your using NHIbernate Projections.Conditional( Restrictions.EqProperty(...........

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

a 夏天 提交于 2019-11-28 02:50:52
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 not reflect in the last queried cache for any DB. This leads to an overhead of compiling the generated

Null list returned from hibernate query with embedded id

浪尽此生 提交于 2019-11-27 23:51:19
问题 I have an entity with an embedded key. The entity has only the key as a field and the key has 7 fields, some of which can be null. When I run the following query: Criteria criteria = session.createCriteria(getPersistentClass()); criteria.add(Restrictions.eq("id.profPropertyId", profileExtensionName)); Object obj = criteria.list(); log.info(obj); return (List<ProfileExtensions>) obj; I get the correct number of results, but each result is null (i.e. I get a list of 4000 null objects). I have

Pagination with Hibernate Criteria and DISTINCT_ROOT_ENTITY

纵然是瞬间 提交于 2019-11-27 18:51:57
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) { criteria.add(Restrictions.eq("cidade.id", cidadeId)); } criteria.addOrder(Order.desc("dataPostagem"));

SQL 'LIKE' operator in Hibernate Criteria API

ε祈祈猫儿з 提交于 2019-11-27 17:42:08
问题 I want to implement some universal filter with Hibernate Criteria . It should work like LIKE operator from SQL: SELECT * FROM table WHERE table.ANYCOLOUMNHERE LIKE '%'||anyvaluehere||'%' I have Map<String, String> where key is a column name, and value is its value. I tried something like this: for (Entry<String, String> filter : filters.entrySet()) { crit.add(Restrictions.ilike(filter.getKey(), filter.getValue(), MatchMode.ANYWHERE)); } But when field type is not String , it causes java.lang

Java - Hibernate criteria.setResultTransformer() initializes model fields with default values

守給你的承諾、 提交于 2019-11-27 14:49:24
问题 I am new to Hibernate and I am trying to get some data from the database. I don't want to get the full data but a projection of an entity. The thing is that in the for-loop when I get the id and the name of my projection, it gets the default values id=0 and name=null instead of id=7 and name="Name 8" which are the records of the original entity in the database. Do you know what causes this problem? The for-loop is in the last code. Here is the Student Entity @Entity(name = "Students") public

Hibernate criteria on collection values

允我心安 提交于 2019-11-27 13:11:15
问题 I'm trying to put together a complicated query using Hibernate. I've been leaning toward Criteria, but I'm beginning to suspect it's not possible, and so any suggestions would be helpful. I have an entity structure like the following: public class Attribute { private Integer id; private String name; private Set<Value> values; } public class Instance { private Integer id; private int instanceRef; private Set<Value> values; } public class Value { private Integer id; private Attribute attribute;

JPA2: Case-insensitive like matching anywhere

人走茶凉 提交于 2019-11-27 10:42:25
问题 I have been using Hibernate Restrictions in JPA 1.0 ( Hibernate driver ). There is defined Restrictions.ilike("column","keyword", MatchMode.ANYWHERE) which tests if the keyword matching the column anywhere and it is case-insensitive. Now, I am using JPA 2.0 with EclipseLink as driver so I have to use "Restrictions" build-in JPA 2.0. I found CriteriaBuilder and method like , I have also found out how to make it matching anywhere ( although it is aweful and manual ), but still I haven't figured