nativequery

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

廉价感情. 提交于 2019-12-01 14:43:38
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) and customer_id = :customerID) ); Note the "any" clauses, which cause the problem, because they expect

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

笑着哭i 提交于 2019-11-29 17:34:52
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 "'". This is not possible. Parameters are only allowed in the where clause. You can use entitymanger in jpa project. @Autowired EntityManager entityManager; entityManager.createNativeQuery("select * from "+tableName+"") 来源: https://stackoverflow.com/questions/40956013/how-to-replace-table

Spring Data JPA delete native query throwing exception

吃可爱长大的小学妹 提交于 2019-11-28 21:19:32
I have a User entity and a Role entity. The relationship is defined like this: @OneToMany @JoinTable(name="USER_ROLES", inverseJoinColumns=@JoinColumn(name="ROLE_ID")) private List<Role> roles = null; Now, when I delete a role, I need to delete the role from all the users that have that role. Normally you'd do something like this by looking up all the users with this role, removing the role from the list, and saving the user. However, when there could be over a million users, I don't want to be looping over this many entities in the app. So, I'm wanting to use a native query to remove rows

How to truncate a table using Doctrine 2?

女生的网名这么多〃 提交于 2019-11-28 19:11:49
I assume that I need to build a native query to truncate a table using Doctine2. $emptyRsm = new \Doctrine\ORM\Query\ResultSetMapping(); $sql = 'TRUNCATE TABLE Article'; $query = em()->createNativeQuery($sql, $emptyRsm); $query->execute(); This gives the error SQLSTATE[HY000]: General error What do I need to change to my code to make this work? cmt Beware of Truncating Tables Beware of truncating tables in any RDBMS, especially if you want to use explicit transactions for commit/rollback functionality. DDL statements perform an implicit-commit Truncate table statements are data definition

JPA Data Repositories with SqlResultSetMapping and native queries

梦想与她 提交于 2019-11-27 17:48:44
问题 I was stuck with the following situation: My entities are related to each other, but in such a way that i could not use JPQL. I was forced to use native SQL. Now I want to map these results to a ValueObject. To be clear, I don't want to get a list of Object array ( List<Object[]> ). I have 6 entities from which I need only some columns. Can anybody give me an example on how to implement such a mapping from a native query? Tutorial that I went through. My code: @SqlResultSetMapping( name =

Spring Data JPA delete native query throwing exception

和自甴很熟 提交于 2019-11-27 12:57:06
问题 I have a User entity and a Role entity. The relationship is defined like this: @OneToMany @JoinTable(name="USER_ROLES", inverseJoinColumns=@JoinColumn(name="ROLE_ID")) private List<Role> roles = null; Now, when I delete a role, I need to delete the role from all the users that have that role. Normally you'd do something like this by looking up all the users with this role, removing the role from the list, and saving the user. However, when there could be over a million users, I don't want to

How to truncate a table using Doctrine 2?

﹥>﹥吖頭↗ 提交于 2019-11-27 12:00:16
问题 I assume that I need to build a native query to truncate a table using Doctine2. $emptyRsm = new \Doctrine\ORM\Query\ResultSetMapping(); $sql = 'TRUNCATE TABLE Article'; $query = em()->createNativeQuery($sql, $emptyRsm); $query->execute(); This gives the error SQLSTATE[HY000]: General error What do I need to change to my code to make this work? 回答1: Beware of Truncating Tables Beware of truncating tables in any RDBMS, especially if you want to use explicit transactions for commit/rollback

How can I use MySQL assign operator(:=) in hibernate native query?

跟風遠走 提交于 2019-11-27 02:03:56
I'm using Hibernate. I wrote some native query because I need to use sub select statement. Query looks like this: SELECT sub.rownum FROM (SELECT k.`news_master_id` AS id, @row := @row + 1 AS rownum FROM keyword_news_list k JOIN (SELECT @row := 0) r WHERE k.`keyword_news_id` = :kid ORDER BY k.`news_master_id` ASC) AS sub WHERE sub.id = :nid When I run this query like this: sessionFactory.getCurrentSession() .createSQLQuery(query) .setParameter("kid", kid) .setParameter("nid", nid) .uniqueResult(); This exception comes out: org.hibernate.QueryException: Space is not allowed after parameter

How can I use MySQL assign operator(:=) in hibernate native query?

坚强是说给别人听的谎言 提交于 2019-11-26 09:53:48
问题 I\'m using Hibernate. I wrote some native query because I need to use sub select statement. Query looks like this: SELECT sub.rownum FROM (SELECT k.`news_master_id` AS id, @row := @row + 1 AS rownum FROM keyword_news_list k JOIN (SELECT @row := 0) r WHERE k.`keyword_news_id` = :kid ORDER BY k.`news_master_id` ASC) AS sub WHERE sub.id = :nid When I run this query like this: sessionFactory.getCurrentSession() .createSQLQuery(query) .setParameter(\"kid\", kid) .setParameter(\"nid\", nid)