Call stored procedure in repository without entity - java

I have a service with spring data jpa which uses multiple datasource. One of the datasource is used as the main storage and Entity is used there and repositories are described. Other datasources are used simply to call stored procedures and functions.
Can you please tell me if I can somehow create a spring data jpa repository for datasource without an entity and use the #Procedure annotations to force the function to call the stored procedure I need?
I read about https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations and just wrote my repository implementation. Looking for alternative, more convenient use cases for the spring data jpa api.

You need to use some entity as JPA is domain driven, refer Any way to use the `#Procedure` annotation without an entity?
and
Using Spring #Procedure to call StoredProcedure without binding to a table
In my opinion, you should use Just use JDBC template.

Related

What is the sense of Spring Repositories over EntityManager and/or Jpa Provider like Hibernate?

I wonder what the "big advantage" of a one-to-one relationship Spring Repository is over the EntityManager (persistencecontext) and Transactionmanager (transaction demarcation) (and/or Hibernate. I don't know how they work together exactly in JPA).
Can you explain the advantage of Repository?
It seems very rigid because you have to extend the repository interface for every Aggregate in a Microservice environment at least. I don't know if you have to do this for simple #Entitys too.
Basically, we use a Repository to perform CRUD operations, and it establishes a connection between the program and database to retrieve data, and call it into the service environment to perform some functions, that's the reason why extends the repository in the service and #Repository is the indication that it is a repository.

How to listen CRUD events in spring data repository for Redis?

In my application I've used spring boot repository to CRUD data with Redis. It seems work as expected.
Other wish from my customer is to care about encrypting\decryping some fields. Redis consumes small data objects few fields only.
Sure I can manage this request inside of service which uses org.springframework.data.repository.CrudRepository but I'm looking for more flexible solution. I believe spring data has equivalent for Redis like in similar directions:
EventListener|AttributeConverter - for hibernate
RepositoryEventHandler - for rest
AbstractMongoEventListener - for mongo
Let me know the class name and library for my purpose, please.

Using Hibernate Sessions features along with Spring Data JPA

I know that Spring Data JPA uses Hibernate, however, I have some questions that need clarification.
Will I be able to use other Hibernate features such as C3P0, Hibernate second level caching, or Hibernate sessions if I use Spring Data JPA?
What are the advantages of using #Query for writing custom queries over using hibernate sessions and HQL?
Is there any performance difference between using Spring Data JpaRepository query methods and using Hibernate HQL?
I have seen the other similar questions here and they don't answer these questions.
I know that Spring Data JPA uses Hibernate
No. It uses JPA. Even though Hibernate is the default JPA implementation of Spring Boot, and the most popular JPA implementation, any JPA engine can be used.
Will I be able to use other Hibernate features such as C3P0
C3P0 has nothing to do with Hibernate. It's a connection pool. You can use any connection pool you wnt both with Spring and with Hibernate. Spring Boot uses HikariCP by default, and I would stick to that (it's a very good pool).
Hibernate second level caching
Yes.
or Hibernate sessions if I use Spring Data JPA?
There's really no good reason to use the old, proprietary Hibernate Session API, instead of using the standard JPA API. If you really need to, I don't see why you couldn't use it, but I wouldn't (and never had to).
What are the advantages of using #Query for writing custom queries over using hibernate sessions and HQL?
Query takes a HQL (JPQL, to be exact) query. If you use Query, you use HQL. The advantage is that you just need to declare the query. the binding of parameters, execution of the query, paging, etc. are done for you by Spring. But you can use custom repository implementations and use the native JPA API if you need to.
Is there any performance difference between using Spring Data JpaRepository query methods and using Hibernate HQL?
No.

spring-jdbc vs spring-data-jdbc and what are they supporting

I'm curious what is the difference between the spring-jdbc (what I missing in the newest spring release) and spring-data-jdbc.
Is there a difference or just a renaming (in the repositories I don't see this)?
And is there somewhere described what are the supported targets(DB/JDBC specs/JDK) of the versions?
e.g. for the plain JDBC from oracle I can see that information here:
http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#01_03_1
(e.g.: JDBC Spec 4.1 in ojdbc7.jar on Java7/Java8 on Oracle DB 12.1/12cR1)
But I miss that for spring-jdbc - where do I find that information?
spring-jdbc
The docs for spring-jdbc are basically here:
https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html
Though it doesn't specifically point you to the Spring project spring-jdbc. This project just provides all of the Spring abstractions over the plain JDBC DataSource that you can use with the Spring Framework. For example, Spring's DataSources which nicely hook into Spring's Transaction management capabilities, like the #Transactional annotation.
Also, the JdbcTemplate is part of this module, which allows you to execute SQL statements and extract objects from ResultSets without dealing with exception handling or the nasty details of properly closing statements, connections and the like.
spring-data-jdbc
spring-data-jdbc, on the other hand, provides the Spring Data abstraction over spring-jdbc. That is, you can create a Spring Data CrudRepository and a simple "entity" (not a JPA entity!) and, as Spring Data does, it will create your queries for you without you having to write native CRUD queries over JDBC, as in this example on the spring-data-examples git repo.
Using the referenced example as a demonstration:
interface CategoryRepository extends CrudRepository<Category, Long> {}
The above code is all you could need (using introspection on the Category object name as the source for the table name (based on a NamingStrategy) and it's properties as columns, again similar to JPA, but not using JPA.
Rather than writing your own like so:
#Repository
public class CategoryRepository {
public void create(Category category) {
jdbcTemplate.execute("insert...");
}
// The rest of my other CRUD operations
}

Does spring-data-jpa has a L2 cache implementation?

I am trying to create Repository with L2 cache with minimal setting. My database is postgresql.
I start with spring-boot-sample-data-jpa-archetype project using maven.
I have removed the HSQL and create a DataSource bean to connect to postgresql.
Also use ddl to create schema and imported the initial script data.
I have also added the #Cacheable to my entities.
Then I use unit test to query an entity 10 times using repository. It took 1~49ms. So that leaves me two questions.
Does repository benefits from L1 cache? how do I know if I am hitting the cache or data source?
How to enable L2 cache? does Spring data has its own implementation?
After some testing and the help from #dunni, I will answer my own question.
Spring uses Hibernate as an implementation of JPA. Spring data provides some wrapper over Hibernate. Also need to choose a caching implementation.
To enable L2 cache, add these properties to your project.
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
also add these dependencies:
hibernate-ehcache
Then, add #Cacheable(true) to your entity model class.
Extends a repository interface, Spring will generate the implementation using naming convention. For example
#QueryHints({#QueryHint(name="org.hibernate.cacheable", value="true")})
Entity findByName(String name)
You can also implement the interface. But that will require adding a hint to your query object to activate L2 cache.
query.setHint("org.hibernate.cacheable", true);
To verify the cache is working, you can use the following properties to see if the SQL has been executed.
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

Categories