hibernate-criteria

Grails withCriteria fetch, or a fast way to sort in predetermined order

我怕爱的太早我们不能终老 提交于 2019-12-11 09:14:35
问题 A database function returns id's of all events within a certain radius, ordered by their distance. Afterwards, to preserve performance I'm eagerly loading necessary collections in withCriteria like this: def events = Event.withCriteria { 'in'('id', ids) fetchMode("someRelation", FetchMode.JOIN) // a few more joins setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY) } However this messes up the ordering. I've noticed that the result of this criteria query returns all events sorted

JPA criteria query in a many-to-many relationship using IN operator

馋奶兔 提交于 2019-12-11 03:13:38
问题 I have the following two entities: Profile, Category In the Profile entity i have a List of categories that the profile has In the Category entity i have a List of profiles that the category has The generated table is called profiles_has_categories and has the following columns: profile_id, category_id I want to find all profiles that belong to any of the categories with the following ids 1, 2, 3, 4 I would do that in plain sql with: SELECT p.* FROM profiles p, profiles_has_categories phc

Hibernate setMaxResult on parent limit child collection too

拟墨画扇 提交于 2019-12-11 02:03:08
问题 I have a parent entity with a @ManyToMany(fetch=FetchType.EAGER) child collection. I need to load only first record i found of Parent entity, so i load it using this criteria: session.createCriteria(Parent.class).setMaxResult(1).uniqueResult(); Work fine, but the limit is applied to child collection too, and this is very bad. How can i get only first record of parent, but all record of its child? Thanks 回答1: Just mark the collection of children as fetch = FetchType.LAZY , don't fetch it in

hibernate criteria query select for update order by

≯℡__Kan透↙ 提交于 2019-12-11 01:38:36
问题 I have the following code that uses Hibernate Criteria API. List<?> results = getCurrentSession() .createCriteria(PersonEvent.class) .add(Restrictions.eq(STATUS, EventStatus.NEW)) .addOrder( Order.asc("sequence") ) .setLockMode(LockMode.PESSIMISTIC_WRITE) .setMaxResults(maxResults).list(); It generates the following sql select * from ( /* criteria query */ select this_.SEQUENCE as SEQUENCE0_0_, this_1_.CREATED_UTC as CREATED3_0_0_, this_1_.UPDATED_UTC as UPDATED5_0_0_, this_1_.STATUS as

how to create Criteria of Group By Month of Date Field + Grails

ⅰ亾dé卋堺 提交于 2019-12-10 23:49:31
问题 I want to get count of users grouped on a month basis from today to 365 days back. I am using groovy with Hibernate Criteria builder API . Any easy way to do this grouping? how do we specify the date format for grouping the date field by month? Right now I have the following: def con_cnt =Users.createCriteria().list { like('employeeid','c_%') between('sunsetdate', fromDate, toDate) projections { count('employeeid') //groupProperty('sunsetdate') } } The groupProperty('sunsetdate') groups it on

Hibernate full text search custom order by

血红的双手。 提交于 2019-12-10 19:26:19
问题 We want to add custom order by to hibernate full text search,suppose we want to search the record based on location, if the location is "country,state,city Then we want to have the search with records in the top which are near to user We followed following links. using mysql "order by case" in hibernate criteria But the order by clause is not getting added when we add it to criteria object Sort is working only when we set to full Text Query object like below, here we can do only asc and desc,

Hibernate Criteria Query for multiple columns with IN clause and a subselect

寵の児 提交于 2019-12-10 18:09:52
问题 I have a question very similar to this question. I am selecting all data from table1 for all matching unique combinations of field3 and field4 from table2. Here is my stripped down SQL: select * from table1 as t1 where (t1.field1, t1.field2) in (select distinct field3, field4 from table2 as t2 where t2.id=12345); I need to translate my SQL to Hibernate Criteria. I have my entity objects mapping correctly to the tables and transform the response into the correct result entity, but I cannot get

is it possible to maintain the different schema in same entity class

前提是你 提交于 2019-12-10 16:27:56
问题 I have two schema(claim and policy). For both schema am using same Entity class. My problem is, claim schema has column city but policy schema does have city column. So If I use entity class by policy schema I get error. is this only the way to change the Entity class for each schema ? or is it possible to maintain the different schema in same entity class? My Entity class : @Entity @Table(name = "Table_name") public class X { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name =

Is Hibernate Criteria API Deprecated and if so what API should we use?

拟墨画扇 提交于 2019-12-10 15:28:25
问题 Just as the title says. Is it deprecated? It says in the dev manual that it is and we should user JPA Criteria, but in the user manual there is no mention of this. 回答1: AFAIK and off looking at the Javadocs, the Hibernate Criteria API is not deprecated. However, there is a very good argument for using JPA over hibernate, as it means you can switch between persistence providers without having to modify your code. Where as if you go with the Hibernate Criteria API, then your code is completely

using mysql “order by case” in hibernate criteria

余生颓废 提交于 2019-12-10 14:46:29
问题 I'm using Hibernate 4.3.1 final, Mysql 5.5 and I want to use an "order by case" order logic on some joined entities. A pure sql representation of what I wish to achieve would look something like: select adv.id, adv.published_date from advert as adv join account as act on act.id = adv.act_id join account_status as aas on aas.id = act.current_aas_id order by case aas.status when 'pending' THEN 1 when 'approved' THEN 1 else 2 end, adv.published_date; This orders the adverts of pending and