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.
This commit is contained in:
Oliver Gierke
2015-03-30 21:31:02 +02:00
parent 977ccb1a36
commit 1da9808edb
2 changed files with 88 additions and 1 deletions

View File

@@ -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);
}
}

View File

@@ -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")));
}
}