jpql

How to write JPA query where parameter is a set?

一曲冷凌霜 提交于 2019-12-29 08:47:12
问题 Assuming the following class, how do you find a Person with a particular email address? public class Person implements Comparable<Person> { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="id") private long id = 0; @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fetch=FetchType.LAZY) private Set<String> email = new HashSet<String>(); } Is it as simple as doing just this, or is there a proper way? select p from Person p where p.email=:email 回答1

JPQL equivalent of SQL query using unions and selecting constants

泄露秘密 提交于 2019-12-29 07:11:38
问题 I've written a SQL query that basically selects from a number of tables to determine which ones have rows that were created since a particular date. My SQL looks something like this: SELECT widget_type FROM( SELECT 'A' as widget_type FROM widget_a WHERE creation_timestamp > :cutoff UNION SELECT 'B' as widget_type FROM widget_b WHERE creation_timestamp > :cutoff ) types GROUP BY widget_type HAVING count(*)>0 That works well in SQL but I recently found that, while JPA may use unions to perform

JPA JPQL: select items when attribute of item (list/set) contains another item

陌路散爱 提交于 2019-12-29 05:55:27
问题 public class Document extends Model { ... @ManyToMany public Set<User> accessors; ... } I want to select all Documents which accessors contain a certain user. I have just minimal experiences with SQL and no experiences with JPQL. So how to do that? thanks in advance 回答1: SELECT d FROM Document AS d WHERE :user MEMBER OF d.accessors Should be what you need, and it is simpler than joining tables. Just dont forget to use the user as a parameter instead of using its id: query.setParameter("user",

JPQL / QueryDSL: join subquery and get aliased column

▼魔方 西西 提交于 2019-12-29 05:24:05
问题 I'm trying to get an average for a count on a groupBy by joining with a subquery. Don't know if that the right way to go at all but I couldn't anything about subqueries other than the mysema doc. Scenario: How many orders per product did a customer do on average? Meaning: A Customer orders products. So a customer ordered a specific product a number of times (count). What's the average number of orders that customer placed for any product? Might sound a bit hypothetical, in fact it's just part

Is this possible: JPA/Hibernate query with list property in result?

时光毁灭记忆、已成空白 提交于 2019-12-29 04:18:26
问题 In hibernate I want to run this JPQL / HQL query: select new org.test.userDTO( u.id, u.name, u.securityRoles) FROM User u WHERE u.name = :name userDTO class: public class UserDTO { private Integer id; private String name; private List<SecurityRole> securityRoles; public UserDTO(Integer id, String name, List<SecurityRole> securityRoles) { this.id = id; this.name = name; this.securityRoles = securityRoles; } ...getters and setters... } User Entity: @Entity public class User { @id private

multiple use of expression via jpql alias keyword

帅比萌擦擦* 提交于 2019-12-25 10:17:27
问题 I'm using spring data with a postgresql server and i want to perform some GPS-data range queries. This means, given a coordinate i compute on the fly the distance from the entry to the given point and check for a certain range. Since i also want to order my data regarding the distance and additionally i want to retrieve the actual distance too, in sql i would use the AS keyword to compute the expression only once and then use this auxiliary expression in the where and the order by part.

How can I create a Predicate from a HQL query?

邮差的信 提交于 2019-12-25 08:49:23
问题 I have this repository: @Repository public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor<User> { @Query("select u from User u, UserRole ur " + "where u.dep = ur.dep " + "and u.allowed = 1") List<User> getAllowed(); } But I want to change the @Query by a custom Spring Data Specification , in order to call it like: repository.findAll(new Allowed()); So I have added extends JpSpecificationExecutor<User> to my repository and now I'm trying to create the

Taking the difference of temporal fields in JPQL

倾然丶 夕夏残阳落幕 提交于 2019-12-25 07:59:05
问题 I have an entity Event which has fields startDate and endDate . I'd like to select only those events that have at most x days left to the their endDate . I want to do this in JPQL and with only one query. How can I do this? 回答1: JPA does not provide any standard date/time functions. You can use a native SQL query using SQL EXTRACT, Or, if you are using EclipseLink you can use the FUNC JPQL operator to call a database specific function, or use EXTRACT if using EclipseLink 2.4, See, http://wiki

How to do group by substring in Eclipselink?

醉酒当歌 提交于 2019-12-25 05:51:17
问题 I'm trying to group the store by store country and see how many stores there are in each country. The store country happens to be the first 2 characters of the store's code. That means if the store code is "US000010", the country is "US" My entity object: ... public class Store { @column(name = "code") private String code; ... } My JPQL is: SELECT substring(s.code, 0, 2), count(s) FROM Store s GROUP BY substring(s.code, 0, 2) This keeps throwing me ORA-00979: not a GROUP BY expression , which

Why isn't my Entity Table mapped in JPA Hibernate?

别来无恙 提交于 2019-12-25 05:03:22
问题 I have an Entity: @Entity(name = "target_group") public class TargetGroup extends AbstractEntity { private String name; private String description; @ManyToMany(fetch = FetchType.LAZY) private List<Customer> customers = new ArrayList<>(); getter.setter... } And I have a code, to get a list about the groups with the stableId (which is in the Abstract Class): public TargetGroup getTargetGroupByStableId(String stableId) { TargetGroup tg = null; try { Query q = em.createQuery("SELECT tg FROM