I want to aggregate over a table that has a boolean column which I want to combine with a logical OR.
In plain SQL (postgres) I would write something like:
sql
SELECT t.agg_column, bool_or(t.bool_column) FROM mytable t GROUP BY t.agg_column;
How can I reflect that in a (non-native) Spring Data #Query annotation? I am looking for something on the lines of:
java
#Query("t.agg_column, bool_or(t.bool_column) FROM mytable t GROUP BY t.agg_column;")
but that doesn't work, since it doesn't know bool_or.
Related
I want to write a recursive query in SQL Server. So I am using CTE. Query is working properly in SSMS but when I am trying to use the same CTE in JPA as native SQL it gives an error:
Invalid name column Id.
The entity (which I am using in CTE to fetch data recursively) has #Id #Column(name="pk_id") private int Id field.
I also followed this SOQ : cte sql to simple subset query for JPA
But still getting error as invalid name column Id.
I have never used CTE before. How can this be fixed?
You can write the SQL query in JPA Repositories since the #Query annotation takes native query as well. For that, you need to specify the SQL query in the value parameter and nativeQuery is true as follow.
You can write CTE queries as well.
public interface ISomeRepository extends JpaRepository<Entity, Long> {
#Query(value = "SQL QUERY NEED TO BE WRITTEN HERE", nativeQuery = true)
List<Long> getEntityIds();
}
I wanted to perform the Spring JPA repository where wanted to apply the and operation among 2 columns where one column cloud have multiple values in it.
SQL query for the same:
select * from table_name where col1='col1_val' and col2 IN
('col2_val_a','col2_val_b','col2_val_c');
I know that for and operation I can extend the JpaRepository and create the method with like this for:
List<MyPoJoObject> findByCol1AndCol2(String col1_val,String col2_val);
and for IN operation we can use : findByCol2In(Collection<String> col2_val)
But i did not know how i can club both the mentioned JPA default method into one, as per my sql statement mentioned before.
You can use the following method named:
List<MyPoJoObject> findByCol1AndCol2In(String col1_val, Collection<String> col2_val);
On this link repository-query-keywords you can find repository query keywords that you can use and combine them as well.
You can certainly combined both into one method.
List<MyPoJoObject> findByCol1AndCol2In(String col1_val,String[] col2_val);
Try this. I am not sure if it will accept Collection<String>. I will try that and update the answer.
HTH.
If you want to perform this logic for more than two columns then your method name becomes verbose.
Instead of stuck with Spring naming why can't you write your own JPA query.
Example:
#Query("select pojo from MyPoJoObject as pojo where pojo.col1 = :col1_val and pojo.col2 in :col2_val")
List<MyPoJoObject> findByColumns(String col1_val, List<String> col2_val);
When I try to get all the rows from a table using my JPA repository the query Hibernate is generating is like this:
Select field1, field2, ... from ... where (field1, field2) IN (select f1, f2 from ....)
It is working properly when my db is PostgreSQL. Now I am about to migrate to Ms SQL Server. My repository looks like this
#Override
public List<VesselVisit> getAllVisits() {
return this.visitsRepository.findAll();
}
The data model has a parent entity that and has a relationship OneToMany to the children using a composite key.
It is not possible to implement this using SQL Server?
You can Use Inner Join Instead of In Operator in SQL Server For achieving it.
I have my service up and running but there's still a few things i can't figure out.
I have a query that is similar to the following
#Query("SELECT t FROM Tablename t")
Then hibernate will generate the following query
Hibernate: select tname.column1 as a, tname.column2 as b, tname.column3 as c, tname.column4 as d from tablename tname
The problem is that Tablename is case sensitive when i query against a mysql database. Is there a way in hibernate to execute the query exactly the way it is spelled out in the annotation? Also, is it possible to stop hibernate from breaking camelcased columns into two works. For example if i had a column named columnOne hibernate will want to generate a column with the name column_one.
I know this more than likely has something to do with hibernate's naming strategy but i haven't been able to find a solution.
Try adding the following in your application.properties file.
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Documentation about naming strategies are here
Currently i am using the CreateSQLQuery query model to read the data from database using HIbernate. Now, I want to modify my query by using either HQL or Hibernate Criteria. My query looks like as follows.
select concat(d.AREA,' ',d.CITY) as location, a.TRANSFERRED_DATE as ActualTransferDate, concat(c.SCAN_CODE,',',c.SERIAL_NO) as ScanserialCode, c.MODEL_NO as ModelNum, c.ASSET_NAME as AssetName from table_transfer a, table_category b, table_asset c, table_location d where a.ASSET_ID = c.ASSET_ID and b.ASSET_CATEGORY_ID = c.ASSET_CATEGORY_ID and a.TRANSFER_TO_LOCATION=d.LOCATION_ID"
I am not sure how can i can convert this to Hibernate SQL or Criterion based query. Can any one help me?
You can introduce the fields for location and scanserialCode in your entity and mark them as #Formula
e.g.
#Formula("concat(d.AREA,' ',d.CITY)")
private String location;
See an example here or here
Then use join and where in HQL or Hibernate Criteria