Skip to content

Commit

Permalink
DATAREST-607 - Register Jackson Hibernate 5 module automatically if p…
Browse files Browse the repository at this point in the history
…resent.

Just as we do for the Jackson Hibernate integration for Hibernate 4, we now register the module for Hibernate 5 as well.
  • Loading branch information
odrotbohm committed Sep 9, 2016
1 parent 360ed8a commit 31c894e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
11 changes: 9 additions & 2 deletions spring-data-rest-webmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>${jackson}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
Expand All @@ -79,7 +86,7 @@
<version>${jackson}</version>
<optional>true</optional>
</dependency>

<!-- Querydsl -->

<dependency>
Expand Down Expand Up @@ -112,7 +119,7 @@
<version>${jsonpath}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.joda.JodaModule;

/**
Expand All @@ -35,15 +36,21 @@ public class Jackson2DatatypeHelper {
Jackson2DatatypeHelper.class.getClassLoader());
private static final boolean IS_HIBERNATE4_MODULE_AVAILABLE = ClassUtils.isPresent(
"com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module", Jackson2DatatypeHelper.class.getClassLoader());
private static final boolean IS_HIBERNATE5_MODULE_AVAILABLE = ClassUtils.isPresent(
"com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module", Jackson2DatatypeHelper.class.getClassLoader());

private static final boolean IS_JODA_MODULE_AVAILABLE = ClassUtils.isPresent(
"com.fasterxml.jackson.datatype.joda.JodaModule", Jackson2DatatypeHelper.class.getClassLoader());
private static final boolean IS_JODA_MODULE_AVAILABLE = ClassUtils
.isPresent("com.fasterxml.jackson.datatype.joda.JodaModule", Jackson2DatatypeHelper.class.getClassLoader());

public static void configureObjectMapper(ObjectMapper mapper) {

// Hibernate types
if (IS_HIBERNATE_AVAILABLE && Hibernate4Checker.isHibernate4() && IS_HIBERNATE4_MODULE_AVAILABLE) {
Hibernate4ModuleRegistrar.registerModule(mapper);
if (IS_HIBERNATE_AVAILABLE && HibernateVersions.isHibernate4() && IS_HIBERNATE4_MODULE_AVAILABLE) {
new Hibernate4ModuleRegistrar().registerModule(mapper);
}

if (IS_HIBERNATE_AVAILABLE && HibernateVersions.isHibernate5() && IS_HIBERNATE5_MODULE_AVAILABLE) {
new Hibernate5ModuleRegistrar().registerModule(mapper);
}

// JODA time
Expand All @@ -52,16 +59,20 @@ public static void configureObjectMapper(ObjectMapper mapper) {
}
}

private static class Hibernate4Checker {
private static class HibernateVersions {

public static boolean isHibernate4() {
return Version.getVersionString().startsWith("4");
}

public static boolean isHibernate5() {
return Version.getVersionString().startsWith("5");
}
}

private static class Hibernate4ModuleRegistrar {

public static void registerModule(ObjectMapper mapper) {
public void registerModule(ObjectMapper mapper) {

Hibernate4Module module = new Hibernate4Module();
module.enable(Hibernate4Module.Feature.FORCE_LAZY_LOADING);
Expand All @@ -70,6 +81,17 @@ public static void registerModule(ObjectMapper mapper) {
}
}

private static class Hibernate5ModuleRegistrar {

public void registerModule(ObjectMapper mapper) {

Hibernate5Module module = new Hibernate5Module();
module.enable(Hibernate5Module.Feature.FORCE_LAZY_LOADING);

mapper.registerModule(module);
}
}

private static class JodaModuleRegistrar {

public static void registerModule(ObjectMapper mapper) {
Expand Down

0 comments on commit 31c894e

Please sign in to comment.