criteria-api

Select MAX timestamp with JPA2 Criteria API

岁酱吖の 提交于 2019-12-20 16:23:16
问题 So my entity has: @Column(name="TS", nullable=false) private java.sql.Timestamp timestamp; My generated MetaModel has: public static volatile SingularAttribute<MyEntity,Timestamp> timestamp; I want to select by the Max Timestamp value: Root<MyEntity> root = query.from(MyEntity.class); Expression maxExpression = cb.max(root.get(MyEntity_.timestamp)); But I am not allowed because: max(Expression<N> x) Create an aggregate expression applying the numerical max operation. <N extends java.lang

“Not in” constraint using JPA criteria

﹥>﹥吖頭↗ 提交于 2019-12-20 09:26:59
问题 I am trying to write a NOT IN constraint using JPA Criteria . I've tried something like this: builder.not(builder.in(root.get(property1))); though I know it will not work. In the above syntax, how can I add the collection / list against which property1 that will be checked? 回答1: builder.not(root.get({field_name}).in(seqs)) seqs is collection. 来源: https://stackoverflow.com/questions/5115422/not-in-constraint-using-jpa-criteria

how to use string left function in hql

北城余情 提交于 2019-12-20 02:37:21
问题 I have a sql query like this select column from table where path = left('INPUTSTRING', length(path)); and trying to accomplish it in hql like this, return session.createQuery("from Table where Path = left(:input, length(Path))"). query.setParameter("input", inputPath). .list(); and getting an error like this Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: left near line 1 how to get this done? What is the corresponding string function in hql? Is there a solution for

JPA/Metamodel: Strange (inconsistent ?) example in Sun Docs

寵の児 提交于 2019-12-19 13:48:13
问题 In Sun Online resources, they provide son example about the usage of the Criteria/Metamodel API, but as far as I understand Java, it seems to be impossible to work: CriteriaQuery<Pet> cq = cb.createQuery(Pet.class); Metamodel m = em.getMetamodel(); EntityType<Pet> Pet_ = m.entity(Pet.class); EntityType<Owner> Owner_ = m.entity(Owner.class); Root<Pet> pet = cq.from(Pet.class); Join<Owner, Address> address = cq.join(**Pet_.owners**).join(**Owner_.addresses**); Pet_ is a instance of class

Java Hibernate Criteria select subclass

大城市里の小女人 提交于 2019-12-19 10:33:40
问题 I want to use the Criteria API to select entities by taking the input from a search value. A document can have more recipients. A recipient has many subclasses @Entity public class Document implements Serializable { @OneToMany(mappedBy="document") private List<Recipient> recipients = new ArrayList<Recipient>(); @Entity public class RecipientAccount extends Recipient { String name; How can i select all documents which have a ReciepientAccount with a certain name? I need to do search all

Eclipselink extend JOIN clause

╄→尐↘猪︶ㄣ 提交于 2019-12-19 08:27:09
问题 The current code: CriteriaQuery criteriaQuery = cb.createQuery(MinutisPreke.class); Root<MinutisPreke> from = criteriaQuery.from(MinutisPreke.class); Join<LankomumasDiena, MinutisPreke> ld = from.join("lankomumasDiena", JoinType.LEFT); cb.and(cb.equal(ld.get("intervalas"), 7)); generates the following query: SELECT COUNT(t0.pr_id) FROM preke AS t0 LEFT OUTER JOIN lankomumas AS t1 ON (t1.pr_id = t0.pr_id) WHERE (t1.intervalas = 7) How to add statement in the LEFT OUTER JOIN ON clause using

How to use CriteriaQuery SUM of custom operation on some cells?

泄露秘密 提交于 2019-12-19 04:04:26
问题 Consider you have table T, with fields A and B. With regular SQL, I could do this: SELECT SUM(A * (100.0 - B) / 100.0) AS D FROM T; And I would get exactly what I expect. However, I'm not sure how to do it with CriteriaQuery. I know how to do sum over 1 field, but not how to do sum over some math expression over multiple fields in a row. 回答1: The CriteriaBuilder interface provides the following arithmetic functions: addition: sum(a, b) substraction: diff(a, b) multiplication: prod(a, b)

Hibernate Criteria API - how to order by collection size?

让人想犯罪 __ 提交于 2019-12-19 03:45:07
问题 Assuming I have classes User and UserGroup. There is an optional 1-many group to user association and the association is mapped from both sides (the UserGroup side via a property called "members", which is a HashSet, the User side via property "group"). Using the Criteria API, how can I query for all groups, sorted by count of group members? (Edit) I should have pointed out when I asked this that pagination was being performed in the SQL, so the ordering should also be performed in the SQL if

Dynamic JPA 2.0 query using Criteria API

依然范特西╮ 提交于 2019-12-18 15:34:19
问题 I am a bit stucked constructing a dynamic query using the CriteriaBuilder of JPA 2.0. I have quite a common use case I guess: User supplies a arbitrary amount of search parameters X to be and / or concatenated: like : select e from Foo where (name = X1 or name = X2 .. or name = Xn ) The Method or of CriteriaBuilder is not dynamic: Predicate or(Predicate... restrictions) Ideas? Samples? 回答1: In your case, I would rather use Expression#in(Collection) to avoid having to loop and to build a

Correct usage of JPA criteria API, Predicates and where method of CriteriaQuery

▼魔方 西西 提交于 2019-12-18 11:35:38
问题 I am trying to test a JPA repository. Here is my client/test code: @Test public void testFindBoitesByMultiFieldWithIdentifiantSet() { BoiteDataOnDemand dod = new BoiteDataOnDemand(); Boite boite = dod.getSpecificBoite(0); boite.setIdentifiant("theIdentifiant"); boiteRepository.save(boite); assertEquals(10, Boite.countBoites()); BoiteQueryInfo boiteQueryInfo = new BoiteQueryInfo(); boiteQueryInfo.setIdentifiant("theIdentifiant"); List<Boite> boites = boiteRepository.findBoitesByMultiField