jpql

month() function and year() function in postgresql through jpa2

这一生的挚爱 提交于 2019-12-11 07:01:58
问题 How to get the month and year of a postgresql date field using jpa2 criteria query or jpql? Has anybody done this? 回答1: Use this: FUNC('MyFunc', a, b, c) Example: SELECT FUNC('to_char', current_date, 'month') 回答2: A few examples, how to extract the month from a date or timestamp in PostgreSQL: select to_char(now(), 'month'); select to_char(now(), 'MM'); select extract('month' from now()); select date_part('month', now()); Just read the manual. 回答3: With JPA criteria API (tested only with

Prevent JPQL query sql injection

限于喜欢 提交于 2019-12-11 06:57:09
问题 I was advised that below query is not safe as parameter :searchFor coming from input field in front end can be used for SQL injection. Plase advise what is the best solution to prevent SQL injection in below code? @Query("SELECT u FROM User u WHERE lower(u.username) LIKE %:searchFor% " + " OR lower(concat(u.firstname, ' ', u.lastname)) LIKE %:searchFor% " + " OR lower(u.email) LIKE %:searchFor%") Page<User> findAllAndSearch(@Param(value = "searchFor") String searchFor, Pageable pageable); I

SQL to JPQL join request

眉间皱痕 提交于 2019-12-11 06:56:23
问题 Hopefully someone here can help me. I have found the request I want to use on sqlDev : SELECT t1.* FROM table1 t1 INNER JOIN ( SELECT ev.ID_AGENCE, MAX(ev.DATE_CREATION) DATE_CREATION FROM table1 ev WHERE ev.ID_AGENCE IN (326,324) GROUP BY ev.ID_AGENCE ) t2 ON t1.ID_AGENCE = t2.ID_AGENCE AND t1.DATE_CREATION = t2.DATE_CREATION order by t1.id_agence; to keep only the closest date in a list, and only one per id (324 and 326 here in my exemple) : 324 22/10/18 324 21/10/18 324 20/10/18 326 10/08

Eager loading a lazy child collection

[亡魂溺海] 提交于 2019-12-11 06:36:43
问题 I have data model similar to this class Office { @ManyToOne @JoinColumn(name = "cafeteria_id") Cafeteria cafeteria; } class Cafeteria { @OneToMany(mappedBy="cafeteria") @LazyCollection(value=LazyCollectionOption.EXTRA) List<Chair> chairs; } class Chair { @ManyToOne @JoinColumn(name = "cafeteria_id") Cafeteria cafeteria; } I have a JPQL query like this select o from Office o where o.cafeteria.someThing = ? Above query works fine but in one case I would like a query which could eagerly load all

What's wrong with my JPQL query?

跟風遠走 提交于 2019-12-11 05:28:35
问题 I am trying to implement join but I am facing error. I have product table and store table. product table references store table through foreign key as shown below: Product.java @Entity public class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long pId; private String model; private String brand; private byte[] image; private Long price; private String currency; private String transmissionType; private String fuelType; @ManyToOne @JoinColumn(name="storeId") private

JPQL Join Fetch on a table with nullable columns

半世苍凉 提交于 2019-12-11 05:23:06
问题 My problem is this; I have an entity that has 2 lists of other entities. When I use a query to select them all like this; select DISTINCT ru FROM RtsResource ru WHERE ru.resourceId= :arg1 I get around 500 hibernate selects and it is incredibly slow. So I tried; select DISTINCT ru FROM RtsResource ru JOIN FETCH ru.projectResources JOIN FETCH ru.resourceSkills WHERE ru.resourceId= :arg1 Which is much faster, but it only selects queries where projectResources or resourceSkills aren't null. Is

JPQL multiple left join with one class

孤者浪人 提交于 2019-12-11 05:16:10
问题 I have following problem with JPQL: One Class has three fields: public class Message{ Integer ownerId; Integer fromUserId; Integer toUserId; //rest of implementation } then I want to search in the database by the user names, but we have 3 users attached to this Message: Owner, Recipient and Sender, so what I did in SQL is: SELECT * FROM message m LEFT JOIN user o ON m.owner_id=o.id LEFT JOIN user fu ON m.from_user_id = fu.id LEFT JOIN user tu ON m.to_user_id = tu.id WHERE o.name like '%foo%'

JPA search String, Long and Boolean

女生的网名这么多〃 提交于 2019-12-11 04:29:23
问题 I have a Spring-Boot app. There's an entity: @Entity @Table(name = "user") public class User { private Long id; private String name; private Long schoolId; private Boolean isActive; // getters and setters } I have a repository: @Repository public interface UserRepositoryPageable extends PagingAndSortingRepository<User, Long> { } I need to make a request to search by schoolId and filter all fields by some string. Something like this: @Query("SELECT u FROM User u " + "WHERE u.schoolId =

Why do subqueries with nested properties in EclipseLink always produce an unnecessary/redundant/duplicate/superfluous join?

旧时模样 提交于 2019-12-11 03:52:03
问题 I wanted to generate the following SQL through EclipseLink (2.6.1) JPA criteria. SELECT zonecharge0_.charge AS col_0_0_ FROM projectdb.zone_charge zonecharge0_ WHERE ( EXISTS ( SELECT country1_.country_id FROM projectdb.country country1_ WHERE country1_.country_id=? and zonecharge0_.zone_id=country1_.zone_id ) ) AND ( zonecharge0_.weight_id IN ( SELECT MAX(weight2_.weight_id) FROM projectdb.weight weight2_ WHERE weight2_.weight BETWEEN 0 AND 60 ) ) Running both of the following criteria and

Rewrite self join to JPQL

假如想象 提交于 2019-12-11 02:55:50
问题 I need to convert this self join to JPQL: SELECT s1.* FROM site AS s1 JOIN (SELECT site_type, MAX(last_update_date) AS LastUpdate FROM site WHERE site.last_update_date > "2011-02-27 16:57:53" GROUP BY site_type) AS s2 ON s1.site_type = s2.site_type AND s1.last_update_date = s2.LastUpdate ORDER BY s1.last_update_date DESC Edit: the solution: SELECT s1 FROM Site s1 WHERE s1.lastUpdateDate = ( SELECT MAX(s2.lastUpdateDate) FROM Site s2 WHERE s1.siteType = s2.siteType) AND s1.lastUpdateDate >