- In our last tutorial, we saw Spring MVC Framework.In this Spring JDBC Tutorial, we are going to discuss Spring JDBCTemplate with Eclipse IDE. Using plain old JDBC for working with a database, it becomes complicated to use an unnecessary code for opening/closing database or handle exceptions etc.
- The all-new Spring Tools can be used in various coding environments, ranging from Eclipse as a full-featured integrated development environment to Visual Studio Code and Theia as lightweight code editors. Continue to use your preferred environment and add great Spring tooling to it.
- The Eclipse Marketplace dialog appears, type Spring Tool Suite or STS into the Find textfield and hit Enter. Eclipse will send query to its server and display results as shown below: Select the version that matches your Eclipse’s version and click Install button. Here we select Spring Tool Suite (STS) for Eclipse Juno (3.8 + 4.2).
By default, Spring Data uses Hibernate as the default JPA implementation provider.
However, Hibernate is certainly not the only JPA implementation available to us.
In this article, we'll go through steps necessary to set up EclipseLink as the implementation provider for Spring Data JPA.
The Spring IDE feature button is the furthest right feature button and the button to its left represents the OEPE plug-ins. Oracle Enterprise Pack for Eclipse with the Spring Framework. The Spring Framework has become a widely used framework for generation of enterprise Java applications because of the many advantages that it offers.
2. Maven Dependency
To use it in our Spring application, we just need to add the org.eclipse.persistence.jpa dependency in the pom.xml of our project:
By default, Spring Data comes with the Hibernate implementation.War thunder coupons.
Since we want to use EclipseLink instead as the JPA provider, we don't need it anymore.
Therefore we can remove it from our project by excluding its dependencies:
The next step is to tell the Spring Framework that we want to use EclipseLink as the JPA implementation.
3. Spring Configuration
JpaBaseConfiguration is an abstract class which defines beans for JPA in Spring Boot. To customize it, we have to implement some methods like createJpaVendorAdapter() or getVendorProperties().
Spring provides a configuration implementation for Hibernate out of the box called HibernateJpaAutoConfiguration. However, for EclipseLink, we have to create a custom configuration.
First, we need to implement the createJpaVendorAdapter() method which specifies the JPA implementation to use.
Spring provides an implementation of the AbstractJpaVendorAdapter for EclipseLink called EclipseLinkJpaVendorAdapter that we're going to use in our method:
Also, we have to define some vendor-specific properties which will be used by EclipseLink.
We can add these via the getVendorProperties() method:
The class org.eclipse.persistence.config.PersistenceUnitProperties contains properties which we can define for EclipseLink.
In this example, we've specified that we want to use weaving and re-create the database schema when the application runs.
And that's it! This is the whole implementation necessary to change from the default Hibernate JPA provider to EclipseLink.
Note that Spring Data uses the JPA API and not any vendor specific methods. So, in theory, there should be no problem when switching from one vendor to another.
In this quick tutorial, we covered how to change the default JPA implementation provider used by Spring Data.
We saw how quick and simple it is to change from Hibernate which is the default to EclipseLink.
As always, the full implementation of the examples is available over on Github.
- Written by Nam Ha Minh
- Last Updated on 20 February 2020 Print Email
- If you are using Eclipse Juno (4.2) or older, follow the tutorial Installing Maven plug-in for existing Eclipse IDE.
- If you have Eclipse Kepler (4.3) or newer, Maven is already integrated in the IDE so this step can be skipped.
- Archetype Group Id: co.ntier
- Archetype Artifact Id: spring-mvc-archetype
- Archetype Version: 1.0.2
- Repository URL: leave empty
- Group Id: MySpringMvc
- Artifact Id: MySpringMvc
- Version: leave this as default, 0.0.1-SNAPSHOT.
- Package: net.codejava.spring
- MvcConfiguration.java: this configuration class uses annotations in replacement of XML since annotations are preferred. It configures a default view resolver and a resource handler. We don’t see Spring application context XML file any more.
- HomeController.java: this controller is generated to handler the default URL mapping (/). Look at its code, it redirects the user to the “home” view.
- home.jsp: This JSP page will be displayed when the user access this application using the default URL (/). This is simply a hello world page.
- web.xml: bootstraps the Spring context loader listener and Spring dispatcher servlet.
- pom.xml: All dependencies for a Spring MVC application are listed here.
http://localhost:8080/MySpringMvcHit Enter, we should see the “Hello World!” page as seen below: