Column type not valid with NATIVE query jpql - eclipselink

this is my repository:
#Repository
public interface EventRepository extends JpaRepository<Events, Long>, JpaSpecificationExecutor<Events>{
#Query(value=" SELECT DISTINCT event.EVENT_ID "
+ "FROM EVENTS event "
+ "JOIN EVENTS event2 "
+ "ON event.EVENT_ID = event2.PARENT_ID "
+ "WHERE event.ENTITY_ID IN (?1)", nativeQuery = true)
List<Long> getDescendantEventIdInEntityId(Set<Long> descendantEntities);
}
Why when this method is called, it's return:
Internal Exception: java.sql.SQLException: Tipo di colonna non valido
Error Code: 17004
Call: SELECT DISTINCT event.EVENT_ID FROM EVENTS event JOIN EVENTS event2 ON event.EVENT_ID = event2.PARENT_ID WHERE event.ENTITY_ID IN (?)
bind => [[1]]
what is wrong with the query?
If the query is ok, is that "bind => [[1]]" the true problem? If yes, why there's too many square brackets?
I've also tried this solution:
#Query(value=" SELECT DISTINCT event2.EVENT_ID "
+ "FROM EVENTS event "
+ "JOIN EVENTS event2 "
+ "ON event.EVENT_ID = event2.PARENT_ID "
+ "WHERE event.ENTITY_ID IN :entitiesId", nativeQuery = true)
List<Long> getDescendantEventIdInEntityId(#Param("entitiesId") Set<Long> entitiesId);
but it return me another error:
Missing IN or OUT parameter at index:: 1
Error Code: 17041
Call: SELECT DISTINCT event2.EVENT_ID FROM EVENTS event JOIN EVENTS event2 ON event.EVENT_ID = event2.PARENT_ID WHERE event.ENTITY_ID IN :entitiesId
SOLUTION: Solved creating a new nativeQuery from EntityManager:
em.createNativeQuery(" SELECT DISTINCT event2.EVENT_ID "
+ "FROM EVENTS event "
+ "JOIN EVENTS event2 "
+ "ON event.EVENT_ID = event2.PARENT_ID "
+ "WHERE event.ENTITY_ID IN ("+idList+") AND event.STATUS = 2")
.getResultList();
Where idList is a String made by StringUtils.join(CollectionOfId, ",");

If Hibernate was used as JPA provider, instead of positional parameter binding (?1) you could also use named parameter binding (:parameterName). This would be less error prone and you could omit the brackets and let Spring manage the syntax for your. You also would not need to parse anything on your own.
Use named parameter binding like:
#Query(value="SELECT DISTINCT event2.EVENT_ID "
+ "FROM EVENTS event "
+ "JOIN EVENTS event2 "
+ "ON event.EVENT_ID = event2.PARENT_ID "
+ "WHERE event.ENTITY_ID IN :entityIds", nativeQuery = true)
List<Long> getDescendantEventIdInEntityId(#Param("entityIds") Set<Long> ids);
Be aware of passing null or an empty set to the method, in both cases! That could cause unpredictable results.
Sadly, named parameters are not a JPA standard and maybe EclipseLink does not support it (in this way). Then you have to go with positional parameters.

Related

How to write IN clause in hibernate native(createNative) query?

How to complete this query with IN clause?
ArrayList<Long> statusIds = new ArrayList<Long>();
_merchantTransaction.getMerchantTxnStatusList().forEach(statusItem ->{
statusIds.add(statusItem.getMerchantTxnStatusId());
});
Query query = entityManager.createNativeQuery("select *"
+ " from merchant_transactions mt"
+ " inner join appl_merchant_txn_statuses mts on mt.merchant_txn_status_id = mts.merchant_txn_status_id"
+ " where"
+ " mt.customer_id ="+ _merchantTransaction.getCustomerId()
+ " and mt.merchant_transaction_type_id = "+ _merchantTransaction.getMerchantTransactionType().getMerchantTransactionTypeId()
+ " and mt.merchant_txn_status_id in " + statusIds
,MerchantTransaction.class);
While executing, this result following query and it gives
SQLGrammerExecption.
select *
from merchant_transactions mt where
mt.customer_id = 3998
and mt.merchant_transaction_type_id = 2
and mt.merchant_txn_status_id in [1, 8]);
How to solve this?
Thanks & Regards,
Dasun.
You should consider using named parameter bindings instead of adding the parameters directly by concatenation as follows:
Query query =
entityManager.createNativeQuery("select *"
+ " from merchant_transactions mt"
+ " inner join appl_merchant_txn_statuses mts on mt.merchant_txn_status_id = mts.merchant_txn_status_id"
+ " where mt.customer_id = :customer_id"
+ " and mt.merchant_transaction_type_id = :merchant_transaction_type_id"
+ " and mt.merchant_txn_status_id in (:status_ids)", MerchantTransaction.class);
query.setParameter("customer_id", _merchantTransaction.getCustomerId());
query.setParameter("merchant_transaction_type_id", _merchantTransaction.getMerchantTransactionType().getMerchantTransactionTypeId());
query.setParameter("status_ids", statusIds);
Advantages of named parameter bindings as described in hibernate best-practices here:
you do not need to worry about SQL injection,
Hibernate maps your query parameters to the correct types and
Hibernate can do internal optimizations to provide better
performance.
As a side note, I see that you are using JPA's Entity Manager createNativeQuery, so this will work:
query.setParameter("status_ids", statusIds);
If you were using Hibernate createSQLQuery, you need this:
query.setParameterList("status_ids", statusIds);

Joining unrelated tables via JPQL

I have the following query which works. All the tables in this query have relations in some way.
#Repository(value = "ARepository")
public interface ARepository extends JpaRepository<CardEntity, String> {
#Query("SELECT xref.shortUtlTx FROM CardEntity card " +
"JOIN card.apexUrlCrossRef xref " +
"JOIN xref.sampleProdOffer offer " +
"WHERE xref.apexCard.cardNumber = :cardNum " +
"AND offer.apexeeOfferId = :myCode"
)
List<String> getAllValues(#Param("cardNum") String cardNum, #Param("myCode") String myCode);
}
But I also wish to join another table (Entity name -> UrlCountEntity) to this query but that table has no relation to the other tables in this query. Is there a way I could do this?
Based on reading a blog, I tried the following but throws errors.
Added this line to the query:
AND EXISTS (SELECT referCount FROM UrlCountEntity referCount WHERE
referCount.url.urlTx = xref.shortUtlTx)
#Repository(value = "ARepository")
public interface ARepository extends JpaRepository<CardEntity, String> {
#Query("SELECT xref.shortUtlTx FROM CardEntity card " +
"JOIN card.apexUrlCrossRef xref " +
"JOIN xref.sampleProdOffer offer " +
"WHERE xref.apexCard.cardNumber = :cardNum " +
"AND offer.apexeeOfferId = :myCode " +
"AND EXISTS (SELECT referCount FROM UrlCountEntity referCount WHERE referCount.url.urlTx = xref.shortUtlTx)"
)
List<String> getAllValues(#Param("cardNum") String cardNum, #Param("myCode") String myCode);
}
Error as follows:
Method threw
'org.springframework.dao.InvalidDataAccessResourceUsageException'
exception.
could not extract ResultSet; SQL [n/a]
Based on this article:
Using Hibernate 5.1 or newer you can join two unrelated tables via JQPL same way you would do it in SQL:
SELECT first
FROM First first JOIN
Second second ON first.property = second.property
WHERE first.property = :param
So you would need to change your query to something like this:
#Query("SELECT xref.shortUtlTx FROM CardEntity card " +
"JOIN card.apexUrlCrossRef xref " +
"JOIN xref.sampleProdOffer offer " +
"JOIN UrlCountEntity referCount ON referCount.url.urlTx = xref.shortUtlTx" +
"WHERE xref.apexCard.cardNumber = :cardNum " +
"AND offer.apexeeOfferId = :myCode")
List<String> getAllValues(#Param("cardNum") String cardNum, #Param("myCode") String myCode);

#query with named #param returning empty result set when using sql IN

Btw this is already working with a jdbc template based solution that I wrote. It's not me wanting to do the rewrite, it is the tec lead's idea. It is giving me a headache.
Lets not get into the discussion on why this is or isn't a good idea, but rather how to make it work.
in my repo
#Repository
interface Foo...extends JpaRepository
this works:
#Query(value = "SELECT t.td, em.ct, j.dsc AS bs, ct.dsc AS ct_name, "
+ "t.tn, et.dsc, f.f_id, f.f_life, f.fl_name, em.f_note "
+ "FROM ext_master em join tax t on em.td = t.td "
+ "join bs j on j.bs = t.bs "
+ "join ex_type et on em.ex_type = et.ex_type "
+ "join ct ct on ct.ct = em.ct "
+ "left join ex_f ef on em.td = ef.td and em.ct = ef.ct "
+ "left join f f on ef.f_id = f.f_id "
+ "WHERE em.ct in ( 'CC' ) "
+ "AND t.state IN ('FF','AK','AL','GA')"
+ "ORDER BY bs, t.tn", nativeQuery = true)
List<Object> getExtensionsByCustTypeAndState(#Param("custTypes") String custTypes, #Param("states") String states);
this doesn't returns empty result set
#Query(value = "SELECT t.td, em.ct, j.dsc AS bs, ct.dsc AS ct_name, "
+ "t.tn, et.dsc, f.f_id, f.f_life, f.fl_name, em.f_note "
+ "FROM extension_master em join tax t on em.td = t.td "
+ "join bs j on j.bs = t.bs "
+ "join exemption_type et on em.ex_type = et.ex_type "
+ "join ct ct on ct.ct = em.ct "
+ "left join ex_f ef on em.td = ef.td and em.ct = ef.ct "
+ "left join f f on ef.f_id = f.f_id "
+ "WHERE em.ct in ( :custTypes ) "
+ "AND t.state IN ('FF', :states)"
+ "ORDER BY bs, t.tn", nativeQuery = true)
List<Object> getExByCustTypeAndState(#Param("custTypes") String custTypes, #Param("states") String states);
both can have one or multiple values, I tried:
with both params like {:states} :#{#states}
with the value inside the param
surrounded by " or by '
passing the whole line in the parameter like "AND t.state IN ('FF','AK','AL','GA')"
passing the 'FF','AK','AL','GA' in the states param and subtituting the in the query with
+ "AND t.state IN ( :states)"
I did write a simple query to make sure I am getting the parameters passed in right. so that's working.
Can anyone write this right, so that the in statements work, or tell me why it is not possible.
Thank you.
You pass all the possible values in one parameter that's why you don't get the results you are expecting.
You have to change the where clause like this:
"WHERE em.ct in ( :custTypes1, :custTypes2, :custTypes3 .... :custTypesN) "
this ended up being the simplest solution
instead of :
"WHERE em.ct in ( :custTypes ) "
+ "AND t.state IN ('FF', :states)"
do:
"WHERE em.ct in :custTypes "
+ "AND t.state IN :states "
and add the FF to the states string on before I call the function.

Springboot Hibernate Query: filter by childrens attributes

Hi I have a problem generating a #query in my SpringBoot repo. Using hibernate.
My POJOs:
class Offer
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#ManyToMany
private List<Attribute> attributes;
#ManyToMany
private List<Offer> offers;
and
class Attribute
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
It works this way to get the "offer.offers by attribute". But now I want to enhance my query to get "offer offer.offers.attribute" an I strugle with it.
What I want is to find all Offers which have "Offer.attribute == 1" AND "all Offer.offers.attribute == 2"
Ending with this query:
#query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN o.offers.attributes ooa " <-- Problem issue
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 "
+ "AND a.id = 100 "
+ "AND ooa.id = 101 "
+ "GROUP BY o.id "
But upon compilation I get this exception:
org.hibernate.QueryException: illegal attempt to dereference collection
[offer0_.id.offers] with element property reference [attributes]
Now I wonder how to do it correctly?
Hope some SQL/Hibernate hero can help me.
Thanks in advance!
Gregor
Well... Iam one step further:
this query works:
#query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN oo.attributes ooa " <-- referencin JOIN
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 "
+ "AND a.id = 100 "
+ "AND ooa.id = 101 "
+ "GROUP BY o.id "
But now I get all results with at least on "Offers.offer.attributes == 101"
But I need all offers.offer having that attribute.
Any hints how to continue?

native query with spring data jpa doesn't return result

Hello I have written the following method in spring data jpa repo:
#RestResource(exported = false)
#Query(
value = "select q.* " +
"from question q " +
"left join question_program qp " +
"on q.question_id = qp.question_id " +
"where " +
"q.difficulty_level_id = ?1 " +
"and " +
"q.paid = false " +
"and " +
"qp.program_id = ?2 " +
"order by random() " +
"limit ?3",
nativeQuery = true
)
List<Question> getFreeRandomQuestions(
#Param("df") Integer df,
#Param("programId") Integer programId,
#Param("qs") Integer qs);
It is returning an empty list whereas when I run the same query in pgadmin I get all the desired rows. It also works in spring data jpa if I remove the join with question_program. What am I doing wrong?
UPDATE:
I have tried select q ... and select * .... Both have the same problem
UPDATE:
I kind of solved this by implementing a service which fetches that data using sessionFactory and raw sql query!.