Using EclipseLink with Spring Boot

The Promise

Spring Boot advertises in its self-introduction that one of its goals is to “Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.”

One thing that we might want to change from the defaults is the choice of JPA implementation. Hibernate is Spring Boot’s default JPA implementation, but EclipseLink (which is in fact the JPA Reference Implementation) is a good choice as well, and many people will have compelling reasons to choose it over Hibernate.

Does Spring Boot keep its promise to get out of our way if we want to deviate from the defaults? Let’s find out!

Put It To The Test

At one point there was an issue filed and resolved in the spring data examples project: people wanted an example demonstrating how to use EclipseLink in Spring Boot. So the eclipselink sample that came out of that is probably the best place to start.

It is a good starting point, but if we use Gradle we still have some work to do because the example uses Maven. We can actually start with the JPA project from spring.io’s Accessing Data With JPA guide as a starting point, and incorporate the basic changes from the eclipselink sample.
The first thing we’ll need to do is  add some configuration to provide the EclipseLink vendor adaptor. Note that this configuration is for the JpaBaseConfiguration from Spring Boot 1.4 (the class for Spring Boot 1.3 is a little different). In this class, the “eclipselink.weaving” property is set to false to eliminate any weaving configuration. This is maybe not what we’d want to do in production, but it makes the demo a bit simpler.

@Configuration
public class EclipseLinkConfiguration extends JpaBaseConfiguration {

   protected EclipseLinkConfiguration(DataSource dataSource, JpaProperties properties,
                     ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
      super(dataSource, properties, jtaTransactionManagerProvider);
   }

   @Override
   protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
      return new EclipseLinkJpaVendorAdapter();
   }

   @Override
   protected Map<String, Object> getVendorProperties() {

      // Turn off dynamic weaving to disable LTW (Load Time Weaving) lookup in static weaving mode
      return Collections.singletonMap("eclipselink.weaving", "false");
   }
}

Changes to the build script are essentially excluding the Hibernate implementation of JPA and including the EclipseLink implementation.


dependencies {

   compile("org.springframework.boot:spring-boot-starter-data-jpa:1.4.0.M3") {
      exclude group: "org.hibernate", module: "hibernate-entitymanager"
   }
   compile("org.eclipse.persistence:org.eclipse.persistence.jpa:2.6.3")

   // .. other dependencies here
}

Finally, one issue with the original sample’s JPA Entity is that EclipseLink doesn’t like using a primitive long as a primary key, so we can change it to Long.

You can get all the source for this project the github.

Results

On running, we see initialization output we’d expect, starting with

Building JPA container EntityManagerFactory for persistence unit 'default'
Initialized JPA EntityManagerFactory for persistence unit 'default'

and ending with

Closing JPA EntityManagerFactory for persistence unit 'default'

Running “gradle dependencies” and searching for hibernate in the output shows zero results, while doing the same for eclipselink clearly shows eclipselink being used

gradle dependencies | grep eclipse.persistence 

+--- org.eclipse.persistence:org.eclipse.persistence.jpa:2.6.3
| +--- org.eclipse.persistence:javax.persistence:2.1.1
| +--- org.eclipse.persistence:org.eclipse.persistence.asm:2.6.3
| +--- org.eclipse.persistence:org.eclipse.persistence.antlr:2.6.3
| +--- org.eclipse.persistence:org.eclipse.persistence.jpa.jpql:2.6.3
| \--- org.eclipse.persistence:org.eclipse.persistence.core:2.6.3
| +--- org.eclipse.persistence:org.eclipse.persistence.asm:2.6.3

Conclusion

Spring Boot got out of our way pretty quickly and easily once we wanted to move away from the default technology. We can consider the promise kept!

This is not the end of course. There is more to the configuration if we want our application to be ready for production, but this is a first step to using EclipseLink in a Spring Boot application. We can build on what else you would want to do in a future post!

Advertisements

Leave a comment

Filed under Software Engineering

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s