Inside my Repository, i have following
public interface TenantRepository extends JpaRepository<Tenant, UUID> {
...
#Query("select substring(email from '#(.*)$') as domain from Tenant group by domain")
public List<String> findAllDomain();
}
When i tried to excute it, i got an error which says:
Syntax error in SQL statement "select substring(tenant0_.email, [*]from, '#(.*)ParseError: KaTeX parse error: Can't use function '\)' in math mode at position 2: '\̲)̲ as c…') as col_0_0_ from tenants tenant0_ group by col_0_0_
So i change the query to this:
"select substring(email from '#(.*)\\$') as domain from Tenant group by domain"
But it got another error:
org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "select substring(tenant0_.email, [*]from, '#(.*)\\$') as col_0_0_ from tenants tenant0_ group by col_0_0_"; expected "INTERSECTS (, NOT, EXISTS, UNIQUE, INTERSECTS"; SQL statement:
select substring(tenant0_.email, from, '#(.*)\$') as col_0_0_ from tenants tenant0_ group by col_0_0_
When i try the query on my PgAdmin4, it works.
Any suggestions?
thx.
I assume you are trying to get all the email domains.
In JPA you can use combination of locate and substring functions to achieve this
#Query("select substring(t.email, locate('#', t.email) + 1) as domain from Tenant t group by domain")
public List<String> findAllDomain();
emails in db
aaa#domain1.com
bbbb#domain2.com
ccccccc#domain2.com
query response
domain1.com, domain2.com
Related
I have Spring boot, Hibernate, and SQL Server.
We use Native queries and they very big, so I can't rewrite it in JPQL or HQL
I need somehow to make Sort order dynamic, I have query similar to:
select * from (select 1 as id, 'Max' as name
union
select 2 as id, 'Mike' as name) as a
order by a.id :sortOrder
:sortOrder hibernate change to ? so I'm getting 'order by a.id ?'
:sortOrder should be ask or desc, but I'm getting an error:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '#P1'
How to make sort order dynamic?
I have a query that uses projections, it works well as long as the return type is List, but it stops working after adding pagination.
Here is the the working code:
#Query("SELECT DISTINCT \n" +
" new com.mycompany.dto.MyDto(me.property1, me.property2, ...) \n" +
"FROM MyEntiry me...")
List<MyDto> findEntities();
I need to extend it adding pagination, so I change it to:
#Query("SELECT DISTINCT \n" +
" new com.mycompany.dto.MyDto(me.property1, me.property2, ...) \n" +
"FROM MyEntiry me...")
Page<MyDto> findEntities(Pageable pageable);
Once I do that the context starts failing because while parsing it inserts select count(me) between SELECT and FROM statements so that the query become invalid:
SELECT DISTINCT
new com.mycompany.dto.MyDto(me.property1, me.property2, ...)
select count(me) FROM com.mycompany.MyEntiry me ...
The context fails with the following exception:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected token: select near line 3, column 1 [SELECT DISTINCT new
com.mycompany.dto.MyDto(me.property1, me.property2, ...) select
count(me) FROM com.mycompany.MyEntiry me ...]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:291)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:186)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:77)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
... 88 more
Question: How to make it work? Is it Spring Data bug?
Note:
The query I added is oversimplified, my real query in fact gathers different values from different tables and I can't implement it without projections
I'm using Spring Boot 1.5.8.RELEASE
Try to use 'native' Spring Data JPA projections.
This should work well:
public interface MyProjection {
String getProperty1();
//...
}
Page<MyProjection> getDistinctAllBy(Pageable pageable);
But, if your query joins many tables you cannot use the pagination without some pain (for example: 1, 2)
UPDATED
Try to add parameter countQuery to #Query annotation:
#Query(value = "select ...", countQuery = "select count(me) from MyEntiry me")
Page<MyDto> findEntities(Pageable pageable);
I want to use spring-batch for retrieving and processing data from a postgres db.
I have a working SQL statement that would give me the full result set (about 400k entries):
private static final String QUERY = "SELECT * FROM MyDataTable ";
Now I want to use the JpaPagingItemReader so that the data is fetched (and written elsewhere) in chunks:
JpaPagingItemReader<MyEntity> reader = new JpaPagingItemReader<>();
reader.setEntityManagerFactory(emf);
reader.setQueryString(QUERY);
But it does not work:
[] 2014-09-17 16:31:58,234 ERROR : QuerySyntaxException: unexpected token: * near line 1, column 8 [SELECT * FROM my_data_table]
I also tried SELECT FROM MyDataTable and SELECT m FROM MyDataTable m without the star. Same result.
So, how can I execute that hql query with spring-batch?
By the way: the query works fine in a sql editor like pgAdmin.
SELECT m FROM MyDataTable m is almost correct (it is valid JPQL query as long as you have entity calles MyDataTable). So, it seems that you don't have entity class named MyDataTable.
As JpaPagingItemReader#setQueryString(String) accepts JPQL queries you should make sure that you have entity class for this table and then you should use its name instead MyDataTable.
By the way - for HQL queries there's HibernatePagingItemReader.
I am trying to execute an hql query with aliases
select **clbs.id as id**
from ClaimDO cl, ClaimBillSummaryDO clbs, HospitalDO h
where clbs.parentGuidObj.id=cl.id and h.id=cl.hospitalSeq and cl.id= '10721'
and I get the following error
org.hibernate.QueryException: , expected in SELECT
However the query runs without error if i remove the alias
select **clbs.id**
from ClaimDO cl, ClaimBillSummaryDO clbs, HospitalDO h
where clbs.parentGuidObj.id=cl.id and h.id=cl.hospitalSeq and cl.id= '10721'
Why are you not using mappings to join your entities? You might as well use native queries to do this. HQL would look more like the following. I have omitted the HospitalDO join since it doesn't look meaningful.
select clbs.id from ClaimDO cl join cl.parentGuidObj clbs where cl.id = :id
I am trying to write query with multiple select subnets in it.But I defined a nativequery
I am giving error. Compiler specifies that "(" after "from" is not proper. How can I define
a native query in JPA 2.0
For eaxmple:
SELECT *
from (SELECT ****C) REI3 where column1 != 1
GROUP BY REI3.column2 order by REI3.column3 ASC
JPA does not have too much to do with validating SQL syntax, query is passed to JDBC driver. Likely you are trying run query such a way, that it is interpreted as JP QL. Instead try following method to execute it as
Query q = em.createNativeQuery("Your SQL here");
Other alternative is to use NamedNativeQuery Example