criteria

Hibernate Create Criteria to join the same table twice - tried 2 approach with 2 difference error

孤者浪人 提交于 2019-11-28 13:06:57
I would like to create create criteria for the following native sql. Unfortunately, I hit error of duplicate associate path when use createCriteria twice. When I try to use Restrictions.sqlRestriction. It unable provide the SQL that I want. Try 1: Create Criteria - duplicate associate path Criteria criteria = getSession().createCriteria( Company.class ); criteria.createAlias( "customerCategories", "c1" ); criteria.add( Restrictions.in( "c1.customerCategory.customerCategoryId", company.getBaseCustomerCategoryId() ) ); criteria.createAlias( "customerCategories", "c2" ); criteria.add(

Using groupProperty and countDistinct in Grails Criteria

心不动则不痛 提交于 2019-11-28 11:30:48
I'm using Grails 1.2.4. I would like to know on how can I sort by "countDistinct" (descending) and with groupProperty inside a projections. Here are my domains: class Transaction { static belongsTo = [ customer : Customer, product : Product ] Date transactionDate = new Date() static constraints = { transactionDate(blank:false) } } class Product { String productCode static constraints = { productCode(blank:false) } } In MySQL terms, this is what I want: select product_id, count(product_id) from transaction group by product_id order by count(product_id) desc In general term, I would like to get

Hibernate Criteria API - HAVING clause work arounds

只愿长相守 提交于 2019-11-28 11:11:10
I've written a query using Hibernate Criteria API to grab a summation of a particular value, now I need to be able to restrict the result to rows where that sum is greater or equal to a particular value. Normally I would use a HAVING clause in my SQL to do this, but the Criteria API doesn't seem to support that at this moment. In raw SQL this is what I need it to do: SELECT user_pk, sum(amount) as amountSum FROM transaction GROUP BY user_pk HAVING amountSum >=50; One work-around that I thought of is to use a subquery in the FROM clause that grabs this summation value and use an outer query to

Hibernate Criteria for “in subselect”

时光总嘲笑我的痴心妄想 提交于 2019-11-28 10:00:31
I'm trying to do something like this, but using Criteria instead of HQL : select user from User where user in ( select user from UserDomain where domain.id = "XXX" ) User being an entity having a one-to-many relationship to the join table UserDomain. The point here is simply to find Users that are linked to a Domain having id = "XXX". This seems like it should be very simple... but I'm having no luck so far turning up any useful docs. The subquery is very useful in cases, that you need to search the User, having one-to-many UserDomains. In that case, the WHERE UserId IN (subquery) brings big

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,

VBA (Excel): Find Based on Multiple Search Criteria Without Looping

蓝咒 提交于 2019-11-28 07:04:17
I have a large data sheet that I want to search in VBA based on 3 sets of criteria . Each row entry can be assumed to be unique. The format of the sheet/data itself cannot be changed due to requirements. (I've seen several posts on related questions but haven't found a working solution for this yet.) At first I used the classic VBA find method in a loop: Set foundItem = itemRange.Find(What:=itemName, Lookin:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows) If Not foundItem Is Nothing Then firstMatchAddr = foundItem.Address Do ' *Check the other fields in this row for a match and exit if found

Mybatis通用Mapper介绍与使用

让人想犯罪 __ 提交于 2019-11-28 05:51:04
前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中, 除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL 。而且,当数据库表结构改动时,对应的所有SQL以及实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,通用Mapper便应运而生…… 什么是通用Mapper 通用Mapper就是 为了解决单表增删改查 ,基于Mybatis的插件。开发人员不需要编写SQL, 不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法 。 如何使用 以MySQL为例,假设存在这样一张表: CREATE TABLE `test_table` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT '', `create_time` datetime DEFAULT NULL, `create_user_id` varchar(32) DEFAULT NULL, `update_time` datetime DEFAULT NULL, `update_user_id` varchar(32) DEFAULT NULL, `is_delete` int(8) DEFAULT NULL, PRIMARY KEY (`id

JPA - FindByExample

淺唱寂寞╮ 提交于 2019-11-28 04:57:58
Does anyone have a good example for how to do a findByExample in JPA that will work within a generic DAO via reflection for any entity type? I know I can do it via my provider (Hibernate), but I don't want to break with neutrality... Seems like the criteria API might be the way to go....but I am not sure how to handle the reflection part of it. Actually, Query By Example (QBE) has been considered for inclusion in the JPA 2.0 specification but is not included, even if major vendors support it. Quoting Mike Keith: I'm sorry to say that we didn't actually get to do QBE in JPA 2.0. Criteria API

How do I use a complex criteria inside a doctrine 2 entity's repository?

若如初见. 提交于 2019-11-28 04:54:29
Lets say I have a table that holds information about festivals. Each festival has a start and end date. I want to select all the festivals that are live (that happen) on a given date. Meaning, I want to select all the festivals that their start date is before or on a given date, and that their end date is after or on a the same given date. So I went on to the repository class of the festival entity, and created a method to do just that. But the criteria argument "findBy" expects is an array, which all the examples only treat as a simple criteria (eg. "array('name' => 'billy')" will select all

How to achieve “not in” by using Restrictions and criteria in Hibernate?

爱⌒轻易说出口 提交于 2019-11-28 04:38:56
I have list of category. I need a list of category by excluding 2,3 row. Can we achieve through hibernate by using Criteria and Restriction? Your question is somewhat unclear. Assuming "Category" is a root entity and "2,3" are ids (or values of some property of the category") you can exclude them using the following: Criteria criteria = ...; // obtain criteria from somewhere, like session.createCriteria() criteria.add( Restrictions.not( // replace "id" below with property name, depending on what you're filtering against Restrictions.in("id", new long[] {2, 3}) ) ); Same can be done with