Query relational database with Spring Data JPA and Hibernate - spring

I am new to Spring and Hibernate. I have a project using Spring, Hibernate and PostgreSQL. I know that I can query the database using Spring Data JPA or Hibernate, but I don't know what the benefits and disadvantages of each approach. I also know that in the background, Spring Data JPA will call Hibernate. So what is the best way I should use to query the database to get the best performance, Spring Data JPA or Hibernate or depending on the specific usage situation. Thanks very much

I think it is based on the requirements of your project.
You can find more answers to this question at StackOverflow.

Related

Difference between Apache Open JPA and Spring JPA

I would like to know what's the key difference between Apache Open JPA and Spring JPA.
Spring already has a mature JPA for dealing with all kinds of Java persistence but still saw few projects in my company where they uses Apache Open JPA.
Can we integrate Apache Open JPA with Spring. Also like to know what are key benefits of Open JPA.
for example ElasticPath uses Spring but for JPA they uses Apache Open JPA
First: There is no Spring JPA.
There is spring-orm which is one of the many artifacts published by the Spring Framework. It contains classes dealing with JPA and it's main implementations (Hibernate and EclipsLink) in order to integrate them into the rest of the framework. Most Spring users rarely deal with it directly.
You are probably thinking of Spring Data JPA which offers repositories implemented with JPA, which in turn offer many ways to declare queries: Query derivation from method names, named queries based on method names, annotated queries, query by example, specifications ... It is part of the Spring Data project, which offers similar features with many different persistence technologies (JPA, MongoDb, Couchbase, Elasticsearch, Jdbc, Redis, Ldap ...). Spring Data JPA uses spring-orm
Since it got mentioned a couple of times now it is time to explain JPA: JPA stands for Java Persistence API and is an API which can and is implemented by multiple vendors. Hibernate is the most popular implementation, EclipseLink is the reference implementation and Apache Open JPA is another one.
Spring Data JPA (and spring-orm) are (mostly) based on JPA and therefore you should be able to use Apache Open JPA with it. But development of Open JPA was so slow in recent years that the Spring Data Team dropped OpenJPA from the JPA implementations it tests against.
To get a feeling for the development speed, you might look at the releases from the last three years (2019-2021):
Hibernate: 32 (not counting alpha and beta releases)
EclipseLink: 12 (not counting Milestone and release candidates)
OpenJPA: 4
As for the benefits of Apache Open JPA, I consider that an opinion question and therefore off topic for SO. But since people in your company seem to use it, I suggest asking them why they chose Open JPA over the other implementations.

HikariCP and Spring JPA

I was reading the documentation of Spring Boot and HikariCP and would like to integrate it to my SB app, but I got a little confused when it comes to Hikari and JPA.
I'm currently using Spring Data (spring-boot-starter-data-jpa) and my questions are: can I use HikariCP when using JPA? will it interfere on the way Hibernate connects to database? should I do some other configuration to make Hibernate using HikariCP? it wasn't really clear on documentations
There is no issue with using JPA and HikariCP together. Depending on which JPA implementation you want to use you can look around a bit more, but generally Hibernate and EclipseLink are supported. For example here is an example using the exact set of technologies you described:
HikariCP + JPA + spring-data

Spring data required hibernate or not

I have one doubt about implementation of spring data, have basic knowledge about spring data.
I understand JPA and Hibernate, how it work. Hibernate provide the implementation for JPA.
My questions, Can we work alone with Spring data to persist data in mysql or we need some provider like hibernate or toplink etc for midsize application
The structure is as follows:
Spring Data JPA
|
JPA
|
Hibernate
You need Hibernate as an JPA implementation, but from your perspective you should only see Spring Data JPA.
When designing your entities if you make sure that you use only annotations from the javax.persistence package you will not depend on one concrete JPA implementation (in this case Hibernate) but theoretically you could swap Hibernate for EclipseLink or something else.

Spring roo and jOOQ

jOOQ should be an alternative to standard JPA. Is there any way to use it in Roo as JPA provider? Is it even possible?
The reason for this: We have Oracle database and jOOQ has quick and easy set up for connection to that database. On the other hand, Roo is very usefull tool but lacks on setting up Oracle database connection.
jOOQ doesn't implement JPA. It just happens to support a few JPA annotations, which is far from actually implementing JPA. In other words, no you cannot use jOOQ as a JPA provider in Spring Roo

Spring Framework Saving Data to Multiple Tables

Newbie here...
I'm working on a Spring MVC app and their JDBC api for data access. How would I go about saving data to multiple tables? The insertion needs to be such that if there's an error or something goes wrong, nothing gets inserted and rolled-back. Would this be Spring's transaction support? If so, the official documentation for transaction support is very confusing to me. Does anyone have a good source for learning how to do that?
I'm using Spring 3.1, Oracle 11g, and Tomcat 6.0
Yes, spring supports transaction. You can use DataSourceTransactionManager to configure your bean.IF you find xml mapping confusing you can use annotation #Transactional. Spring's annotation support for transactions are really simple. Spring in Action book has examples for transaction management.

Resources