From 1da9808edb33e173edde595f57754e5911894737 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 30 Mar 2015 21:31:02 +0200 Subject: [PATCH] DATAREST-500 - Enable lazy-loading proxy resolution on Hibernate 4 module. We now explicitly activate the resolution of lazy-loading proxies on Hibernate 4 to make sure not already populated lazy-loading proxies are actively resolved. --- .../webmvc/json/Jackson2DatatypeHelper.java | 6 +- ...ackson2DatatypeHelperIntegrationTests.java | 83 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelperIntegrationTests.java diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelper.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelper.java index 9d54d4e43..171d39e94 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelper.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelper.java @@ -62,7 +62,11 @@ public class Jackson2DatatypeHelper { private static class Hibernate4ModuleRegistrar { public static void registerModule(ObjectMapper mapper) { - mapper.registerModule(new Hibernate4Module()); + + Hibernate4Module module = new Hibernate4Module(); + module.enable(Hibernate4Module.Feature.FORCE_LAZY_LOADING); + + mapper.registerModule(module); } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelperIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelperIntegrationTests.java new file mode 100644 index 000000000..004957673 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelperIntegrationTests.java @@ -0,0 +1,83 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc.json; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import javax.persistence.EntityManager; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.mapping.PersistentPropertyAccessor; +import org.springframework.data.mapping.context.PersistentEntities; +import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; +import org.springframework.data.rest.webmvc.jpa.Order; +import org.springframework.data.rest.webmvc.jpa.OrderRepository; +import org.springframework.data.rest.webmvc.jpa.Person; +import org.springframework.data.rest.webmvc.jpa.PersonRepository; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Integration tests for {@link Jackson2DatatypeHelper}. + * + * @author Oliver Gierke + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { RepositoryTestsConfig.class, RepositoryRestMvcConfiguration.class }) +@Transactional +public class Jackson2DatatypeHelperIntegrationTests { + + @Autowired PersistentEntities entities; + @Autowired ObjectMapper objectMapper; + + @Autowired PersonRepository people; + @Autowired OrderRepository orders; + @Autowired EntityManager em; + + Order order; + + @Before + public void setUp() { + + this.order = orders.save(new Order(people.save(new Person("Dave", "Matthews")))); + + // Reset JPA to make sure the query returns a result with proxy references + em.flush(); + em.clear(); + } + + /** + * @see DATAREST-500 + */ + @Test + public void configuresHIbernate4ModuleToLoadLazyLoadingProxies() throws Exception { + + PersistentEntity entity = entities.getPersistentEntity(Order.class); + PersistentProperty property = entity.getPersistentProperty("creator"); + PersistentPropertyAccessor accessor = entity.getPropertyAccessor(orders.findOne(this.order.getId())); + + assertThat(objectMapper.writeValueAsString(accessor.getProperty(property)), is(not("null"))); + } +}