How to build the following PSQL query to update multiple records using jOOQ 3.10.x?
UPDATE users
SET name = s.name, city = s.city
FROM users_staging s
WHERE users.id = s.id;
UsersStaging s = USERS_STAGING.as("s");
DSL.using(configuration)
.update(USERS)
.set(USERS.NAME, s.NAME)
.set(USERS.CITY, s.CITY)
.from(s)
.where(USERS.ID.eq(s.ID))
.execute();
Related
I'm trying to create a data filter using SpringBoot and PostgreSQL, I'm trying to run a query to get a customer by its name or id or account id in a searchBar, running into an issue when running a native SQL statement in Spring Data JPA, I'm getting the following error:
The error says that Could not execute query. I've tried running the statement in different ways but still giving me the error, these are the ways I have tried:
#Query(
value = "SELECT json_data FROM contact WHERE name = :searchText OR account_id = :searchText OR id = :searchText",
nativeQuery = true)
#Query(
value = "SELECT json_data FROM contact WHERE contact.name LIKE %:searchText% OR contact.account_id LIKE %:searchText% OR contact.id LIKE %:searchText%",
nativeQuery = true)
#Query(
value = "SELECT json_data FROM contact WHERE name = ?1 OR account_id = ?1 OR id = ?1",
nativeQuery = true)
Page<Contact> findByWord(Pageable pageable, #Param("searchText") String searchText);
I'm able to run successfully the query in PGAdmin as follows:
SELECT json_data FROM contact
WHERE name = 'TESLA ARQUITECTURA S.A.S'
OR account_id = ''
OR id = ''
or:
SELECT json_data FROM contact
WHERE name = ''
OR contact.account_id = ''
OR contact.id = '2'
or:
SELECT json_data FROM contact
WHERE name = ''
OR account_id = '1674590687S'
OR id = ''
All of these statements are working and the idea is that if I pass as a parameter to the SQL statement it will return the information from the column json_data.
So I'm not sure if im doing something wrong when creating the query in the repository, I would appreciate so much your help on this.
Do select * not select json_data in your query, currently you are trying to map a single column into a whole Entity that's why it's complaining the id is missing from the result set. column name seq_id was not found in this result set was there in the error log all along.
SELECT NEW Map (PRODUCT_CATEGORY, COUNT(PRODUCT_CATEGORY) AS COUNTER) from Product WHERE USER_ID = (SELECT USER_ID FROM USERS WHERE USERNAME='burak123'
Hi everyone,
As hibernates document says here: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select
I am trying to map the query result into a hash map like this
#Query(value = "SELECT NEW Map( PRODUCT_CATEGORY , COUNT(PRODUCT_CATEGORY) AS COUNTER ) from Product WHERE USER_ID=(SELECT USER_ID FROM USERS WHERE USERNAME=(:username)) ",nativeQuery = true)
HashMap<Integer,Integer> getCategoryCountsWithUsername(#Param("username")String username);
But it throws an JdbcSyntaxErrorException. I am trying to solve this for like 1 hours already. Can someone help?
You are using a native query, not an HQL query. Check your SQL syntax.
With a native query, your named parameter won't work. You need to remove the nativeQuery = true
I am trying to generate Native Query like this so that I can run them separately.
Expected Query:
select test.TEST_KEY
from TEST_TABLE test
where test.TEST_CODE = 'TEST_01' and test.TEST_ACCOUNT_NUMBER = '001' and test.POSTED_UTC_DATE between timestamp '2020-06-19 23:59:59' and timestamp '2020-06-19 23:59:59'
Query I am getting
select testTable.testKey
from testTable
where testTable.testCode = 'TEST_01' and testTable.testAccountNumber = '0000124001' and testTable.postedUtcDate between timestamp '2020-06-19 23:59:59' and timestamp '2020-06-19 23:59:59'
Code
public String getTestResults(DataDto dataDto) {
SQLTemplates templates= OracleTemplates.builder().printSchema().build();
Configuration configuration=new Configuration(templates);
configuration.setUseLiterals(true);
PathBuilder<?> entityPath = new PathBuilder<>(getEntityClass(), getEntityName());
SQLQuery<Object> sqlQuery= (SQLQuery<Object>) new SQLQuery(configuration)
.select(entityPath.getString(getColumnMap().get("TEST_KEY")))
.from(entityPath)
.where(buildCondition(dataDto).build());
sqlQuery.setUseLiterals(true);
String query=sqlQuery.getSQL().getSQL();
return query;
}
I referred to this post and query DSL document however no help so far.
How to get fully materialized query from querydsl
http://www.querydsl.com/static/querydsl/3.3.1/reference/html/ch02s03.html
There was a bug in constant rendering in old versions of QueryDSL. Please upgrade to a more recent version.
See also: https://github.com/querydsl/querydsl/issues/2017
Is it possible to delete multiple rows via JPA in a database table via a specific ID?
e.g.
Delete from PERSON_LANGUAGE where PERSON_ID = 125;
Thanks in advance!
Down below is what im currently trying to use without any luck!
public void deletePersonLanguageById(PersonLanguage personLanguage){
PersonLanguage personLanguage1 = em.find(PersonLanguage.class, personLanguage.getPersonId());
em.remove(personLanguage1);
}
I want it to delete everything in my databasetable where the ID = 1
You can use a native query to execute SQL directly against your database
Query q = em.createNativeQuery("DELETE FROM person_language WHERE person_id = 1");
q.executeUpdate();
or a JPQL query:
Query q = em.creteQuery("DELETE FROM Person p WHERE p.personId = 1");
q.executeUpdate();
You can create a custom repository that extends from the JPA Repository and there you add the deleteByPersonId(String personId).
Some more examples here: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.details
I'm using Hibernate Criteria API. Can you help me get a list of id's like this SQL query.
Here is the SQL query:
SELECT id FROM upload_rdp idx WHERE idx.is_update_stat = 0 AND
(SELECT COUNT(r_a.id) FROM rdp r_a WHERE r_a.upload_rdp_id = idx.id) =
(SELECT COUNT(r_cv.id) FROM rdp r_cv WHERE r_cv.upload_rdp_id = idx.id AND
r_cv.is_checked_valid = 1) ORDER BY idx.id;
Here is a code, where I get a list of the first query. The SQL query is:
SELECT id FROM upload_rdp idx WHERE idx.is_update_stat = 0;
Using the Criteria API:
Criteria criteria = createEntityCriteria().addOrder(Order.asc("id"));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.add(Restrictions.eq("isUpdateStat", 0));
criteria.setProjection(Projections.property("id"));
criteria.list();
Thanks and sorry for my English.