criteria

Write subquery in Criteria of nHibernate

心不动则不痛 提交于 2019-12-04 05:14:41
I've read about subquery in Criteria, but I am still unable to grasp it properly. Here I am taking one example and if somebody can help me write that using subquery it will be great. Lets say we have table Employee{EmployeeId.(int),Name(string),Post(string),No_Of_years_working(int)} Now I want all the employees who are Managers and working for less than 10 years. I know that we can get the result without using subqueries but I want to use subquery just to understand how it works in criteria. So, how I can write Criteria using subquery to get those employees. Well - the code should be something

Hibernate Criteria Query - nested condition

隐身守侯 提交于 2019-12-04 04:12:00
I can't figure out how to create a query like this with Hibernate Criteria synthax select * from x where x.a = 'abc' and (x.b = 'def' or x.b = 'ghi') Do you have an idea of how to do that? I'm Using Hibernate Restriction static methods but I don't understand how to specify the nested 'or' condition You specific query could be: crit.add(Restrictions.eq("a", "abc")); crit.add(Restrictions.in("b", new String[] { "def", "ghi" }); If you're wondering about ANDs and ORs in general, do this: // Use disjunction() or conjunction() if you need more than 2 expressions Disjunction aOrBOrC = Restrictions

using a ParameterExpression versus a variable in JPA Criteria API

断了今生、忘了曾经 提交于 2019-12-04 00:17:23
When using the JPA Criteria API, what is the advantage of using a ParameterExpression over a variable directly? E.g. when I wish to search for a customer by name in a String variable, I could write something like private List<Customer> findCustomer(String name) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Customer> criteriaQuery = cb.createQuery(Customer.class); Root<Customer> customer = criteriaQuery.from(Customer.class); criteriaQuery.select(customer).where(cb.equal(customer.get("name"), name)); return em.createQuery(criteriaQuery).getResultList(); } With parameters this

JPA Criteria Query API and order by null last

半腔热情 提交于 2019-12-03 21:38:01
My problem is null values must be last order by statement. My code snipshot below. I use javax persistance criteria builder. My query complicated. import javax.persistence.criteria.CriteriaBuilder; public Predicate getSomePredicate() { Predicate predicate = cb.conjunction();.... ...predicate.getExpressions().add(cb.and(cb.or(cb.and(v1, v2), cb.and(s1, s2)))); EOrderByType orderType = EOrderByType.values()[orderBy] ; switch (orderType) { case PRICE: cq.where(predicate).orderBy(cb.asc(root.get("price"))); break; case PRICE_HIGH_TO_LOW: cq.where(predicate).orderBy(cb.desc(root.get("price")));

Hibernate Criteria order by a specific state

烂漫一生 提交于 2019-12-03 17:05:33
Hi In the database, we have a PRSN_ADDRESS table which has many addresses. A user is shown these addresses in a grid. The requirement is show the addresses associated with this user's state first and then show all other states. For example, say the table has 10 records with 5 of the addresses having state as Maryland, 2 from PA and 3 from NJ. Now if the user is associated with Maryland, we need to show all the 10 addresses, but Maryland addresses should show up in the first five and then the other 5 in any order. We are using Hibernate Criteria. I haven't worked on Formulas and not sure if

过滤器模式

假如想象 提交于 2019-12-03 13:24:51
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。 实现 我们将创建一个 Person 对象、 Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列表。 CriteriaPatternDemo ,我们的演示类使用 Criteria 对象,基于各种标准和它们的结合来过滤 Person 对象的列表。 步骤 1 创建一个类,在该类上应用标准。 Person.java public class Person { private String name ; private String gender ; private String maritalStatus ; public Person ( String name , String gender , String maritalStatus ) { this . name = name ; this . gender = gender ; this . maritalStatus = maritalStatus ; } public String getName ( ) { return name ; }

Does NHibernate Criteria API support projections on collection properties?

喜欢而已 提交于 2019-12-03 13:02:09
问题 I need to replicate the following working HQL query using criteria API. session.CreateQuery( "select c " + "from Parent p " + "inner join p.Children c " + "where p.Id = 9 " + "and c.Id = 33") .SetMaxResults(3) .List(); The query selects all the children that satisfy a certain criteria that belong to parents that satisfy another criteria. In my example both criterion are simple Id equalities but they could be anything. For some reason the equivalent criteria API query returns a list with the

Can I call a stored procedure with hibernate criteria?

 ̄綄美尐妖づ 提交于 2019-12-03 12:45:51
Can I use Hibernate criteria to call a stored procudure? Lachlan Roche See Using stored procedures for querying in the reference documentation. Mapped queries are called like this. List employment = sess.getNamedQuery("BigSP") .list(); A mapped query can return entities. <sql-query name="BigSP" callable="true"> <return alias="emp" class="Employment"> <return-property name="employee" column="EMPLOYEE"/> <return-property name="employer" column="EMPLOYER"/> <return-property name="startDate" column="STARTDATE"/> <return-property name="endDate" column="ENDDATE"/> <return-property name="regionCode"

How to create criteria in groovy/grails for nested object?

让人想犯罪 __ 提交于 2019-12-03 11:33:24
I need help on creating hibernate criteria for nested object. For example : class office{ Integer id; OfficeDetails cmdData ; } class OfficeDetails { Integer id; Region region; } class Region { Integer id; Integer regionNum; } Now, from the service class ( officeService) I am trying to pull up all of the offices that matches a certain region as : List<Office> findAllByRegion( Integer regionNumber){ def criteria = { eq ( 'cmdData.region.regionNum', regionNumber ) } def allOfficesInTheRegion = Office.findAll(criteria) return allOfficesInTheRegion } Always getting exception :"org.hibernate

When to use the select clause in the JPA criteria API?

回眸只為那壹抹淺笑 提交于 2019-12-03 10:50:17
问题 Without using CriteriaQuery#select() : public List<Address> getAddressOfManager(String designation, String name, String orderByColumn) { Boolean ascending = false; CriteriaBuilder cb = emanager.getCriteriaBuilder(); CriteriaQuery<Address> cq = cb.createQuery(Address.class); Root<Address> root = cq.from(Address.class); //cq.select(root); <------------- Join<Address, Employee> employeeAddress = root.join(Address_.employee); Join<Employee,Project> employeeProject = employeeAddress.join(Employee_