criteria-api

Criteria Builder Create new Object In Select Statement

五迷三道 提交于 2019-12-04 08:06:49
问题 I was wondering if it's possible to create such query like : em.createQuery( "SELECT NEW EmpMenu(p.name, p.department.name) " + "FROM Project p ").getResultList(); also is it possible to do it via Specification: public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { return ???; } Thanks in advance! 回答1: Yes, Criteria API does have have construct similar to JPQL constructor expressions. Result class is set via construct method in CriteriaBuilder. Your JPQL

JPA2 Criteria API runtime cast from varchar(25) to decimal

时光总嘲笑我的痴心妄想 提交于 2019-12-04 07:30:38
So I've seen all threads on stack overflow on similar subjects but I've found no solution to my problem. I'm trying to create a Criteria query and I get this SQL (1st SQL, simplified): SELECT latitude FROM stations WHERE (ABS(latitude - 45.893227) <= 0.2) but it gives me this error: java.sql.SQLDataException: The resulting value is outside the range for the data type DECIMAL/NUMERIC(31,31). That's because my latitude is of type varchar(25). So this fixes it (2nd SQL): SELECT latitude FROM stations WHERE (ABS(CAST(latitude AS DECIMAL) - 45.893227) <= 0.2) But now I need to do it in Criteria

How can I express joining to a grouped subquery using NHibernate?

筅森魡賤 提交于 2019-12-04 06:51:06
I'm trying to express a SQL query using NHibernate's Criteria API, and I'm running into difficulty because I'm thinking in a database-centric way while NHibernate is object-centric. SQL (works great): select outerT.id, outerT.col1, outerT.col2, outerT.col3 from tbl outerT inner join (select max(innerT.id) from tbl innerT group by innerT.col1) grpT on outerT.id = grpT.id Essentially, this is a self-join of a table against a subset of itself. I suppose I could try turning the self-join into a restriction: select outerT.id, outerT.col1, outerT.col2, outerT.col3 from tbl outerT where outerT.id in

JPA criteria query, order on class

我们两清 提交于 2019-12-04 04:12:55
Is there a way with JPA criteria queries to order on class ? Imagine the following domain objects: abstract class Hobby { ... } class Coding extends Hobby { ... } class Gaming extends Hobby { ... } Using regular QL I was able to do from Hobby h order by h.class But when I apply the same logic on a criteria query, the runtime exception "unknown attribute" occurs. CriteriaQuery<Hobby> criteriaQuery = builder.createQuery(Hobby.class); Root<Hobby> hobbyRoot = criteriaQuery.from(Hobby.class); criteriaQuery.orderBy(builder.asc(hobbyRoot.get("class")); List<Hobby> hobbies = entityManager.createQuery

How to use key of MAP in Criteria Query?

隐身守侯 提交于 2019-12-04 03:44:44
I have a Bean like this Class TestA { Map<String,TestB> testBMap; } Class TestB { String data; ... } I want to fetch the TestA data along with the map testBMap where key ='test1' . How can i do this using Hibernate. The key must be the value of one of the persistent fields of TestB (let's says this field is names "foo"), so this code should work : Criteria criteria = session.createCriteria(TestA.class, "a"); criteria.createAlias("a.testBMap", "b"); criteria.add(Restrictions.eq("b.foo", "test1")); criteria.setFetchMode("a.testBMap", FetchMode.JOIN); return criteria.list(); YoMan78 In my case

JPA Criteria Query - How to implement Join on two tables to get desired result in single Query

青春壹個敷衍的年華 提交于 2019-12-04 02:21:48
问题 I have 2 classes mapped with db Tables. Composite Primary Key Class : @Embeddable public class Pk implements Serializable, Cloneable { @Column(name = "dataId") private String dataId; @Column(name = "occurrenceTime") private Timestamp occurrenceTime; public String getDataId() { return dataId; } public Pk setDataId(String dataId) { this.dataId = dataId; return this; } public Timestamp getOccurrenceTime() { return occurrenceTime; } public Pk setOccurrenceTime(Timestamp occurrenceTime) { this

Criteria/aggregation: search for all complete documents whose array field has element with latest given status

陌路散爱 提交于 2019-12-04 02:19:42
问题 I have document structure like this - { "_id" : "11223344", "orderId" : "00001041", "key" : "56h68ab4c876dbe1cd0b1ee" "status" : [ { "updatedTimeStamp" : ISODate("2017-06-01T16:05:42.737Z"), "orderStatus" : "INITIATED" }, { "updatedTimeStamp" : ISODate("2017-06-01T16:05:42.737Z"), "orderStatus" : "CONFIRM_PAYMENT" }, { "updatedTimeStamp" : ISODate("2017-06-01T16:07:36.797Z"), "orderStatus" : "RESTAURENT_CONFIRMATION_PENDING" }, { "updatedTimeStamp" : ISODate("2017-06-01T16:20:36.798Z"),

JPA Criteria Query over an entity hierarchy using single table inheritance

馋奶兔 提交于 2019-12-04 00:53:59
问题 Say I have the following entities: @Entity @Inheritance(strategy = SINGLE_TABLE) @DiscriminatorColumn(name = "type") public abstract class BaseEntity { private Date someDate; private Date otherDate; private boolean flag; } @Entity @DiscriminatorValue("entity1") public class Entity1 extends BaseEntity { private String someProperty; } @Entity @DiscriminatorValue("entity2") public class Entity2 extends BaseEntity { private String otherProperty; } I'm trying to build a criteria query that returns

How to query for entities by their collection value

北慕城南 提交于 2019-12-04 00:46:11
问题 I'm using jpa and I have the following entity: @Entity @Table(name="favorites_folders") public class FavoritesFolder { private static final long serialVersionUID = 1L; @Id private String id; @NotNull @Size(min = 1, max = 50) public String name; @ElementCollection(fetch = FetchType.LAZY) @CollectionTable( name="favorites_products", joinColumns=@JoinColumn(name="folder_id") ) @Column(name="product_id") @NotNull private Set<String> productsIds = new HashSet<String>(); } What I want to do is to

Write HQL clause using Hibernate Criteria API

我与影子孤独终老i 提交于 2019-12-03 16:09:14
I want to write a method that returns a list of last added objects grouped by field 'serviceId'. The following HQL works, but I want to write this using Criteria API: FROM Notification WHERE date IN (SELECT MAX(date) FROM Notification GROUP BY serviceId) ORDER BY date ASC Something like this: Criteria crit = session.createCriteria(Notification.class); crit.add(Restrictions.in("date", <MAX dates>)); criteria.addOrder(Order.desc("date")); Thanks in advance. EDIT: Now I need a similar query that works using eclipselink API =/ Basically, I need the last N rows (max date), which status is one of