jpql

Query @ElementCollection in an @Entity with a Collection

你离开我真会死。 提交于 2019-12-12 04:18:07
问题 Let's say that I have an @Entity (JPA 2.0) @Entity class Entry { @ElementCollection Set<String> users = new HashSet(); @ElementCollection Set<String> groups = new HashSet(); } How can I query it to find all Entries where users is "John" and groups are "Foo", "Bar" and "FooBar"? 回答1: Try using something like this @Query("SELECT DISTINCT e FROM Entry e WHERE :user MEMBER OF e.users AND :groups in e.groups") List<Entry> yourQuery(@Param("user") String user, @Param("groups") List<String> groups);

Query dsl returning duplicate records on @one to many association(leftJoin vs leftJoin.fetch vs fetchAll)

爷,独闯天下 提交于 2019-12-12 02:50:37
问题 Here is my scenario: i have person entity which looks like below. @Entity public class Person{ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Set<PhoneNumber> phoneNumbers = new HashSet<>(0); @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "AGENCY_ID") private Agency agency; I am unable to retrieve correct data,when i query for persons. Problems i have : 1. duplicate records. 2. person with no agency not returning . 3. Bad performance Here is what i tried, and see

Does EclipseLink support ORDER BY FIELD(field, :values)

放肆的年华 提交于 2019-12-12 02:34:16
问题 MySQL supports queries such as: SELECT id FROM users WHERE id IN(3,4,8,1) ORDER BY FIELD(id, 3,4,8,1); Does EclipseLink support this query (without reverting to a native query)? When I try the following: Select id from User user where user.id in :ids ORDER BY FIELD(user.id, :ids) I receive the error: Syntax error parsing the query ... line 1, column 98: unexpected token [(]. 回答1: ORDER BY FIELD(field, :values) is not supported by JPA (JQL) or EclipseLink. JPA does provide a mechanism to

java hibernate query.list returns empty list

杀马特。学长 韩版系。学妹 提交于 2019-12-12 00:58:33
问题 I am using hibernate with JPA annotations and Jboss transaction manager I build the session factory open up a session and create a query when i use query.list it always returns me an empty list any idea? 回答1: Few suggestions: Enable configuration parameter show-sql in persistence.xml . Try this, i.e. <property name="hibernate.show.sql" value="true" /> Furthermore, it is better to format that using, <property name="hibernate.format_sql" value="true" /> And then try to run the same query in

JPQL - Update with Multiple where conditions

泄露秘密 提交于 2019-12-12 00:23:44
问题 Does JPQL support the following Update? Update Person p set p.name = :name_1 where p.id = :id_1, p.name = :name_2 where p.id = :id_2, p.name = :name_3 where p.id = :id_3 If not, are there any alternatives? I tried it myself. But createQuery returned null. 回答1: Case expression is supported in JPA 2.0. I have provided pseudo-code, can make modifications accordingly. You can try below JPQL query using CASE . Update Person p set p.name = CASE WHEN (p.id = :id1) THEN :name_1 WHEN (p.id = :id2)

criteria query: indistinct result lists

你说的曾经没有我的故事 提交于 2019-12-11 22:55:58
问题 I have 2 little problems. Let's say I have an entity class called MyEntity . This class has two kinds of properties. Therefore I have two different class Property1 and Property2 which are also entity classes. There are bidirectional relations betweens MyEntity and the property classes, especially Property1 has an attribute List<MyEntity> owningEntities and Property2 has the attribute MyEntity owningEntity . Now I have the following query: SELECT e FROM Property1 p JOIN p.owningEntities e

Error with JPQL

牧云@^-^@ 提交于 2019-12-11 20:49:02
问题 I have a problem with JPA and JPQL. I have the next query: String query="SELECT c from Cliente c"; Query quer=em.createQuery("Select c from Cliente c"); List<Cliente> lista= quer.getResultList(); When I execute that, i hace the following error : [java] Exception in thread "main" java.lang.NoClassDefFoundError: antlr/RecognitionException [java] at org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:59) [java] at org.hibernate.engine

java persistance query language limit resultset

旧时模样 提交于 2019-12-11 20:00:43
问题 Suppose I have following rows in a table "mytable" id 1 2 3 ... ... 500 and my query is like select m from mytable m where m.id < 300 how can I get following output in the same order? 201 202 ... ... ... 299 I am using setMaxResult(100) but it returns me output like this 1 2 ... ... 100 any advice? 回答1: You can use setFirstResult to define the start index. Together with setMaxResult you can get a specific range. 回答2: String ql = "select m from mytable m where m.id < 300 order by m.id"; Query

Spring Data Jpa(JPQL) inner queries support

时间秒杀一切 提交于 2019-12-11 18:48:24
问题 I have written native sql query with spring data jpa. Now i want to convert it to the jpql query with spring data jpa only. My Query is SELECT sample.api_name, sample.hitcount, r.unit_rate*sample.hitcount AS amnt FROM (SELECT u.api_name AS api_name, u.tenant_id, u.count AS hitcount FROM tableA u WHERE u.tenant_id = :tenant AND u.time_stamp BETWEEN :dateFrom AND :dateTo GROUP BY u.api_name, u.tenant_id) AS sample LEFT JOIN tableB r ON sample.api_name = r.api_name AND sample.tenant_id =r.tenant

Spring JPA repository JPQL query using a collection of objects

↘锁芯ラ 提交于 2019-12-11 16:53:13
问题 let's say I have the next entity or object: class Person { BigInteger cardNumber; String name; int age; Address address; } Then, I have a List<Person> and I want to find the youngest person based on the cardNumber and name . So, I'd like to write a query similar to this: @Query("SELECT p FROM Person p WHERE p.cardNumber = :myPerson.cardNumber AND p.name LIKE :myPerson.name ORDER BY p.age ASC") public List<Person> find(@param("myPerson") List<Person> personList); My problem is that I'm not so