jpql

jpa fetch join query

拟墨画扇 提交于 2019-12-19 10:26:39
问题 This is how my domain looks: public class Template implements Serializable { private static final long serialVersionUID = 1L; @OneToOne(cascade=CascadeType.ALL) private FieldConfig fieldConfig; } public class FieldConfig implements Serializable { private static final long serialVersionUID = 1L; @OneToMany(cascade= CascadeType.PERSIST) @JoinColumn(name = "fieldConfigId") private Set<Field> fieldSet; } I want to achieve if I load a template from the db that automatically the fieldConfig is

How to do a complex LEFT JOIN condition in JPQL?

℡╲_俬逩灬. 提交于 2019-12-19 10:17:32
问题 I am using JPQL for the model queries from Play Framework. I wonder if JPQL supports "complex" ON condition for LEFT JOIN. In an example I have, there are 2 tables: 'App' - list of applications 'AggregationHistory' - the list of the aggregation records per application, per date. In the model, it has 'app' field representing many-to-one with 'App' (column name 'app_id' in the physical table) Suppose I want to calculate the list of all the apps that do not have a record for a specific date. In

EclipseLink JPQL (Glassfish v3): join fetch syntax problem?

天涯浪子 提交于 2019-12-19 09:38:58
问题 With Hibernate I'm used to do something like the following: select n from NetworkElement n join fetch n.site s where s.active is true However, EclipseLink complains a lot about this: Caused by: Exception [EclipseLink-8024] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing the query [select n from NetworkElement n join fetch n.site s], line 1, column 49: syntax error at [s]. (The query on the

Is there a way to get the count size for a JPA Named Query with a result set?

ε祈祈猫儿з 提交于 2019-12-18 12:14:59
问题 I like the idea of Named Queries in JPA for static queries I'm going to do, but I often want to get the count result for the query as well as a result list from some subset of the query. I'd rather not write two nearly identical NamedQueries. Ideally, what I'd like to have is something like: @NamedQuery(name = "getAccounts", query = "SELECT a FROM Account") . . Query q = em.createNamedQuery("getAccounts"); List r = q.setFirstResult(s).setMaxResults(m).getResultList(); int count = q.getCount()

Using reserved JPQL keywords with JPA

Deadly 提交于 2019-12-18 04:53:20
问题 I have an entity class called "Group" and NetBeans warns me "The entity table name is a reserved Java Persistence QL keyword". A similar case would be the use of reserved SQL keywords. Will this name be escaped? Would the use of a different table name solve the problem @Table(name="otherName"). Or should I rename the class? 回答1: Will this name be escaped? There is nothing in the JPA spec that says so, if your provider does, this is provider specific. Would the use of a different table name

How do I count the number of rows returned by subquery?

强颜欢笑 提交于 2019-12-18 02:27:42
问题 I want to do something like this: select count(*) from (select ...) (As it would be in SQL), but in JPA. Any ideas on how I would do it? 回答1: This should do the trick (If you want to use JPA criteria API): CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Long> query = cb.createQuery(Long.class); Root<Entity> root = query.from(Entity.class); //Selecting the count query.select(cb.count(root)); //Create your search criteria Criteria criteria = ... //Adding search

a new object was found through a relationship that was not marked cascade PERSIST

こ雲淡風輕ζ 提交于 2019-12-17 23:38:01
问题 In trying to get a @OneToMany relationship between Article and HeaderField I probably have the mapping not quite right, resulting in: init: Deleting: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties deps-jar: Updating property file: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties compile: run: DEBUG: nntp: newsrc loading /home/thufir/.newsrc DEBUG: nntp: newsrc load: 1 groups in 31ms [EL Info]: 2012-07-31 02:05:05.677--ServerSession(8979162)--EclipseLink,

How to retrieve only certain fields of an entity in JPQL or HQL? What is the equivalent of ResultSet in JPQL or HQL?

自古美人都是妖i 提交于 2019-12-17 23:06:36
问题 In JPQL, I can retrieve entities by : query = entityManager.createQuery("select c from Category c"); List<Category> categories = query.getResultList(); But, if I wish to retrieve the id and name fields (only) of the Category entity, I need something like the ResultSet object, through which I can say : rs.getString("name") and rs.getString("id") . How to do this through JPQL , without retrieving the entire entity ? Basically, for a how to retrieve information from a query like : select c.id,c

Finding items with a set containing all elements of a given set with jpql

若如初见. 提交于 2019-12-17 20:06:03
问题 I want to find the items that contain all the given tags in their tags set. Here are the simplified classes: @Entity class Item { @ManyToMany var tags: java.util.Set[Tag] = new java.util.HashSet[Tag]() } @Entity class Tag { @ManyToMany(mappedBy="tags") var items: java.util.Set[Item] = new java.util.HashSet[Item] } If I try it like this select distinct i from Item i join i.tags t where t in (:tags) I get the items that contain any of the given tags. That is not surprising, but I want Items

How do I query “older than three minutes” in JPA?

家住魔仙堡 提交于 2019-12-17 19:14:51
问题 Is there a way to put a date range condition referencing the current time in a JPA query (that is compatible across databases)? I can do SELECT m FROM Mail m WHERE m.sentAt < :date but I want to do it without having to bind a parameter (so that this can be configured as part of the named query repository and the date calculation logic does not have to enter the calling code). So instead of :date I need "currentTime minus 3 minutes" as a hard-code literal. 回答1: JPA supports the CURRENT