JPQL 'Function' doesn't send operator - mysql

I am using EclipseLink over Spring Boot and have this jpql in a repository:
#Query("select function('pMonth',p.payDay) ,sum(p.price) " +
"from FullPayment p where " +
"group by function('pMonth', p.payDay)"
)
List<Object[]> paymentMonthlyHistory();
but it does not send the operator p.payDay calling my custom function pMonth. Here is the database error:
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect number of arguments for FUNCTION adventure_db.pMonth; expected 1, got 0
Error Code: 1318
Call: SELECT pMonth(), SUM(PRICE) FROM panel_fullPayment WHERE ((PAYDATE >= ?) AND (TRANSACTIONAMOUNT >= PRICE)) GROUP BY pMonth()
bind => [1 parameter bound]
I use Mysql as DBMS. Any help would be appreciated

The problem was a typo. The field name is payDate but I wrote p.payDay. I put the answer here to emphasize that if you mistype the field name here, it won't show you an error on boot, but will generate a wrong query.

Related

How to print query of Django ORM

I am using Django ORM query with Extra params. when I try to print the SQL query relevant to that ORM Query,i am getting the below Error message.
ORM Query:
Record = SAMPLE_TABLE.objects.extra(where=["REPLACE(Message,' ','') "+whereCaseSensitive+" like %s "+query],params=[duplicateCheckMessage]).filter(~Q(iStatus=2),~Q(iAppStatus=2),iEntityID=entityId,iTemplateType=1).first()
Message - FieldName ,
whereCaseSensitive - '',
query - ( FIND_IN_SET("test",Testfield))
I am trying to fetch the sql query related to this using
print(Record.query)
when i run this i am getting Exception as 'NoneType' object has no attribute 'query'
Can any one help on this ?
You can't print the query for the first (or any) element of a queryset because it is an instance, not a query. To print .query you need to do it on the queryset
Try this
my_query = SAMPLE_TABLE.objects.extra(*some_extra).filter(*some_filter)
print(my_query.query)
record = my_query.first()

if null return blank in spring data jpa Query

I have the below #Query in my repository
#Query("select new Foo(id, code, coalesce(properties, '') as properties, env) "
+ "from Foo order by id desc")
public List<Foo> findAll();
I want to return blank for properties when its null.
But when I run the service I am getting below error:
unexpected token: as near line 1, column 87
Any idea on how to resolve this error? Thanks in advance.
The error is coming from trying to declaring an alias in a constructor expression. Remove the as properties.

SQLSTATE[22023]: Invalid parameter value: 3037 Invalid GIS data provided to function mbrcontains

(Hi !)
I'm not very good at SQL and I didn't find any solution on internet, I have the following error (with Symfony4)
SQLSTATE[22023]: Invalid parameter value: 3037 Invalid GIS data provided to function mbrcontains.
My request is here :
$sql = '
SELECT * FROM rent_release r
WHERE CONTAINS("date", :yearRequested)
';
date is defined later because it's how it works with doctrine:
$stmt->execute(['yearRequested' => $year]);
Does anyone know what's the problem ?
Someone helped me on Symfony slack
SELECT * FROM rent_release r WHERE YEAR(`date`) = :yearRequested

DRUPAL PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

I do not know what I am doing wrong here? Can someone please help me? When the following query is executed in Drupal7 custom module, I get the following error:
ERROR:
ResponseText: PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens:
SELECT t.tid AS tid, t.name AS name
FROM
{taxonomy_term_data} t
WHERE (t.vid = :vid) AND (LOWER(t.name) LIKE LOWER('%%:last_string%%'))
LIMIT 50 OFFSET 0;
Array
(
[:vid] => 6
[:last_string] => server
)
CODE:
$result = db_select('taxonomy_term_data', 't')
->fields('t', array('tid', 'name'))
->where('t.vid = :vid', array(':vid'=>$vid))
->where("LOWER(t.name) LIKE LOWER('%%:last_string%%')", array(':last_string'=>$last_string))
->range(0, $num_results)
->execute();
The query works if I directly hard code the value for :last_string,
Example:
->where("LOWER(t.name) LIKE LOWER('%%server%%')")
any help is much appreciated..
Try using only one % because: % is a substitute for zero or more characters. You don't need 2 of them.
The LOWER function takes a string as parameter and '%:last_string%' is taken as string not as a binding to the array(':last_string'=>$last_string), that's why it works when you remove the binding. So try to not put :last_string inside the LOWER function because it won't recognize it as a binding.

Simple difficulty with HQL Query

I am having a trouble with executing an HQL query like this:
select new myPackage.view.CoverDocumentReportView(Re.code AS fulCd,
Re.creditPrice AS crtprc,
Re.debitPrice AS dbtprc,
(Re.debitPrice - Re.debitPrice) AS redbtprc,
(Re.creditPrice- Re.creditPrice) AS recrtprc,
(Re.debitPrice-Re.creditPrice) AS rem)
from
(select fullCode as code,
sum(creditPrice) as creditPrice ,
sum(debitPrice) as debitPrice
from DocumentMaster DM,
DocumentAccount DA,
Tree T ,
AccountTree AT,
DocumentDetailed DD
where DM.id = DA.documentMaster and
DA.accountTree = T.id and
DA.accountTree = AT.id and
DD.documentAccount = DA.id
group by DA.accountTree ) As Re
1)
If I execute this like:
SQLQuery crit = (SQLQuery) session
.createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(CoverDocumentReportView.class));
ArrayList<CoverDocumentReportView> li = (ArrayList<CoverDocumentReportView>) crit.list();
ERROR 2012-12-22 14:16:19,838 [http-8080-1] org.hibernate.util.JDBCExceptionReporter : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.datx.web.accounting.view.CoverDocumentReportView(Re.code AS fulCd,
Re.creditP' at line 1
2)
If I execute it with this:
Query query = session.createQuery(sql).setResultTransformer(Transformers.aliasToBean(CoverDocumentReportView.class));
ArrayList<CoverDocumentReportView> li = (ArrayList<CoverDocumentReportView>)query.list();
The error will be:
ERROR 2012-12-22 14:51:46,709 [http-8080-1] org.hibernate.hql.ast.ErrorCounter : line 1:224: unexpected token: (
ERROR 2012-12-22 14:51:46,709 [http-8080-1] org.hibernate.hql.ast.ErrorCounter : line 1:308: unexpected token: sum
What is the problem?
SQL and HQL are two different languages.
HQL doesn't support subqueries in from clauses, so this query can't be an HQL query.
And SQL doesn't know about Java objects, and doesn't have any new() function allowing to create them, so the query is not a valid SQL query either.
Make it a valid SQL query, execute it using createSQLQuery(), then iterate through the results and create instances of your objects from the returned rows. Or use a result transformer as you're doing, which will do that for you. the result transformer will use the aliases you assigned to the returned columns of the SQL query to create beans for you. You don't need any new CoverDocumentReportView() in the query to make that work. Read the javadoc for details.