How I can handle a field in PostgreSQL whit datatype 'jsonb'.
I want to map it to an entity and use it whit a JPQL Query (check if a field exists with the given ID)
It should be performant as possible.
Related
This question already has answers here:
How to replace table name with parameter value while using Spring Data JPA nativeQuery
(5 answers)
Closed 3 years ago.
I am using Hibernate and Spring data JPA to build a web project.
In Eclipse-Link we can use native query like
String tableName = "sometablename";
String query = "SELECT * FROM " +tableName +"WHERE id > 10";
In Hibernate I am using #Query annotation
#Query(value = "SELECT COUNT(r.id) as resultsCount FROM #{#resultTable} r WHERE r.customerId= :customerId AND r.is_deleted=0 AND r.is_ignored=0 ", nativeQuery = true)
Integer getResultsCount(#Param("customerId") int customerId,
#Param("resultTable") String resultTable);
I tried #{#resultTable} but this is getting replaced as a string with quote and i am getting an exception that
You have a error in your SQL Syntax
I want to use table name dynamically from params. Is it possible? And if yes, Please tell me how?
It's not possible, #org.springframework.data.jpa.repository.Query takes only jpql, you cannot pass the name of the table since it's not recognized as any entity.
It states the javadoc of Query:
/**
* Defines the JPA query to be executed when the annotated method is called.
*/
String value() default "";
The best solution would be not to pass tablename as string, but resolve it using for example inheritance (link) or rebuild your datamodel somehow. As a quick and dirty solution I would suggest creating a custom repository and using EntityManager.createNativeQuery and pass the sql there. But remember to validate the query you're composing (validate user input, use enums for table names), because it can lead to sql injection.
Your own observations actually answer your question:
I tried #{#resultTable} but this is getting replaced as a string with quote and i am getting an exception
The placeholders which are used inside the #Query query string are intended to filled with literal values. Hence, the table name ended up appearing as literal string, inside single quotes. This means that behind the scenes #Query and Spring are probably using a JDBC prepared statement. It is not possible to bind the name of the table. Allowing this would be a major security hole.
The only possible workaround here would be to concatenate your query string together, and then trying to use that string with #Query. But note that this would not be a safe option.
What you are doing is wrong, you are mixing business logic into DAO layer, i suggest you create two DAO's, each one with its own table and query, then into the business/service layer call the desired one.
In Room 2.4, there is a new feature called relational query method in DAO which you can write your custom query to select columns from 2 entities and Room can be able to aggregate into Map<TableA, List<TableB>> return type.
I have a fairly complicated query which do left join with nested queries to return a map of railway stations and their associated rail lines (many-to-many relationship). I tried to make a #Query method returns Flow<Map<RailStation, List<RailLine>>> and it can return the map that I want.
Now, I want to go one step further to make it returns paging 3's PagingSource. As the original type is a Map, so I think I should make the paging #Query method as PagingSource<Int, Map.Entry<RailStation, List<RailLine>>>. (Map.Entry should be the representative type of a single list item rather than Map as it represent the whole query result.) However, the Room annotation processor complainted about this line saying that it cannot handle this type:
[ksp] RailStationDao.kt:130: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer, java.util.Map.Entry<RailStation, java.util.List<RailLine>>>).
So my question is: does the Room annotation processor and Paging 3 support for relational query method with paging 3? If not, is there any alternative way to archive the same goal? It seems like the #Relation annotation in Room can only support for simple table joining, but my case is I need to write nested query in the LEFT JOIN clause.
I am writing an application which contains an entity with property named addedDate of type Date.
I have written an interface which extends MongoRepository and defined a method named
findByAddedDateBetween(Date startDate,Date endDate);
But when I query with 2 dates the output only contain list entities between the dates,Not including the entities whose addedDates is the queried dates. I want to include the boundary values also.
According to Spring JPA documentation for MongoDB, the between keyword is not inclusive in Mongo DB if you use 2 parameters.
As an alternative, you can use the spring Range class. You must use this method in order to include boundaries.
findByAddedDateBetween(Range<Date> range)
Where Range is declared like this :
Range<Date> range = Range.of(Range.Bound.inclusive(dateStart), Range.Bound.inclusive(dateEnd));
I have a table where the date field is a varchar. Since it is a legacy application I do not want to change the datatype. I want to write the following query in criteria. Any help will be highly appreciated
select * from ucms_vu vu where vu.case_e_dt is not null and to_date(vu.case_e_dt,'YYYY-MM-DD') = to_date('2018-02-02','YYYY-MM-DD')
Using purely JPA, I believe that is not possible. Because you are wanting to do a specific manipulation in a not manipulable data.
If you use Hibernate too, #Formula annotation resolves your problem.
You can create a attribute in your ucms_vu entity:
#Formula("to_date(case_e_dt,'YYYY-MM-DD')")
private Date caseE;
And your criteria would use this attribute to perform the query.
For Example:
Criteria criteria = session.createCriteria(Ucms_vu.class, "ucms");
criteria.add(Restrictions.eq("ucms.caseE", new Date("your date 02/02/2018"));
criteria.list();
I am working with a legacy system which requires doing a query for a partial match on a number field. So I want to do something like
where ID like concat(:num, '%') where num parameter is of the type Long. Is there a way to do that using Spring Data JPA derived queries. If the id was a string then I can simply do
findByIdContaining(String id)
I would appreciate any help.
No, this is not possible with derived queries (those where the query is derived from the method name). But it's easy with an annotated #Query annotation.