How to add oracle hint (use_nl) to a spring boot jpa query - jpa

I have a hql query
select new com.packagename.CountryInfoDto(c.countriesId, c.internationalCode, c.countryName, ot.name) from Country c
inner join OtherTable ot on c.otid = ot.id
where c.deleted = (:deleted)
order by c.countryName
I create the query like this.
TypedQuery query = entityManager.createQuery(queryString,
BookingInfoDto.class);
Now if I want to hint use_nl(c ot) (use nested loops) how can I add that hint?

You can't. You need to use a native query.

Related

Inner join on one PostgreSQL database table and another one is normal array

I am new to PostgreSQL.
Can you please tell me why the below query is not working.
Select *
from custom_test as dbtable
inner join aLocalArray as localtable on dbtable.id = localtable.id
Here custom_test is database table in PostgreSQL. aLocalArray is an array which is prepared by me.
If you want to join the array, you probably want to use UNNEST(array):
SELECT *
FROM custom_test AS dbtable
JOIN UNNEST (aLocalArray) AS localtable (id)
ON dbtable.id = localtable.id
But as suggested in the comments, the dbtable.id = any(aLocalArray) predicate or even the ARRAY[dbtable.id] #> aLocalArray operator might also do the trick.

how to call spring jap query none parameters

I use spring data jpa with native query
I have already some query like this
How to use native query none parameter.
String q="SELECT t1.blockNumber-1 FROM someTAble t1 LEFT JOIN someTAble t2 ON t2.blockNumber = t1.blockNumber-1 WHERE t2.blockNumber IS NULL AND t1.blockNumber> 0 ORDER BY t1.blockNumber";
#Query(value = q,nativeQuery = true)
List<Entity> findByBlockNumberIs();
they are occur errors Column 'sequence' not found.
That query means are when i insert some Contiguous data int value then i find missing data.
But
this query working
SELECT *,t1.blockNumber-1 FROM someTAble t1 LEFT JOIN someTAble t2 ON t2.blockNumber = t1.blockNumber-1 WHERE t2.blockNumber IS NULL AND t1.blockNumber> 0 ORDER BY t1.blockNumber
The difference between the two queries is whether there is a '*' or not
how to change simple to my query.
How to i changed error
OR How to use spring data jpa predicate
QEntity qBe1= QEntity .blockEntity;
QEntity qBe2= QEntity .blockEntity;
build.and(qBe2.blockNumber.eq(be.getBlockNumber()-1))
.and(qBe2.blockNumber.isNull().and(qBe1.blockNumber.gt(0)));
is predicate can use left join?
well...
use this.
List<Integer> findByBlockNumber()

How to write this query in JPA

How to write this query in JPA?
select sol.ID_UNICA, sol.version
from uexdfr01.SOLUTION sol
join
(select solution4_.ID_UNICA, max(solution4_.version) as maxVersion
from uexdfr01.ORDER order3_
inner join uexdfr01.SOLUTION solution4_ on order3_.ID_SOLUTION=solution4_.ID_SOLUTION
where solution4_.ID_UNICA in (130,139,143,129,126,128,141,121,124,131)group by solution4_.ID_UNICA) as groupedtt
on sol.ID_UNICA = groupedtt.ID_UNICA
AND sol.version = groupedtt.maxVersion*
If you are not sure about db relationships use createNativeQuery and pass your query inside parenthesis.
Query selectRecords = getEntityManager().createNativeQuery(sqlQuery.toString());
Returned values are type of Objects so make sure to assign them in List as following way..
List<Object[]> data= selectRecords.getResultList();

Get count from query

I am using mysql 5.5 with openjpa 2.3.0.
I have entities with namedQueries (generated in netbeans - I would like to be able to use this), for example:
#NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
#NamedQuery(name = "User.findByGender", query = "SELECT u FROM User u WHERE u.gender = :gender")
I am creating restfull aplication with paged results. I would like to return for every paged result the Content-Range header as 1-20/250 where 20 is pagesize, 250 total count.
I tried to create a query
entityManager.createNativeQuery("SELECT count(1) FROM (" + namedQuery.toString() + ") as foo;");
where I could dynamicaly insert any named query and return the count without returning the result list -> it should be faster.
When I execute this, exception occurs
SQL state 42S22: Unknown column 'u' in 'field list'
Executing the query itself in entitymanager is ok.
Can I use the entity manager or criteria builder to create a query for counting results without returning the result list (and without writing for every namedQuery a count duplicate)? thank you for helping.
You are mixing JPQL with native queries. JPQL says SELECT u FROM entity u, SQL would be SELECT * FROM entity u or SELECT col1,col2,col3 FROM entity u
You could write a JPQL named query counting the stuff, e.g. SELECT COUNT(u) FROM entity u. A getSingleResult() would then return an Object[], whose first element contains the count.
Not nice but working. Why do You have to query for the number anyway? Pagination means next = lastindex+pagesize. if next < lastindex+pagesize, the end is reached.

Sql Server Union Query Optimization

I have given a task to optimize the below sql query. Currently the query is timing out and causing a lot of blocking . I just started using t-sql, so please help me with optimizing the query.
select ExcludedID
from OfferConditions with (NoLock)
where OfferID = 27251
and ExcludedID in (210,223,409,423,447,480,633,...lots and lots of these...,
13346,13362,13380,13396,13407,1,2)
union
select CustomerGroupID as ExcludedID
from CPE_IncentiveCustomerGroups ICG with (NoLock)
inner join CPE_RewardOptions RO with (NoLock)
on RO.RewardOptionID = ICG.RewardOptionID
where RO.IncentiveID = 27251
AND ICG.Deleted = 0 and RO.Deleted = 0 and
and ExcludedUsers = 1
and CustomerGroupID in (210,223,409,423,447,480,633,...lots and lots of these...,
13346,13362,13380,13396,13407,1,2);
You can try to insert those IDs to temp table and join it instead of using IN statement.
The key to solving you problem is NOT to fix the SQL, but to fix indexes on your tables. For example, you should have a compound index on the OfferConditions table with OfferID and ExcludedID.
When you create the indexes on the other tables, remember that if the field is in the where OR the join filter, it should be part of your compound index.