Hibernate and CRUDRepository Spring Boot - java

I am new to Hibernate and Spring boot. I understand that hibernate is ORM tool so that you can map objects to database and just use save() to save obj into the database automatically. In Spring Boot, I would do something like
public interface CourseRepository extends CrudRepository<Course, String>{
}
and somehow I will get all the CRUD operations like save and findAll etc.
From another tutorial of hibernate, it says that session is required to save the object to the database. However, I don't see any form of sessionfactory or session being implemented in Spring boot and I am still able to do the save and findAll operations etc.
How is this happening and what relations does this have with hibernate?
Where exactly should I start to find out more about hibernate, stuff like #OneToMany and basically understand the details of the whole application to database operations in spring boot? Thanks

Spring Boot takes care of all the session management. Refer to this question for better understanding:
Spring Boot & Spring Data: how are Hibernate Sessions managed?

there are two things crudRepository and sessionFactory. try to find out difference between them and also try to read what is JPA and Hibernate.

Related

Relation between Hibernate and MySQL in Spring Boot?

When we use JPA API in a String Boot project by extending repository class with JpaRepository and use MySQL database instead of hibernate's H2 database, I wanted to know is there hibernate still involved behind the scenes or, is it that JPA works with MySQL?
From what I have researched, Hibernate is implementation of JPA but I can't find any reference how MySQL works with JPA?

what is the difference between spring-boot-starter-data-rest and spring-boot-starter-data-jpa

I am trying to understand various dependencies of spring boot.
I have come across three of them:
spring-boot-starter-data-rest
spring-boot-starter-data-jpa
spring-boot-starter-data-jdbc
I would like to know the difference between the three. Tried searching online documents, which say the three are almost related to spring data. how to resolve the confusion?
To add in here I have also found an another dependency
spring-boot-starter-web-services. I think it supports both SOAP and REST. Its just my assumption, I waiting for an explanation
spring-boot-starter-data-jpa is used to access your database with JPA (Java Persistence API)
spring-boot-starter-data-jdbc is used to access your data with jdbc (Java Database Connectivity)
the difference between JPA and JDBC is the level of abstraction. JDBC is more low level, JPA is more 'magic'
and spring-boot-starter-data-rest is used to provide Rest Endpoints on top of your Spring Data repositories.
To do this, you just annotate your spring data repository with a RepositoryRestResource annotation and direct Spring MVC creates the Restful endpoints.
#RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(#Param("name") String name);
}
This is very handy if you do very CRUD like (Create, Read, Update, Delete) applications.
JPA means "Java Persistence API". It's for querying or saving data in relational databases using object-relational mapping.
REST means "REpresentational State Transfer". It's a style for creating web services that leverages HTTP verbs, a simpler alternative to SOAP.
JDBC starter uses the Spring JdbcTemplate to query or save data in relational databases without relying on object-relational mapping. You write SQL and ask the JdbcTemplate class to execute it in the database using JDBC.
I would say JPA and JDBC starters should be mutually exclusive: either one or the other.
You use REST only if you're writing web services. These may or may not query or persist data in a relational database. I would expect to see both the REST starter and a persistence starter in a pom if a REST service needed persistent data.
Three very different starters.
You ask why Spring Boot has a REST data starter parent that combines the two. In a word: convenience.

How Spring Boot Handle Hibernate Session?

I see with the Spring Boot you don't need to open and close the hibernate session.
But for the understanding purpose how it is working internally, On which layer it is opening the hibernate session and when it is closing.
I created one POC.
I have one Spring boot application it has two Entity One is Customer and other is Address and there is one to Many relation between Customer and Address.
And I have one two Apis one is adding record and other is fetching all the records.
These APis are there in the CustomerEndpoint and it is annotated with
#RestController
#RequestMapping(value="/customer").
And also created CustomerRepository which extends CrudRepository for saving and fetching the Customer records.
So as per my understanding while fetching the Customer using CustomerRepository inside CustomerEndpointclass should throw LazyInitialization error when we will say customer.getAddress(as its fetchtype is LAZY).
But it is not throwing any error, it is working properly.
I was thinking the hibernate session not be there in the CustomerEndpoint class.
Can anyone help me how this Hibernate session is maintained by Spring boot?
As everybody is thinking it as a duplicate of another question but my question is not top of their explanation as according to them session is valid till the Repository so according to that i should get LazyInitialization exception while saying customer.getAddress inside CustomerEndpoint as it is not a Repository but i am not getting any Exception
First of all it is not a good practice to use Repository layer in your Presentation layer.
OSIV (Open Session in View) is enabled by default in Spring Boot, and OSIV is really a bad idea from a performance and scalability perspective.
Because of this you are not getting the exception and able to work in your presentation layer.
Check by putting the follwoings in your application.properties file
spring.jpa.open-in-view=false
You can refer OSIV AntiPattern for more details
I think, It still works If your customer.getAddress is inside the transaction

Spring Boot JdbcTemplate Filtering Mechanism

I've got a Spring Boot application with an API which retrieves entities from a data source. This is implemented using JdbcTemplate which for the most part is all I need.
However, I've got an endpoint which takes many query parameters which will need to be reflected in the database query. Something like
GET /api/v1/people?age=10&height=125&weight=40&eyecolor=blue&...
To save me spending the rest of my life creating the SQL query for this - does Spring provide some easy filtering mechanism? I'm aware that Spring JPA would do this but I'm hesitant to change all of my code to go down that route.
Spring does not offer a way to Process/Make a Filter on 'many query parameters" however you may wanna look at GraphQL http://graphql.org

Do I need to configure hibernate before configuring spring-data?

I am a little bit lost here, my main goal is to create a MVC pattern with Spring MVC and include Spring-security with user/passwords from the database.
So far, I have Spring security and MVC running well, but I dont know how to include the database (I must use spring data in some point).
I've read the tutorials and info of the site, and it says its a layer that works with other ORM (such as hibernate). So my question is, Should I configure hibernate before Spring data?
Is there any guide on how to do it (where they use annotations only?).
The answer to the question "do I need hibernate configured to use Spring Data JPA" is "yes" if you want Hibernate to be your JPA implentation.
So here's what the stack looks like for a typical Spring MVC + Spring Data JPA application:
#Controller class with #RequestMapping
calls
#Service class
calls
#Repository class (this annotation is optional, extend CrudRepository interface)
The repository you write uses an #Entity (JPA) class to access the database through Hibernate as described in this tutorial. Spring does a great job hiding most of the setup details of Hibernate from you (you don't need a persistence.xml if you do it right).

Categories

Resources