延迟加载问题:Hibernate 3,Spring 4,Jackson 2.4.0

时间:2014-09-08 18:09:57

标签: java json spring hibernate spring-mvc

我的项目正在使用

  • Spring 4.0.5.RELEASE
  • Hibernate 3.6.0.FINAL
  • 杰克逊 - 数据类型-hibernate3-2.4.0.jar

问题: / Spring4HibernateMaven / contacts / 3会产生LazyInitializationException !!

将ObjectMapper(请参阅spring-servlet.xml)更改为com.company.gis.ucvs.ioe.json.HibernateAwareObjectMapper,将Contact POJO转换为JSON失败,但有异常。但是,如果我只是创建一个虚拟Contact对象,POJO到JSON转换工作正常。我错过了什么?真的很感谢你的帮助/指点。谢谢!

STACK TRACE

org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话     org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)     org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)     org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)     com.company.gis.ucvs.ioe.form.Contact _ $$ _ javassist_6.getId(联系_ $$ _ javassist_6.java)     sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     java.lang.reflect.Method.invoke(Method.java:597)     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:726)     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:506)     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)     com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:114)     com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1837)     org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:253)

来自spring-servlet.xml的SNIPPETS

<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
**<!--  bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" /-->
<bean id="jacksonObjectMapper" class="com.company.gis.ucvs.ioe.json.HibernateAwareObjectMapper" />**

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonObjectMapper" />
<property name="targetMethod" value="configure" />
<property name="arguments">
    <list>
        <value type="com.fasterxml.jackson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value>
        <value>false</value>
    </list>
</property>
</bean>    
来自ContactController.java的

SNIPPETS

@RequestMapping(value = "/contacts/{id}", method = RequestMethod.GET,headers="Accept=application/json")
public @ResponseBody Contact getContact(@PathVariable Integer id)  {

    //Contact c = new Contact(); <--- Sample contact POJO to JSON works fine
    //c.setEmail("NO EMAIL");
    Contact c = contactService.getContact(id);
    return c;

}
来自ContactServiceImpl.java的

SNIPPETS

@Transactional
public Contact getContact(Integer id) {
    Contact c = contactDAO.getContact(id);
    return c;

}
来自ContactDaoImpl.java的

SNIPPET

@Autowired
private SessionFactory sessionFactory;

public Contact getContact(Integer id) {
    Contact contact = (Contact) sessionFactory.getCurrentSession().load(
            Contact.class, id);
    return contact;
}
来自HibernateAwareObjectMapper的

SNIPPETS

@SuppressWarnings("serial")
public class HibernateAwareObjectMapper extends ObjectMapper {

    public HibernateAwareObjectMapper() {
        Hibernate3Module hm = new Hibernate3Module();
        registerModule(hm);  
        //Whether lazy-loaded object should be forced to be loaded and then serialized (true); or serialized as nulls (false).Default value is false. 
        hm.configure(Hibernate3Module.Feature.FORCE_LAZY_LOADING, true);
    }

} 

1 个答案:

答案 0 :(得分:0)

您正在处理分离的实体。只要您的对象离开@Transactional方法,它们就会被分离。将它们转换为DTO(数据传输对象)或取消对象,请参阅http://java-success.blogspot.de/2012/09/understanding-hibernate-proxy-objects.html