in my table items has a json column named tag. keeping data like ["tag1", "tag2"] .
i want to select from this table filter with the given tag.
in mysql command line, json_contains works.
select * from items where json_contains(tags, '"tag1"');
but how can i using it in Spring JPA?
#Query(value = "select * from items where json_contains(tags, ?1))", nativeQuery = true)
Page<ItemDO> list(String query, Pageable pageable);
got error
TRACE 21469 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [desc]
WARN 21469 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
ERROR 21469 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : 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 ') limit 10' at line 1
so how can i using json_contains withing Spring JPA?
There are two issues with your implementation:
There as an extra parenthesis at the end of SQL query ), look at what logs say. Instead, you should write it as follows:
#Query(value = "select * from items where json_contains(tags, ?1)", nativeQuery = true)
Having done that, you also need to wrap the method parameter (your query variable) inside double quotations, to exactly match what you tried in MySQL command line console. So, you will call the method as follows:
yourRepository.list("\"tag1\"", PageRequest.of(,10) );
Alternative solution
You may use Spring Data Specifications API to avoid native queries.
Thus, you could do the following:
#Repository
public interface ItemRepository extends JpaRepository<ItemDao, Integer> , JpaSpecificationExecutor<ItemDao> {
default Page<ItemDao> findItemsByJsonTagValue(String jsonTagValue, Pageable pageable){
return findAll((root, query, builder) -> {
final String CONTAINS_FUNCTION = "JSON_CONTAINS";
final String JSON_COLUMN_NAME = "tags" ;
final int TRUE_BIT = 1;
return builder.equal(
builder.function(
CONTAINS_FUNCTION,
String.class,
root.<String>get(JSON_COLUMN_NAME),
builder.literal(String.format("\"%s\"", jsonTagValue))),TRUE_BIT);
}, pageable);
}
}
And then somewhere in your code, you would call the method as follows :
itemRepository.findItemsByJsonTagValue("tag1", PageRequest.of(0 ,10));
Related
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.
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.
SnappyData v.0-5 w/ ClientDriver JDBC driver.
I have a persistent row table in SnappyData called: sensor_data.
From the snappy> shell, this query returns thousands for rows.
snappy> select * from sensor_data where year_num = 2013 and
month_num = 1;
When run from a JDBC connection in SpringBoot, I get this error:
PreparedStatementCallback; uncategorized SQLException for SQL [select
* from sensor_data where year_num = ? and month_num = ?]; SQL state [XCL14]; error code [20000]; (SQLState=XCL14 Severity=20000) The
column position '1' is out of range. The number of columns for this
ResultSet is '0'.
Java Code is:
List<SensorData> list = jdbcTemplateBean.query("select * from sensor_data where year_num = ? and month_num = ?",
new Object[] {year, month}, new SensorDataRowMapper());
What do I need to do to fix this JDBC issue?
Trimmed Stacktrace on Spring boot:
org.springframework.jdbc.UncategorizedSQLException:
PreparedStatementCallback; uncategorized SQLException for SQL [select
* from sensor_data where year_num = ? and month_num = ?]; SQL state [XCL14]; error code [20000]; (SQLState=XCL14 Severity=20000) The
column position '1' is out of range. The number of columns for this
ResultSet is '0'.; nested exception is java.sql.SQLException:
(SQLState=XCL14 Severity=20000) The column position '1' is out of
range. The number of columns for this ResultSet is '0'. at
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:645)
at
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:680)
at
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:707)
at
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:757)
at
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:192)
at
org.kritek.scalability.repository.SensorDataRepository.findByYearAndMonth(SensorDataRepository.java:58)
...
at
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:745) Caused by:
java.sql.SQLException: (SQLState=XCL14 Severity=20000) The column
position '1' is out of range. The number of columns for this
ResultSet is '0'.
...
Caused by: ERROR XCL14: The column position '1' is out of
range. The number of columns for this ResultSet is '0'. at
com.pivotal.gemfirexd.internal.client.am.ColumnMetaData.checkForValidColumnIndex(ColumnMetaData.java:856)
at
com.pivotal.gemfirexd.internal.client.am.ColumnMetaData.getColumnType(ColumnMetaData.java:638)
... 72 more
From the stack it looks like NamedParameterJdbcTemplate is being used but '?' placeholders are being used. For NamedParameterJdbcTemplate you need to use named parameters like here.
I will recommend using the standard '?' placeholder mechanism with JdbcTemplate like:
private JdbcTemplate jdbcTemplateBean;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplateBean = new JdbcTemplate(dataSource);
}
List<SensorData> list = jdbcTemplateBean.query(
"select * from sensor_data where year_num = ? and month_num = ?",
new Object[] { year, month }, new SensorDataRowMapper());
The issue has been resolved. Here is what happened.... SnappyData routed the query to Spark because it determined it could not handle it. Spark knows nothing about JDBC PreparedStatements or bind variables and raised the error. To fix, I had to set the SnappyData JDBC property "route-query = false" in my DataSource configuration. This ensured it was not routed to Spark.
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.
I have the following JPA SqlResultSetMapping:
#SqlResultSetMappings({
#SqlResultSetMapping(name="GroupParticipantDTO",
columns={
#ColumnResult(name="gpId"),
#ColumnResult(name="gpRole"),
// #ColumnResult(name="gpRemarks")
}
)
Which is used like this:
StringBuilder sbQuery = new StringBuilder("Select ");
sbQuery.append(" gpId, ");
sbQuery.append(" gpRole, ");
// sbQuery.append(" gpRemarks ");
sbQuery.append(" FROM v_group_participants_with_details ");
Query query = em.createNativeQuery(sbQuery.toString(), "GroupParticipantDTO");
The view is like this:
DROP VIEW IF EXISTS `v_group_participants_with_details`;
CREATE VIEW `v_group_participants_with_details`
AS
SELECT
gp.id AS gpId,
gp.role AS gpRole,
gp.remarks AS gpRemarks
FROM GroupParticipation gp
;
The GroupParticipation table has the remarks column defined as LONGTEXT (I'm using Mysql 5.x)
Now for the problem:
When the remarks field is commented out from the query everything works perfectly, but if I try to include the remarks field in the query, I get the following error:
javax.persistence.PersistenceException: org.hibernate.MappingException:
No Dialect mapping for JDBC type: -1
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)
What gives? How can I get a LONGTEXT column from a native query?
This problem is reported in HHH-1483 and HHH-3892. In short, Hibernate does not know, how to map a LONGVARCHAR column returned by a native query.
This problem is fixed in Hibernate 3.5.0+. For previous versions, a workaround would be to extend the MysqlDialect to register the correct Hibernate Type for a LONGVARCHAR:
import java.sql.Types;
import org.hibernate.Hibernate;
public class MyMySQL5Dialect extends org.hibernate.dialect.MySQL5Dialect {
public MyMySQL5Dialect() {
super();
// register additional hibernate types for default use in scalar sqlquery type auto detection
registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName());
}
}