QuerDSL to generate Native Oracle Query - java

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

Related

How to pass dashed strings to oracle from a query written in java

I have a problem with this query when I pass it to an oracle dbms
SELECT * FROM RD_RBF WHERE REQUEST_ID = 'S2N-F01-000000000001'
because of the dashes in the string the jvm return me this exception
java.sql.SQLException: Fail to convert to internal representation
How can I pass this query to oracle correctly? Thanks a lot
P.S. I'm not shure of the code because I'm using Talend software that generates automatically the code of components but I can post part of the code above
String dbquery_tOracleInput_1 = "SELECT * FROM RD_RBF WHERE REQUEST_ID = 'S2N-F01-000000000001'";
java.sql.ResultSet rs_tOracleInput_1 = null;
try{
rs_tOracleInput_1 = stmt_tOracleInput_1.executeQuery(dbquery_tOracleInput_1);
java.sql.ResultSetMetaData rsmd_tOracleInput_1 = rs_tOracleInput_1.getMetaData();
int colQtyInRs_tOracleInput_1 = rsmd_tOracleInput_1.getColumnCount();
Use toraclerow component.
Query as follows in component:
"SELECT * FROM RD_RBF WHERE REQUEST_ID = ?"
Go to advance setting, select use prepared statement and add 1 paarmeter index.
also select the propagate Query's record set.
tOracleRow_1 -----> tParseRecordSet----->

jOOQ - build UPDATE FROM psql query

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();

Spring data - JPA : issue query with DTO and IF statement

I am trying to make a query that would build a DTO object :
#Query("Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, IF(a.salarie,'yes','no'))
from Activity a where a.year = ?1 and a.month = ?2")
List<AnalyseProductionDTO> getAnalyseProduction(Year year, Month month);
It doesn't work when I put a IF statement in the query.
I am getting a
java.lang.IllegalArgumentException: Validation failed for query
It works when I do :
#Query("Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, 'yes')
from Activity a where a.year = ?1 and a.month = ?2")
List<AnalyseProductionDTO> getAnalyseProduction(Year year, Month month);
Isn't it possible to include an IF statement in a constructor used in a query?
By default #Query doesn't allow native queries. The query should be either HQL if the ORM is hibernate or anything that is supported by the underlying ORM.
Since IF() function is native to MySQL and not supported by ORM query languages its validation will fail.
You can try the following with #Query.nativeQuery set to true.
#Query(value = "Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, IF(a.salarie,'yes','no'))
from Activity a where a.year = ?1 and a.month = ?2", nativeQuery = true)

Jpa create native query from form input

Usually during my work hours i spend a lot of time querying the db(oracle) and parsing blob from various table where the streams that we receive are stored.
There are various type of stream so i was trying to made a simple webapp where i write the select statement and it returns all the stream parsed accordingly.
My problem is that using jpa and executing the simple native query:
select B_BODY from TABLE_B where TRANSACTION_ID = 'GG-148c-01502790743907855009';
the statement doesn't return anything but querying directly the database return the record.
this is my java code:
#Transactional(readOnly = true)
public List<Object[]> retrieveBlobs(String squery) {
squery = squery + " and rownum <= "+maxResults;
Query query = em.createNativeQuery(squery);
List<Object[]> resultList = query.getResultList();
return resultList;
}
this is the sql generated:
Hibernate:
select
B_BODY
from
TABLE_B
where
TRANSACTION_ID ='GG-148c-01502790743907855009'
and rownum <= 100
i know that this way might seems weird but our team spend a lot of time trying to tokenize the stored streams(the code that identify how to parse the stream is also stored in the tables).Useless to say this application is going to be used only internally.there is a way to just execute the query as it is and retrieve the correct output?
Well, I tried to reproduce your problem on MariaDB (with mysql-connector-java + hibernate) but selecting a lob with native query was working properly.
You can try to create entities which will be holding your blob and check if this would help. Just make a standard entity with #Lob annotation over your lob column.
#Entity
#NamedQueries(
#NamedQuery(name = FIND_ALL, query = "SELECT m FROM LobEntity m")
)
public class LobEntity {
public static final String FIND_ALL = "PhpEntity.findAll";
#Id
#Column(name = "id")
private String id;
#Lob
#Column(name = "lob")
private byte[] lob;
//Use Blob class if you want to use streams.
//#Column(name = "lob")
//#Lob
//private Blob lob;
}

Encountered "INSERT" at character 1, but expected: ["DELETE", "SELECT", "UPDATE"]

I have an insert JPA named query as below. When I try to execute it from my DAO using enity manger I am getting the below error. I am using Apache OpenJPA, I could not identify the root cause of this issue. I cannot use the persist method because of the complexity of my domain object.
Encountered "INSERT" at character 1, but expected: ["DELETE", "SELECT", "UPDATE"].
#NamedQuery(
name=IuaPersistenceConstants.QUERY_INSERT_AGREEMENT_ACKNOWLEDGEMENT,
query="INSERT INTO AgreementAcknowledgement agrackn VALUES agrackn.id.agrmntCntntUrl = :agrmntCntntUrl, agrackn.id.prvsndUserSqn = :prvsndUserSqn, agrackn.id.prvsnUserEffD = :userEffDate, " +
"agrackn.id.agrmntacknEffD = :agrmntacknEffD, agrackn.acknExpD = :acknExpD, agrackn.crtTs = :crtTs, agrackn.crtUidC = :crtUidC, agrackn.lstUpdtTs = :lstUpdtTs, agrackn.lstUpdtUidC = :lstUpdtUidC")
Query ackQuery =getEntityManager().createNamedQuery(IuaPersistenceConstants.QUERY_INSERT_AGREEMENT_ACKNOWLEDGEMENT);
ackQuery.setParameter("agrmntCntntUrl", agreementDO.getAgreementURL());
ackQuery.setParameter("agrmntacknEffD", agreementDO.getAgreementEffDate());
ackQuery.setParameter("acknExpD", agreementDO.getAgreementExpDate());
ackQuery.setParameter("prvsndUserSqn", userDO.getUserSequence());
ackQuery.setParameter("userEffDate", userDO.getUserEffDate());
ackQuery.setParameter("crtTs", new Date());
ackQuery.setParameter("crtUidC", "WS");
ackQuery.setParameter("lstUpdtTs", new Date());
ackQuery.setParameter("lstUpdtUidC", "WS");
ackQuery.executeUpdate();
OpenJPA doesn't seem to support JPQL "INSERT" queries. But then that is not surprising since JPA DOES NOT define INSERT queries. It defines SELECT, UPDATE, DELETE queries and that is all. See the JPA spec. This is not SQL.

Categories

Resources