nativequery

Getting generated identifier for JPA native insert query

末鹿安然 提交于 2019-12-24 03:39:27
问题 For performance reasons I need to use some native queries to insert new objects into a database. I have a very sophisticated JPA persistence layer which manages my usual entity reading and writing and I would like to use this persistence layer to run the native queries. I know I need the methods EntityManager#createNativeQuery(String) and Query#executeUpdate to achieve my goal. My current problem is that I would also like to get the generated identifier of the newly inserted row. I know this

Refactor native query to JPQL query

南笙酒味 提交于 2019-12-23 05:25:12
问题 Suppose we have the following table, named as 'documents': id | createDate | createBy | updateDate | updateBy -------------------------------------------------- (various rows here) the two *date columns are timestamps while the other are all strings (even the id ) Currently I have the following native query used in a Spring Repository: select COUNT(distinct(u.user_working_total)) from ( select distinct(createBy) user_working_total from documents where createDate >= :startDate and createDate <

How to replace table name with value from parameter while using Spring Data JPA nativeQuery

给你一囗甜甜゛ 提交于 2019-12-17 17:22:49
问题 like this: public interface XXXRepository extends CrudRepository<XXX, Integer> { @Query(value = "select * from ?1 where ...", nativeQuery = true) List<XXX> findByXXX(String tableName, ...);} It gives MYSQL syntax error with upon codes. The syntax error shows that the table name in the SQL is surrounded with "'". 回答1: This is not possible. Parameters are only allowed in the where clause. 回答2: You can use entitymanger in jpa project. @Autowired EntityManager entityManager; entityManager

Retrieving data from native query and conversion

筅森魡賤 提交于 2019-12-11 13:29:14
问题 I have following query @Override public List<PlayerDetails> testPlayerQuerry() { return copyPlayersToDetails(em.createNativeQuery("select player_name from player p\n" + "join Player_Game pg on p.player_id = pg.fk_player_id\n" + "join Game g on pg.fk_game_id = g.game_id\n" + "where g.game_id = 2").getResultList()); } which as far as I am aware it return list of String? due to the error I am getting Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to entities.Player at

Complex criteria query: using JPA instead of NativeQuery

[亡魂溺海] 提交于 2019-12-11 02:53:59
问题 I've in my Java EE project this NativeQuery for MySQL: SELECT *, ROUND((price_2-price_1)*100/price_1,2) AS varprice_1, ROUND((quantity_2-quantity_1)*100/quantity_1,2) AS varcant_1, ROUND((price_3-price_2)*100/price_2,2) AS varprice_2, ROUND((quantity_3-quantity_2)*100/quantity_2,2) AS varcant_2, 1 FROM ( SELECT c.id_customer AS id_customer, c.name AS customer, r.id_rep AS id_rep, r.descr AS rep, a.id_article AS id_article, a.name AS article, ROUND(SUM(if(docdate BETWEEN '2013-06-30' AND '2013

NativeQuery Spring Data return object

青春壹個敷衍的年華 提交于 2019-12-10 15:44:01
问题 I need to implement a query in Spring Data like this :- Select User.name, sum(Activity.minutes) From User, Activity, ActivityStatus Where User.id = ActivityStatus.userId And Activity.id = ActivityStatus.activityId AND ActivityStatus = "COMPLETED" GROUP BY user.name; So i need to join 3 tables, therefore I have to use @Query with nativeQuery = true ( correct me if I'm wrong here ) And so my Repository method looks like this :- @Query(value = "Select User.name, sum(Activity.minutes) as total

Set list parameter to native query

风格不统一 提交于 2019-12-10 13:04:39
问题 I would like to set parameter to a native query, javax.persistence.EntityManager.createNativeQuery Something like that Query query = em.createNativeQuery("SELECT * FROM TABLE_A a WHERE a.name IN ?"); List<String> paramList = new ArrayList<String>(); paramList.add("firstValue"); paramList.add("secondValue"); query.setParameter(1, paramList); Trying this query result in Exception: Caused by: org.eclipse.persistence.exceptions.DatabaseException: Internal Exception: com.mysql.jdbc.exceptions

How to use the Postgres any-clause with JPA/Hibernate native queries (array parameters)

老子叫甜甜 提交于 2019-12-09 03:26:24
问题 So we've got a whole lot of Postgres SQL queries stored in files and used from PHP. The task is to replace PHP with Java. We want to reuse as much of the queries "as is" to keep the migration path short. I can't get the Array parameters to work. Here's a query example: update user_devices set some_date = now() where some_id in ( select distinct some_id from user_devices where user_id = any(:userIDs) and device_id = any(:deviceIDs) and exists (select 1 from users where user_id = any(:userIDs)

createNativeQuery set parameter

半腔热情 提交于 2019-12-07 21:47:31
问题 I have the following that contains a NativeQuery where i need to set a parameter but somothing is wrong beacause parameter not set so the query is SELECT movieId, title, genres FROM movies where title like '%%'" so return all the rows. What is wrong public List<T> findMovie(String keyword) { Query q = getEntityManager().createNativeQuery("SELECT movieId, title, genres FROM movies where title like '%?%'", entityClass); q.setParameter(1, keyword); //etc return q.getResultList(); } 回答1: public

Manage @NamedNativeQuery and schema

∥☆過路亽.° 提交于 2019-12-01 20:20:53
问题 I have many EntityManager , one per schema that I have (I use entity-mappings file to map EMs with schemas). It works. When I use @NamedQuery it's working like a charm but when I use @NamedNativeQuery schema is not used. I have to qualify with it SELECT foo FROM schema.table . Is it the right behaviour ? I think it's not possible to parameter @NamedNativeQuery to dynamically pass schema (I believe only columns can be dynamics not tables or schemas or anything else) so how can I use