How to prevent adding default table alias in hibernate #Joinformula - spring

In my application I am using the following #JoinFormula
#JoinFormula(value="(select s.\"Id\" from \"Student\" s where ash.\"Class_Id\" = id)" )
However, at runtime the generated query is:
(
select
s.cla0_."Id"
from
cla0_."Student" s
where
s.cla0_."Class_Id" = cla0_.id) as formula4_0_,
I need to prevent adding cla0_ table alias to the formula query. How could it be done in hibernate?
I am using it in spring boot, and postgres as database.

Related

Spring boot #Query ignores #Table on view call

I have a spring boot 2 application that works great with entities that are mapped to a table that is in a MariaDB. I now do a view call and I have mapped that entity to the view using the #Table(name="ViewExpiredAccounts") annotation and now when the method is called the table name is ignored. I have a JpaRepository with this method:
#Query(value = "SELECT v FROM ViewExpiredAccounts v")
List<ViewExpiredAccount> expiredAccounts();
When I call this method I get an error:
Table 'view_expired_accounts' doesn't exist
The query SHOULD! translate the table name so that the eventual SQL query sent to MariaDB is: SELECT * from ViewExpiredAccount however it doesnt do that. Is this a bug in Spring??
did you reset the hibernate naming strategy?
if yes, the following property might help
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.jpa.hibernate.ddl-auto=none issue

enter image description here
I am using a schema.sql and data.sql to insert data in h2 database.
each time I run the app, i need to manually run the insert command in h2 console to insert the data. I have one registartion form link in home page for "candidate". I fill data in the form and the data is added in the database. It is working fine in this case.
But I want to prepopultate the data. so I used
spring.jpa.hibernate.ddl-auto=none, now the data in data.sql is being prepopulated, but when I fill the data in registration form after submitting the data I am getting an error.
could not prepare statement; SQL [select employee0_.email as email1_0_0_, employee0_.first_name as first_na2_0_0_, employee0_.last_name as last_nam3_0_0_, employee0_.password as password4_0_0_, employee0_.role as role5_0_0_ from employee employee0_ where employee0_.email=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
gitlab link:
https://gitlab.com/DipronilDey/Assessment
Please help.
By default, Spring Boot uses SpringPhysicalNamingStrategy, generates "first_name" as column name.
You want "firstName", so you need set physical-naming-strategy explicitly:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
reference:
https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/appendix-application-properties.html#common-application-properties

Change SQL query with a parametric query in Oracle

We have an application that uses a framework that we don't have access to it's java classes. I can just see the .class file in byte-code format. So I can't override that class. At that class we have a native query like below:
"Select col1 from table1 where col2='x'";
I mean where condition is not parametric. As well I don't have access to change the query in the code. the App uses Oracle database and Hibernate as ORM.
The question is
"Is there any way to tell Oracle or Hibernate to change this hardcoded query with a parametric one?"

Using JdbcCursorItemReader and IN clause in the SQL query

I am developing a spring batch application and using the chunk based processing.
For Reader i am using the org.springframework.batch.item.database.JdbcCursorItemReader class.
When configuring the Reader and specifying the SQL property, how can we use the combination of preparedStatementSetter to set values for an IN clause in the SQL?
My SQL:
select * from Employee where employeeId IN (,,,,,,).
How can I dynamically set the values for IN clause using preparedStatementSetter?

How to use table functions in queries with Spring Data

When using Spring Data JPA with Hibernate, what are the options available to write queries that join with table functions.
For example, I'd like to generate queries as described below:
CASE 1: SELECT * FROM getfoo(1) AS t1;
CASE 2: SELECT * FROM getfoo(1) x INNER JOIN tbl1 y on x.id = y.id;
Edit
To elaborate more, I'm using Spring Data for all things CRUD (It works great). However, I need to write complicated queries that join tables with "table functions". Table functions(AKA Table-Valued User-Defined Functions) are database functions that return tabled-values which can be used in the JOIN clause in combination with tables. Postgresql and Sql Server support them.
In the Spring Data realm, which includes much more than JPA, what are the options to consider when writing such queries? Whats the best practice from your experience? user2658013 was kind enough to describe one such approach using the entityManager.reateNativeQuery method.
In my mind here are the options:
JPA
Use #NamedStoredProcedureQuery ( >=JPA 2.1)
Use entityManager.createNativeQuery or #NamedNativeQuery
Non-standard
Use Spring Data's #Query to declare finder queries directly on repository methods.
Use SimpleJdbcCall
Any others?
I believe you are asking about Postgres stored functions. Postgres stored functions are analogous to "Stored Procedures". So I think you are asking how would invoke a stored procedure using JPA? Am I close?
The following pseudo code is derived from details published here (see section on Stored Procures):
http://www.oracle.com/technetwork/articles/vasiliev-jpql-087123.html
EntityManager em = emf.createEntityManager();
SOMEVAL_TYPE result = (SOMEVAL_TYPE)em.createNativeQuery("SELECT getfoo(?1) FROM SOMEDB")
.setParameter(1, SOME_PARM)
.getSingleResult();
In general you can use JPQL with JPA instead of SQL.
Note! The above assumes you have already created the stored function in you Postgres database.
Hope that helps :)

Resources