ConverterNotFoundException only in Testing time - java

I have java spring (v2.7.1) application and I want to test my database (H2 DB) layer.
I have the following query:
#Query(value =
"SELECT something.c1 " +
"FROM " +
"(SELECT Count(c1) AS Count, c1 " +
"FROM Table1 " +
"WHERE c2 IN (:thing2Ids) " +
"GROUP BY c1) " +
"AS something " +
"WHERE something.Count = :thingCount",
nativeQuery = true)
List<UUID> findThings(#Param("thingCount") Integer thingCount, #Param("thing2Ids") List<String> thing2Ids);
At the end of my query I want to get back a list of UUIDs. When I run this query (i.e. I build and run the application and call this method) it is working fine. But when I try to test this, I got the following error:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.util.ArrayList<?>] to type [#org.springframework.data.jpa.repository.Query java.util.List<java.util.UUID>] for value '[[somevalue1, [somevalue2]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [byte[]] to type [#org.springframework.data.jpa.repository.Query java.util.UUID]
Why my code needs converter at testing time but runtim don't? I don't get the point here.
I have checked this, this articles already but I don't want to write a converter just because of testing my app. Anyone did come across similar case?

Related

PostgreSQL NativeQuery with Param - org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"

I'm seeing the below error whenever I try and execute a nativeQuery with params.
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
The query being used and repo function is below
private String queryStr = "SELECT * FROM schema1.txn " +
"WHERE stat = 'Shipped' " +
"AND id IN :idList\\:\\:text[] " +
"AND issue_dt < NOW() - :ageToExpire\\:\\:interval";
#Query(value = queryStr, nativeQuery = true)
List<txn> findTxns(
#Param("idList") String idList,
#Param("ageToExpire") String ageToExpire
);
And the findTxns call looks like so
.findTxns("('ID1','ID2','ID3')", "'10 days'")
I'm unsure as to why I'm still seeing the above error. I'm casting the string params to their respective text list and interval types but Postgres doesn't seem to agree with that. I've seen similar issues but I can't seem to get any of the marked resolutions working in my case; I've tried isolating the query to just use one param but that isn't resolving anything.

No Value specified for parameter 2

I am getting this error when trying to run my Spring boot.
java.sql.SQLException: No value specified for parameter 2
My code is this:
public UserTemp findHistoryByID(Integer Patient_Number) {
String sql = "select Col1\n" +
"from (\n" +
" select Past_Diagnoses_1 as Col1\n" +
" from patienthistory\n" +
" where Patient_Number = ?\n" +
" union\n" +
" select Past_Diagnoses_2 as Col1\n" +
" from patienthistory" +
" where Patient_Number = ?" +
" ) as T;";
return jdbcTemplate.queryForObject(sql, new Object[]{Patient_Number}, (rs, rowNum) ->
new UserTemp(
rs.getString("Col1")
));
}
As in the comments, you are having 2 placeholders in the SQL query. So you have to pass patient_number 2 times.
Coming to your second question, it depends on your requirement.
If you need a single result, you need to fix it on the DB side as it's a data issue or the query used is not proper.
If more than one result is allowed, you can use jdbcTemplate.queryForList() instead of jdbcTemplate.queryForObject(). And change the return type of findHistoryByID() to List<Map<String,Object>> and all callers of this function.
Note: Here key for each Map in List is column names returned from DB.
More information on jdbcTemplate.queryForList() is in official documentation

Convert query Oracle with HQL (Hibernate Query Language)

I have this query for Oracle and i must convert in HQL (Hibernate Query Language). I haven't found information on convert key words like "START WITH" or "CONNECT BY PRIOR" or "SINBLINGS".
can you help me ?
The Query is this:
SELECT NOME_PADRE, COD_PADRE, NOME_FIGLIO, COD_FIGLIO,
CONFIRM_COD_FIGLIO, LEVEL
FROM (
SELECT s1.NOME AS NOME_PADRE, a.PADRE AS COD_PADRE, s2.NOME AS
NOME_FIGLIO, a.CMU AS COD_FIGLIO, s2.CMU AS
CONFIRM_COD_FIGLIO
FROM ALBERO a JOIN STRUTTURE s1 ON a.PADRE = s1.CMU
JOIN STRUTTURE s2 ON a.CMU = s2.CMU
ORDER BY PADRE ASC
)
START WITH COD_PADRE = '00000'
CONNECT BY PRIOR COD_FIGLIO = COD_PADRE
ORDER SIBLINGS BY COD_PADRE
Thank you very much
I tried to use a native query.
I use Spring-Boot and i have a Controller, a Service and a Repository.
In my repository, I implement a native query like this:
String QUERY = " SELECT s.CMU, s.NOME, a.PADRE, LEVEL "
+ " FROM STRUTTURE s LEFT JOIN ALBERO a ON s.CMU = a.CMU "
+ " START WITH a.PADRE = '00000' "
+ " CONNECT BY PRIOR a.CMU = a.PADRE "
+ " ORDER SIBLINGS BY a.PADRE";
#Query(nativeQuery = true, value = QUERY)
public List<Struttura> findAllWithConnectBy();
but I have this error:
org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'strutturaController': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

Spring data jpa, Native Query, returned wrong field types

I have following native query method in my repository:
#Query(value="SELECT appSub.ApplicationFormId as appFormId, appSub.profileId as profileId, "
+ "p.CASId as profileCASId, ps.programId as programId FROM [unicas_config].[dbo].ApplicationFormEarlyDecisionConfig appFormED "
+ "INNER JOIN [unicas_ux].[dbo].ApplicationSubmission appSub ON appFormED.ApplicationFormId = appSub.applicationFormId "
+ "INNER JOIN [unicas_ux].[dbo].Profile p ON appSub.profileId = p.id "
+ "INNER JOIN [unicas_ux].[dbo].ProgramSelected ps ON p.id=ps.ProfileId AND appSub.applicationFormId = ps.instanceId "
+ "WHERE appFormED.EarlyDecisionVerdictDate >=:fromDate AND appFormED.EarlyDecisionVerdictDate <:toDate "
+ "AND appSub.EarlyDecisionStatus='Applied Early Decision' "
+ "AND appSub.ApplicationStatus='Received' "
+ "AND ps.IsPaid =1 "
+ "ORDER BY appSub.ApplicationFormId",nativeQuery = true)
List<Object[]> getAllEarlyDecisionApplicantsWithPaidProgramsOnVerdictDate(#Param("fromDate") Date fromDate, #Param("toDate") Date toDate);
Now, I want to map the returned result:
long appFormId = (Long)obj[0]
long profileId = (Long)obj[1]
long programId = (Long)obj[3]
When I am doing that, I am getting java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long as Hibernate consider these ids of Integer type instead of Long.
Please, tell me how could I programatically tell Hibernate to return proper type.
To be on the safe side, I always cast numeric types to Number and then get the value of desired type from it, as JDBC driver can return Integer, Long, BigDecimal, etc. depending on the type of the database column:
((Number) obj[0]).longValue()
Of course, don't forget to check for null if column is nullable.

Number of characters in sql query result

I am using Spring 3.1.1 with Hibernate 4 and a MSSQL database. Finally, I have been able to query my database with joins in my table that returns the correct answers. Although, it seems that it does not return the entire strings of my messages, but cuts at 29/30 digits. Here is my query:
SQLQuery sql = this.getCurrentSession().createSQLQuery(
"SELECT event.id as eventid, CAST(event_type.type_name AS varchar) as eventtype, event.check_date, event.event_date, event.status, CAST(event_message.message AS varchar) as eventmessage " +
"FROM event_log event " +
"LEFT JOIN event_type " +
"ON (event.event_type_id = event_type.id) " +
"LEFT JOIN event_message " +
"ON (event.event_message_id = event_message.id) " +
"WHERE event.event_type_id = " + jobId +
"ORDER BY eventid");
The result can be:
4506 Database 2014-01-15 14:14:15.02 2014-01-15 14:14:15.02 false Database-connection cannot be
Where the columns are id, task_name, check_date, event_date, status and the message-string at the end. .
The result goes to a ArrayList<Object[]> where I read row[0] etc. to get the entities in the row. The string message gets cut after 29 digits, which is disturbing. Why does it cut the string and how can I fix this? In my database the string exists in it's full and is entitled 400 characters max.
I know this is probably not the best way to query my database, but it will do for my application since it works.
You are using varchar without a length. Never do this!
Replace:
CAST(event_type.type_name AS varchar)
With something like:
CAST(event_type.type_name AS varchar(255))
In some contexts, the default length is 32. In some it is 1. In general, though, always specify the length.
EDIT:
Actually, you probably don't even need the cast. Why not just have event_type.type_name in the select list?

Categories

Resources