Joining unrelated tables via JPQL - sql

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

Related

How to create a dynamic query using collection-valued named parameters?

As the title suggests, i'm currently trying to add parts to the JPQL-query using collection-valued named parameters (:queryLst).
Function call:
List<PanelSet> psetLst = setRepository.getMaxZchnrGroupByLeftEight(p.getCustomerNumber(), p.getDrawingNumber(), queryLst);
queryLst:
// Is used to store values from scanned and convert them into parts of a query
ArrayList<String> queryLst = new ArrayList<>();
for (int i = 0; i < size1; i++) {
scanEdvRev = scanned.get(i).toString();
queryLst.set(i, "and left(a.drawingnumber, 8) != left('" + scanEdvRev + "', 8)");
}
SetRepository:
public interface SetRepository extends CrudRepository<PanelSet, Integer> {
#Query("select distinct max(a.drawingNumber) from PanelSet a "
+ "where a.customerNumber = :customerNumber "
+ "and a.drawingNumber != :drawingNumber (:queryLst) "
+ "group by left(a.drawingNumber, 8)")
List<PanelSet> getMaxZchnrGroupByLeftEight(#Param("customerNumber") String customerNumber,
#Param("drawingNumber") String drawingNumber,
#Param("queryLst") ArrayList<String> queryLst);
}
When i run the project i get the following exception:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 159 [select distinct max(a.drawingNumber) from com.asetronics.qis2.model.PanelSet a where a.customerNumber = :customerNumber and a.drawingNumber != :drawingNumber (:queryLst) group by left(a.drawingNumber, 8)]
I'm unsure whether my approach to this problem is the correct way of doing this and whether this exception is caused by a simple syntax error or by my usage of collection-valued named parameters.
I've followed this guide trying to solve the problem.
EDIT: I'm basically trying to add each String from ArrayList<String> queryLst to the parametrized query inside setRepository.
#Query("select distinct max(a.drawingNumber) from PanelSet a "
+ "where a.customerNumber = :customerNumber "
+ "and a.drawingNumber != :drawingNumber (:queryLst) "
+ "group by left(a.drawingNumber, 8)")
If successful, the query behind the function
List<PanelSet> getMaxZchnrGroupByLeftEight(#Param("customerNumber") String customerNumber,
#Param("drawingNumber") String drawingNumber,
#Param("queryLst") ArrayList<String> queryLst);
should look like this:
queryStr = "select distinct max(a.drawingNumber) from PanelSet a "
+ "where a.customerNumber = " + customerNumber + ""
+ "and a.drawingNumber != " + drawingNumber + "";
for (String s : queryLst) {
queryStr = queryStr + s;
}
queryStr = queryStr + " group by left(a.drawingNumber, 8)";
I hope this clarifies what i'm trying to do with queryLst.
It can't be done using your approach of passing in a list of query chunks.
The closest you'll get is by adding every possible condition to the query and provide values for all those conditions in a way that allows conditions to be ignored, typically by providing a null.
You code might look like this:
#Query("select distinct max(a.drawingNumber) from PanelSet a "
+ "where a.customerNumber = :customerNumber "
+ "and a.drawingNumber != :drawingNumber "
+ "and a.myTextColumn = coalesce(:myTextColumn, a.myTextColumn) "
+ "and a.myIntegerColumn = coalesce(:myIntegerColumn, a.myIntegerColumn) "
// etc for all possible runtime conditions
+ "group by left(a.drawingNumber, 8)")
List<PanelSet> getMaxZchnrGroupByLeftEight(
#Param("customerNumber") String customerNumber,
#Param("drawingNumber") String drawingNumber,
#Param("myTextColumn") String myTextColumn,
#Param("myIntegerColumn") Integer myIntegerColumn);
Passing null for myTextColumn or myIntegerColumn will allow that column to be any value (except null).
You'll have to find SQL that works for the type of conditions you have and the data type of the columns involved and whether nulls are allowed.
If passing nulls doesn't work, use a special value, perhaps blank for text columns and some "impossible" date like 2999-01-01 fir date columns etc and code the condition like:
and (a.myCol = :myCol or :myCol = '2999-01-01')

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

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!.

Hibernate, SQL server and #SqlResultSetMapping with native query

I'm working with a very old database and I've to retrieve some specific data.
I use sql server and hibernate. I've written a class named Language with a guid (id of a person) and a set known languages. I've to retrieve all languages known by each person in the db, but I don't know if I can fill the languages set with a specific native query.
#NamedNativeQuery(
name = "queryLanguages",
query = "SELECT c.GUIDPersona as guid, d.Caption as language" +
" FROM " +
" Competenze c " +
" INNER JOIN " +
" DomainItemsCaption d " +
" ON (c.IdConoscenza = d.ItemID) " +
" WHERE " +
" d.DomainName = 'Conoscenza' AND " +
" d.Caption LIKE 'Lingue ~ %'",
resultSetMapping = "Languages"
)
#SqlResultSetMapping(
name = "Languages",
entities = {
#EntityResult(
entityClass = Language.class,
fields = {
#FieldResult(name="guid", column="guid"),
#FieldResult(name="languages", column="language")
}
)
}
)
public class Language {
private String guid;
private Set<String> languages;
...
}
Can I fill the "languages" field with a specific native query retrieving all known languages for the person associated to the entity?

Entity Framework - how to join tables without LINQ and with only string?

I have a question about Entity Framework. Please answer if you know answer on this. I have such query :
String queryRaw =
"SELECT " +
"p.ProductName AS ProductName " +
"FROM ProductEntities.Products AS p " +
"INNER JOIN CategoryEntities.Categories AS c " +
"ON p.CategoryID = c.CategoryID ";
ObjectQuery<DbDataRecord> query = new ObjectQuery<DbDataRecord>(queryRaw, entityContext);
GridView1.DataSource = query;
GridView1.DataBind();
Particularly I want to join few tables in one query, but I can NOT use LINQ and can NOT use ObjectQuery with objects mapped to DB fields inside my query. Because each entity creates dynamically. So this is what i can NOT use :
msdn.microsoft.com/en-us/library/bb425822.aspx#linqtosql_topic12
msdn.microsoft.com/en-us/library/bb896339%28v=VS.90%29.aspx
The question is can I use something like this instead of using objects?
query.Join ("INNER JOIN CategoryEntities.Category ON p.CategoryID = c.CategoryID ");
The purpose is to use Join method of ObjectQuery with syntax as in Where method :
msdn.microsoft.com/en-us/library/bb338811%28v=VS.90%29.aspx
Thanks, Artem
Any decision i see right now is to temporary convert ObjectQuery to string, add joined table as string and then convert it back to ObjectQuery :
RoutesEntities routesModel = new RoutesEntities(entityConnection);
String queryRaw = "SELECT " +
"rs.RouteID AS RouteID, " +
"rs.LocaleID AS LocaleID, " +
"rs.IsSystem AS IsSystem " +
"FROM RoutesEntities.Routes AS rs ";
_queryData = new ObjectQuery<DbDataRecord>(queryRaw, routesModel);
var queryJoin = _queryData.CommandText + " INNER JOIN LocalesEntities.Locales AS ls ON ls.LocaleID = rs.LocaleID ";
_queryData = new ObjectQuery<DbDataRecord>(queryJoin, routesModel);
Maybe someone has more consistent suggestions?
Finally I Found a better solution for this, we can use Sub Query inside main Query. For example :
var db = CustomEntity();
ObjectQuery<Categories> query1 = db.Categories.Where("it.CategoryName='Demo'").Select ("it.CategoryID");
var categorySQL = query1.ToTraceString().Replace("dbo", "CustomEntity"); // E-SQL need this syntax
ObjectQuery<Products> query2 = db.Categories.Where("it.CategoryID = (" + categorySQL + ")");
Some example is here :
http://msdn.microsoft.com/en-us/library/bb896238.aspx
Good luck!