criteria

Complex queries with JPA Criteria builder

旧巷老猫 提交于 2019-11-28 03:52:07
Can someone suggest me how to build up the following query using JPA Criteria builder API? SELECT id,status,created_at from transactions where status='1' and currency='USD' and appId='123' order by id It's better if I can find a solution which creates dynamically based on the parameters given as a Map<String,String> using metamodel classes or any other way. It's like this (without metamodel): Map<String, Object> params = ...; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<Transaction> r = cq.from(Transaction.class); Predicate p= cb

Hibernate Query By Example and Projections

 ̄綄美尐妖づ 提交于 2019-11-28 03:07:10
To make it short: hibernate doesn't support projections and query by example? I found this post: The code is this: User usr = new User(); usr.setCity = 'TEST'; getCurrentSession().createCriteria(User.class) .setProjection( Projections.distinct( Projections.projectionList() .add( Projections.property("name"), "name") .add( Projections.property("city"), "city"))) .add( Example.create(usr)) Like the other poster said, The generated sql keeps having a where class refering to just y0_= ? instead of this_.city . I already tried several approaches, and searched the issue tracker but found nothing

How to query on a month for a date with Hibernate criteria

前提是你 提交于 2019-11-28 01:30:08
问题 I need to use criteria to query a database. The data I'm searching has a date, say 'startDate' and I have a month say 0 for January, I need to extract all data where startDate has month = 0; in sql I'd use something like 'where month(startDate) = 0' but I don't know how to do this with hibernate criteria and if it's possible at all. Can you help me? Thank you guys. Luca. 回答1: With criteria, I think you'll have to write your own expression class. Something like this should work (not tested,

Complex Hibernate Projections

自古美人都是妖i 提交于 2019-11-28 01:10:54
问题 I want to ask, it is possible that I create query projections and criterion for more than one level deep? I have 2 model classes: @Entity @Table(name = "person") public class Person implements Serializable { @Id @GeneratedValue private int personID; private double valueDouble; private int valueInt; private String name; @OneToOne(cascade = {CascadeType.ALL}, orphanRemoval = true) @JoinColumn(name="wifeId") private Wife wife; /* * Setter Getter */ } @Entity @Table(name = "wife") public class

NHibernate creates proxy via session.Load(), but not via Linq or Criteria API

醉酒当歌 提交于 2019-11-27 23:56:08
问题 I have an odd problem in my current project. Lazy loading for queries does not work. When I query a list, nhibernate fetches all associations separately. I extracted small parts of it and put it into a separate solution. Basically what I've got now, is a Account-Table and a AccountSync-Table. Both have an ID and a URL, while the ID is just a db-guid. My classes are: public class HippoAccount { public virtual Guid Id { get; set; } public virtual string Url { get; set; } public virtual

设计模式-过滤模式

别等时光非礼了梦想. 提交于 2019-11-27 22:16:53
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。 下面创建了CriteriaMale、CriteriaFemale、CriteriaSingle、AndCriteria、OrCriteria都实现了Criteria接口并重写了meetCriteria方法,在Client中可以根据需要过滤出所需要的数据 1.创建Criteria接口和Person实体类 package com.design.demo.filter; /** * @author: GuanBin * @date: Created in 下午11:11 2019/8/18 */ 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 =

How to return an entity with chosen columns using Criteria

雨燕双飞 提交于 2019-11-27 20:17:50
问题 I'm really new with Hibernate. I want a List<User> using hibernate criteria, but only with fields User id and name filled up. Is that possible? Something like the query shown below: SELECT user.id, user.name FROM user Regards. 回答1: This is exactly what projections are for. Here is an example: Criteria cr = session.createCriteria(User.class) .setProjection(Projections.projectionList() .add(Projections.property("id"), "id") .add(Projections.property("Name"), "Name")) .setResultTransformer

Using hibernate criteria, is there a way to escape special characters?

风格不统一 提交于 2019-11-27 20:12:08
For this question, we want to avoid having to write a special query since the query would have to be different across multiple databases. Using only hibernate criteria, we want to be able to escape special characters. This situation is the reason for needing the ability to escape special characters: Assume that we have table 'foo' in the database. Table 'foo' contains only 1 field, called 'name'. The 'name' field can contain characters that may be considered special in a database. Two examples of such a name are 'name_1' and 'name%1'. Both the '_' and '%' are special characters, at least in

Querying ManyToMany relationship with Hibernate Criteria

时间秒杀一切 提交于 2019-11-27 19:54:27
I'm not sure how to describe this problem, so I think an example is the best way to ask my question: I have two tables with a manyToMany relationship: DriversLicence <-> LicenceClass LicenceClass is things like "Car", "Motorbike", and "Medium Rigid". Using Hibernate Criteria, how can I find all licences that have both "Car" and "Motorbike" LicenceClasses? UPDATE 12/11/2008 I have discovered that this can easily be achieved by using a custom ResultTransformer. However the problem is that a result transformer only gets applied AFTER the query returns its results, it does not actually become part

Hibernate Criteria and multiple join

与世无争的帅哥 提交于 2019-11-27 19:10:09
is possible with Hibernate criteria do it? select A.something, B.something, C.something, D.something from A JOIN B on A.id = B.id_fk JOIN C ON B.id = C.id_fk JOIN D ON C.id = D.id_fk; I have got exactly the same problem, and was able to resolve it like this: return criteria.createCriteria(A.class) .createCriteria("b", "join_between_a_b") .createCriteria("c", "join_between_b_c") .createCriteria("d", "join_between_c_d") .add(Restrictions.eq("some_field_of_D", someValue)); Note: "b" , "c" and "d" in code above refer to attribute names in A , B and C classes, correspondingly (class A has attribute