criteria-api

Union All and Sum with JPA CriteriaBuilder

大兔子大兔子 提交于 2019-12-03 03:35:55
I am trying to convert a native SQL query to use the Criteria API in JPA 2.0. I have found a lot of Criteria API examples on Google, but I am having a really hard time putting all of the pieces together. I'm hoping that a more experienced person will be able to help me out. The native query looks like this: select sum(amount) from firstTable, secondTable where firstTable.id = secondTable.id and amount <> 0 and firstTable.id = ? union all select sum(amount) from firstTable, thirdTable where firstTable.id = thirdTable.id and amount <> 0 and firstTable.id = ? The original query result set was

Criteria Builder Create new Object In Select Statement

久未见 提交于 2019-12-03 00:36:17
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! Yes, Criteria API does have have construct similar to JPQL constructor expressions. Result class is set via construct method in CriteriaBuilder. Your JPQL query expressed as an criteria query is: CriteriaBuilder cb... CriteriaQuery<EmpMenu> q = cb.createQuery

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

非 Y 不嫁゛ 提交于 2019-12-03 00:25:49
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_.project); cq.where(cb.or(cb.equal(employeeProject.get(Project_.name), name),cb.equal(employeeAddress

Criteria SpatialRestrictions.IsWithinDistance NHibernate.Spatial

北战南征 提交于 2019-12-02 21:31:53
Has anyone implemented this, or know if it would be difficult to implement this/have any pointers? public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance) { // TODO: Implement throw new NotImplementedException(); } from NHibernate.Spatial.Criterion.SpatialRestrictions I can use "where NHSP.Distance(PROPERTY, :point)" in hql. But want to combine this query with my existing Criteria query. for the moment I'm creating a rough polygon, and using criteria.Add(SpatialRestrictions.Intersects("PROPERTY", myPolygon)); EDIT Got a prototype

What does an underscore concatenated to a class name mean?

假装没事ソ 提交于 2019-12-02 20:07:53
I was reading the "Dynamic, typesafe queries in JPA 2.0" article and stumbled upon this example: EntityManager em = ... CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaQuery<Person> c = qb.createQuery(Person.class); Root<Person> p = c.from(Person.class); Predicate condition = qb.gt(p.get(Person_.age), 20); // ^^ --- this one c.where(condition); TypedQuery<Person> q = em.createQuery(c); List<Person> result = q.getResultList(); I was wondering, what exactly does the underscore here mean? Since an underscore it is a valid part of a classname I don't understand why this can be used in JPA. I

“Not in” constraint using JPA criteria

不想你离开。 提交于 2019-12-02 20:07:24
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? jYeory builder.not(root.get({field_name}).in(seqs)) seqs is collection. 来源: https://stackoverflow.com/questions/5115422/not-in-constraint-using-jpa-criteria

Criteria query for unidirectional one-to-many relationship

喜欢而已 提交于 2019-12-02 06:52:15
问题 So, I have the following entities: @Entity public class Supplier { @Column(name = "SUPPLIERID") private BigInteger supplierId; @OneToMany @JoinColumn(name = "ID_SUPP", foreignKey = @ForeignKey(name = "fk_POIS_SUPP")) private List<POS> posList; ... } @Entity public class POS { @Column(name = "POSID") private BigInteger posId } So, POS does not have a reference to Supplier , which means that we have a unidirectional one-to-many relationship. I need to look for a POS by posId and supplierId .

Criteria query for unidirectional one-to-many relationship

依然范特西╮ 提交于 2019-12-02 06:25:48
So, I have the following entities: @Entity public class Supplier { @Column(name = "SUPPLIERID") private BigInteger supplierId; @OneToMany @JoinColumn(name = "ID_SUPP", foreignKey = @ForeignKey(name = "fk_POIS_SUPP")) private List<POS> posList; ... } @Entity public class POS { @Column(name = "POSID") private BigInteger posId } So, POS does not have a reference to Supplier , which means that we have a unidirectional one-to-many relationship. I need to look for a POS by posId and supplierId . That is, find a supplier with the specified supplierId and then find a pos in the supplier's list of pos

how to use string left function in hql

China☆狼群 提交于 2019-12-01 22:41:45
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 this using criteria query apis? Yes, left() is not supported by the MySQLDialect . See the list of HQL

Create a JPA Criteria fully dynamically

谁都会走 提交于 2019-12-01 18:20:00
问题 Usually I'm a Hibernate user and for my new project we use JPA 2.0. My DAO receives a Container with a generic. public class Container<T> { private String fieldId; // example "id" private T value; // example new Long(100) T is a Long private String operation; // example ">" // getter/setter } The following lines won't compile: if (">".equals(container.getOperation()) { criteriaBuilder.greaterThan(root.get(container.getFieldId()), container.getValue()); } Because I must specify the type like