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.
Related
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.
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
}
I am still a beginner in hibernate.I have started reading the user guide in which i found this architecture.
I know that hibernate is a jpa implementation and the jpa jar contains only interfaces.But i want to understand why JPA API is in the same level as Hibernate native api.
And if JPA contains only Interfaces how can we call for example entityManger.persist(entity) and normaly the entity manager is an interface.
where is the entity manger implementation ???
I know that hibernate is a jpa implementation and the jpa jar contains only interfaces.But i want to understand why JPA API is in the same level as Hibernate native api.
Because you can either use the JPA API (EntityManager, EntityManagerFactory etc) or Hibernate native API (Session, SessionFactory etc) to interact with the ORM entities and the database.
And if JPA contains only Interfaces how can we call for example entityManger.persist(entity) and normaly the entity manager is an interface. where is the entity manger implementation ???
JPA API like EntityManager, EntityManagerFactory etc are implemented by Hibernate (one of the JPA implementations, other like EclipseLink etc do exist). You will find that implementation in one of the hibernate jar files. .
A quick ref of Hibernate implementation of EntityManagerFactory here on grepcode
Is it possible to use hibernate-search only for it's annotations (bean => document/document => bean mapping), without using a database at all? If so, are there any online samples that show basically how to set this up?
I found the following: http://mojodna.net/2006/10/02/searchable-annotation-driven-indexing-and-searching-with-lucene.html, but I'd prefer hibernate-search if it supports my use case.
Hibernate search 3.4 has decoupled the query engine from Hibernate Core. For instance, Hibernate Search is reused to implement queries with Infinispan. I don't know if the code is packaged so that you could use HS with, let's say Spring and JDBCTemplate (something I would like to do). That's a lead I will investigate later but maybe you can check it out...
Starman is correct, Hibernate Search in version 3.4 is abstracting the search engine from Hibernate Core, and the Infinispan Query is an integration example which works fine without a database. There would be no problems with Spring either, but you'd need to make sure to send update event to the query engine so that the index doesn't get out of synch. When using Hibernate the advantage is that it transparently listens for changes to the database and applies them to the index at transaction commit, so the index is always in synch (or close, if configuring Search to use async backends).
I'd suggest to look into the code of Infinispan Query, as it's very small and just delegating calls to expose an Infinispan flavoured API. Most of the code is tests or integration to properly manage the lifecycle of the engine: start and stop it together with Infinispan.
I don't think that's possible because when you enable Hibernate search you are enabling that on a Entity and that Entity has references to the table and the search index.
I have a Spring/Roo application which uses PostgreSQL and Hibernate.
As is appropriate, the connection information is located in the database properties file
src/main/resources/META-INF/spring/database.properties
Unfortunately, I have a situation where querying the database through Hibernate is draining the resources too much. I am sure that I can extract the database information (url/username/password) from the file listed above, but I am not sure where to begin my search.
Is there a manual or otherwise where I can find this information?
If you wish to bypass Hibernate to write more efficient queries by hand, you don't have to make separate connections to do it, and should not do so.
Get a Hibernate session and unwrap it to get the underlying java.sql.Connection. Or use native SQL via Hibernate's own interfaces.
That way you still get to use the useful bits of Hibernate, like the connection pooling integration. Sharing the same connection pool as Hibernate will improve efficiency, and you'll have a lot less extra code if you do it this way.
I haven't used Spring Roo, so I can't speak specifically for it. Here's info for Hibernate used via JPA or here. For direct Hibernate usage where you have a Session object, use Session.connection() on old versions of Hibernate, or the Work interface on newer versions:
Session.doWork()
session.connection() deprecated on Hibernate?
How to get jdbc connection from hibernate session?
Alternative of deprecated hibernate getSession().connection()
If you insist on doing this by hand anyway, start with ClassLoader.getResource(...).