Spring Jpa Reository - spring

I prepared the HQL query and stored in string variable.
I am using Spring JPA Repositories to deal with database.
I want to execute this HQL query using spring reprository. But I could able to find the way how to invoke this without #Query annotation.
How to run the HQL queries in Spring JPA without #Query annotation?

There are two ways that you can try.
Use #NamedQuery. Check out this.
Write a custom repository and then use criteria builder to interact with the database.

Related

How to get limited random results using Spring Boot Repositories

I have a database with many fornames and names packed in "Person" Entity. Also I use Spring Boot with #Repository to access the data.
I want to achieve something like that.
give random 5 results.
Is there an easy way to achieve it with Repository Annotations? Without writing native query?

Can we use multiple datasources with jdbi in spring boot project

Can we use multiple datasources with jdbi.
Will the configuration will be same as what we have with JPA : https://www.baeldung.com/spring-data-jpa-multiple-databases
So, in first, you can set more than one database for your application with JDBI, exactely like JDBC. You just have to set them inside your application.properties.
Second, if you want to use JDBI, you'll use a kind of classical queries, instead of a dialect for JPA/hibernate inside repositories.
You can read this discussion to compare them : Benchmarking spring data vs JDBI in select from postgres Database

How to add conditionally annotations on jpa entity hibernate

Have a situation where I need to use single JPA Entity class for different databases.
I have used #Nationalized annotation on fields to support I18N. I need this #Nationalized annotation applicable only when I use oracle as my database. If I use same code for other databases it should not be consider(eg: derby)
is there any mechanism to achieve this in hibernate

With Spring Data JPA, under what circumstances should I use EntityManager directly?

Spring Boot Data JPA does an amazing job of generating repositories and abstracting away the managemet of Datasources and EntityManager etc. But sometimes I see code where the EntityManager is included as a field and accessed directly, like in this Dzone example
I don't really understand from that example, when should I include the EntityManager in my class and interact with it directly and when can I just rely on the repository Spring Data JPA autogenerates? Can someone explain?
One good use of EntityManager that I can think of is when you want to get results from a database using complex queries such as join. You can directly query using EntityManager and use ResultTransformer to get into your custom model.

Can I use JPARepository with an Elasticsearch datasource?

Can I use an ES data source with JPA Repository? The main reason I ask is that I would like to use the JPA Specification executor for queries.
Thanks.
No. Specificationand JpaSpecification are based on JPA queries which are different than Spring Data Elasticsearch queries.

Resources