0

I am developing using framework of struts2, spring and hibernate. I used HibernateTemplate of spring. In return data to view by using JSON of struts2 got error (Proxy is detached (i.e session is null)...) in that case when I used struts default method is no error.

Here is my applicationContext file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                    http://www.springframework.org/schema/jdbc
                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">

<!-- Hibernate Configuration -->

<!-- <mvc:resources mapping="/style/**" location="/style/"/> -->
<context:annotation-config />
<context:component-scan base-package="com.SSH" />

<tx:annotation-driven transaction-manager="transactionManager" />
<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/SSH"
    p:username="root" p:password="root" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>com/SSH/xml/Phone.hbm.xml</value>
            <value>com/SSH/xml/ExamType.hbm.xml</value>
            <value>com/SSH/xml/Mark.hbm.xml</value>
            <value>com/SSH/xml/Subject.hbm.xml</value>
            <value>com/SSH/xml/Address.hbm.xml</value>
            <value>com/SSH/xml/Classes.hbm.xml</value>
            <value>com/SSH/xml/Contact.hbm.xml</value>
            <value>com/SSH/xml/Exam.hbm.xml</value>
            <value>com/SSH/xml/Student.hbm.xml</value>
            <value>com/SSH/xml/Teacher.hbm.xml</value>
            <!-- any more table -->
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
        </props>
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

Here is my implementation class:

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import com.SSH.Model.Contact;
import com.SSH.Model.Phone;
import com.SSH.Services.ContactService;

@Service("ContactService")
@Transactional(propagation=Propagation.REQUIRED)
public class ContactTemplate implements ContactService{

@Autowired
HibernateTemplate hibernateTemplate;

@Override
public Contact getContact() {
    // TODO Auto-generated method stub
    Contact contact = hibernateTemplate.get(Contact.class, new Long("1"));
    Hibernate.initialize(contact.getAddress());
    Hibernate.initialize(contact.getPhone());
    return contact;
}

@Override
public void setContact(Contact contact) {
    // TODO Auto-generated method stub
    hibernateTemplate.save(contact);
}

}

Here is my action class:

import java.util.HashSet;
import java.util.Set;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import com.SSH.Model.Address;
import com.SSH.Model.Contact;
import com.SSH.Model.Phone;
import com.SSH.Services.ContactService;
import com.opensymphony.xwork2.ActionSupport;

public class Main extends ActionSupport{
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Autowired
private ContactService contactService;
private Contact result = new Contact();

public String welcome(){
    Set<Phone> phoneSet = new HashSet<Phone>();
    Phone phone = new Phone(80, 12345);
    phoneSet.add(phone);
    Address address = new Address();
    address.setCity("Yangon");
    address.setStreet("Kyan Sis Thar");
    address.setHomeNo("S20");
    Contact contact = new Contact(address, phoneSet);

    contactService.setContact(contact);
    result = contactService.getContact();
    return SUCCESS;
}

public Contact getResult() {
    return result;
}

public void setResult(Contact result) {
    this.result = result;
}



}

Here is my struts.xml file:

<package name="default" extends="struts-default,json-default" namespace="/">
    <action name="welcome" class="com.SSH.Action.Main" method="welcome">
  <result name="success">/view/welcome.jsp</result> // this struts result type is OK 
    </action>
  </package>

This is JSON result type:

   <package name="default" extends="struts-default,json-default" namespace="/">
     <action name="welcome" class="com.SSH.Action.Main" method="welcome">
  <result type="json" />// this type got error
    </action>
</package>

My error screen shot:

enter image description here

Pls, thanks.

Aung Thet
  • 3,051
  • 3
  • 13
  • 18

1 Answers1

0

You must be having a lazy child which is not loaded. The JSON parser is trying to load a lazy property/child/entity outside of the transaction scope. Please try to load all the lazy children within the session/transaction and then send it to te UI. You can load the lazy child by just invoking it like the following

obj.getLazyChild();

Also, Hibernate.Initilize() will also help.

Zeus
  • 6,386
  • 6
  • 54
  • 89