jpql

Suqueries in select clause with JPA

白昼怎懂夜的黑 提交于 2019-12-13 15:22:58
问题 I need to execute a subquery in a select clause with Apache Openjpa 2. Does JPA support subqueries in SELECT clause? My Query is something like this: SELECT t.date, t.value, (SELECT COUNT(DISTINCT t2.value2) FROM table t2 WHERE t2.date = t.date) FROM table t WHERE ... When I execute my query, I get a class cast exception: Exception in thread "main" <openjpa-2.1.1-SNAPSHOT-r422266:1141200 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: at org.apache.openjpa.kernel

Does the JPQL avg aggregate function work with Integers?

泄露秘密 提交于 2019-12-13 13:11:27
问题 I have a JPA 2 Entity named Surgery . It has a member named transfusionUnits that is an Integer . There are two entries in the database. Executing this JPQL statement: Select s.transfusionUnits from Surgery s produces the expected result: 2 3 The following statement produces the expected answer of 5 : Select sum(s.transfusionUnits) from Surgery s I expect the answer of the following statement to be 2.5 , but it returns 2.0 instead. Select avg(s.transfusionUnits) from Surgery s If I execute

How to create Predicate BooleanExpression for many to many relations in QueryDSL

和自甴很熟 提交于 2019-12-13 08:12:33
问题 How can inner join be done on Many to Many relations using Predicate BooleanExpression? I have 2 entities public class A { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST}) @JoinTable(name = "a_b_maps", joinColumns = @JoinColumn(name = "a_id", nullable = false,referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "b_id",

Criteria Query select join

倾然丶 夕夏残阳落幕 提交于 2019-12-13 07:14:03
问题 I need help to convert this JPQL query in Criteria Query: SELECT u.documentList FROM Unit u WHERE u.id = :id this is what i have tried: CriteriaQuery<Document> query = builder.createQuery(Document.class); Root<Unit> root = query.from(Unit.class); Join<Unit, Document> join = root.join(Unit_.documentList); query.select(join); query.where(builder.equal(root.get(AbstractEntity_.id), entity.getId())); executing this query result in a complex SQL query that returns an empty list. IMO this should

JPA/MySQL - Whats wrong with this JPQL query?

廉价感情. 提交于 2019-12-13 06:22:42
问题 I have entity Post : @Entity @Table(name = "post") @NamedQueries({ @NamedQuery(name = "getNewestPosts", query = "SELECT p FROM Post p ORDER BY p.date DESC"), // getting resultList ordered by date @NamedQuery(name = "getMostVisitedPosts", query = "SELECT p FROM Post p ORDER BY p.visitors DESC"), // ordered by most visited @NamedQuery(name = "getMostCommentedPosts", query = "SELECT p FROM Post p ORDER BY SIZE(p.comments) DESC") }) public class Post implements Serializable { @Id @GeneratedValue

Select subset of the list contained in an entity

爷,独闯天下 提交于 2019-12-13 06:02:17
问题 Say I want to get all rows of the MyEntity that have an id lower than 10. This entity contains a list of Another entity. I would like this list to be fetched only by a subset of the listAnother . This subset containing only Another where the user contained in it is a specific one. Basically in SQL it would be like this : SELECT * FROM myentity m LEFT JOIN another a ON m.idTable=a.my_entity AND a.user = "test1" WHERE m.idTable < 10; I didn't manage however to translate this query to jpql. My

How can I test a boolean parameter in a NamedQuery with JPA?

一个人想着一个人 提交于 2019-12-13 03:17:54
问题 How can I tell JPA (EclipseLink 2.4) to check a boolean IN parameter whether it is true or false? SELECT e FROM MyEntity e WHERE :inParam = TRUE AND e.x = 'bla' or SELECT e FROM MyEntity e WHERE :inParam = true AND e.x = 'bla' Doing it as above results in an exception that TRUE wouldn't be a valid value for inParam . 来源: https://stackoverflow.com/questions/29033065/how-can-i-test-a-boolean-parameter-in-a-namedquery-with-jpa

JP QL - Filtering result in a One To Many relationship

一个人想着一个人 提交于 2019-12-13 00:33:16
问题 I am stuck trying to construct a JPQL query and was hoping someone with more JPA experience than mine could help. Consider the following two entities: class Author{ String name @OneToMany(mappedBy="author") Set<Book> books } class Book{ String title Boolean inPrint @ManyToOne Author author } If I want to return a specific Author (by name) and eagerly fetch (ie LEFT JOIN FETCH) the books where the Book.inPrint flag is true, how would I express that in JPQL? 回答1: SELECT a FROM Author a LEFT

JPA query with @ManyToMany relationship and no navigation

时光毁灭记忆、已成空白 提交于 2019-12-13 00:09:49
问题 Similar to this post, I have these (almost the same) classes: public class Project { @ManyToMany private Set<Person> resources; // get and set of resources } public class Person { } The difference is that my properties are private (using beans as entities). The question is: how would I create a query to return all projects of a determined person (in JPQL and/or using CriteriaQuery)? I found all these other similar questions, but none helped me, because all of them rely on the navigation from

JPA/JPQL: AS identifier disallowed in SELECT clause

对着背影说爱祢 提交于 2019-12-12 18:16:52
问题 I have a pretty complex JPQL query of the form SELECT NEW com.domain.project.view.StandingsStatLine( ro.id AS rid , cl.name AS team , te.ordinalNbr + 1 AS nr , pa.wasWithdrawn AS wd , SUM(CASE WHEN paf.wasWithdrawn = FALSE AND paa.wasWithdrawn = FALSE AND scf.finalScore IS NOT NULL THEN 1 ELSE 0 END) AS g , SUM(CASE WHEN paf.wasWithdrawn = FALSE AND paa.wasWithdrawn = FALSE AND scf.finalScore > sca.finalScore THEN 1 ELSE 0 END) AS w , SUM(CASE WHEN paf.wasWithdrawn = FALSE AND paa