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

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).

Related

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.

Spring data JPA WITHOUT an entire Spring Boot application

I am creating an internal CLI that is communicating with a PostgreSQL database and the easiness to create a no-code repository is one of the features that convince me to choose Spring data JPA.
However, I am not able to find some tutorial or GitHub repository to set up a Spring data JPA project without an entire spring boot application.
On the project https://github.com/spring-projects/spring-data-book/tree/master/jpa there is no main entry point, so the code is not runnable and by the way, it was updated 8 years ago ...
This other StackOverflow thread Spring Data JPA without Spring Boot does not help me because the guy could run his spring application on Google Cloud Platform finally (that was the cause of why he ask how to setup sping data jpa without spring boot).
I don't know how to start, if you have any ideas I will be happy to discuss with someone who is more experienced than me.
Thank you.
This might help if no more complete tutorial turns up https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/repositories.html
Look for this section
Standalone usage
You can also use the repository infrastructure outside of a Spring container, e.g. in CDI environments. You still need some Spring libraries in your classpath, but generally you can set up repositories programmatically as well. The Spring Data modules that provide repository support ship a persistence technology-specific RepositoryFactory that you can use as follows
In particular it says you can use a factory to generate repositories:
RepositoryFactorySupport factory = … // Instantiate factory here
UserRepository repository = factory.getRepository(UserRepository.class);
So adding the spring parts that contain the spring data classes may be enough for this level and if you want to have DI, too, you likely need to combine them with the respective spring dependencies and configure a regular spring application.

how to switch spring 4 + Hibernate with mongoDB project to MySQL?

I searching too much for my question in www.stackoverflow.com, but nothing helps me.so I have to post my question.
I have completed my project in spring 4 + Hibernate with MongoDB. now client requirement is MySQL. so I have to switch that project in Hibernate with MySQL.
I have tried to write code in the project. but it shows to much error in session as NullPointerException and another about my service and repository related.
so can you suggest any demo for a Hibernate with MySQL configuration in spring MVC 4.? and yes which configuration is good? XML based or java class based?
Please follow the link, it would help you to get start with Spring MVC + Hibernate (MySQL).
https://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-hibernate-tutorial/
About configuration: I personally prefer Java based configuration (not XML)
For xml configuration please have a look at:
http://www.codejava.net/frameworks/spring/spring-4-and-hibernate-4-integration-tutorial-part-1-xml-configuration
You should use Spring Boot instead of Spring MVC 4. Spring Boot will help you a lot as not too many configuration. You can easily switch to this without too much changes. No persisting of entity need to write as you can use Spring Data JPA for that.
You can following Spring Boot + Hibernate example by StartWithJava.
http://www.startwithjava.com/spring-boot-web-application-using-themeleaf/

Hibernate and CRUDRepository Spring Boot

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.

CommandLine Application with Hibernate + Annotations Only

Is it possible to use Hibernate with Annotations, without Spring framework for a command line application.
Right now i have spring with hibernate configured for command line application.I have main function which received data and it calls other (namely 'storeDB') object to get AbstractApplicationContext context and save the data.
Like to bring up the hibernate first ( bring up hibernate framework,
connecting to DB) and like to wait for a socket to process the data.
Can it be configured through java configuration than XML
configuration
and is it possible do in Hibernate(with annotation) without Spring?
Yes of course you can:
Use Hibernate annotations (without XML configuration) to setup your database connections and map
your entities to your database, for further information about it take a look at:
Hibernate with Annotation.
TutorialsPoint Hibernate - Annotations tutorial.
Use only Hibernate without Spring in a Java Application to manage your persistence strategies, because they are two independents frameworks:
You can see from the Hibernate Definition that:
Hibernate ORM enables developers to more easily write applications whose data outlives the application process. As an Object/Relational Mapping (ORM) framework, Hibernate is concerned with data persistence as it applies to relational databases (via JDBC).
And from the Spring project definition that:
The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.
Conclusion:
As you can see Hibernate and Spring are two differents frameworks that handles differents levels of Java Applications.

Categories

Resources