Struts 2 + Hibernate = LazyInitializationException

0 respostas
P

Fala feras :smiley:

Já criei um filtro mas isso não ta resolvendo. Alguém já passou por isso?

HibernateUtil

public class HibernateUtil {
	private static final SessionFactory sessionFactory;

	private static final Configuration cfg;
	
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            cfg = new AnnotationConfiguration();
            cfg.configure();
            cfg.addSqlFunction("LEFT", new StandardSQLFunction("LEFT", Hibernate.STRING));
        	sessionFactory = cfg.buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }


}

Filter.java

public class HibernateSessionRequestFilter implements Filter {

	private static Log log = LogFactory.getLog(HibernateSessionRequestFilter.class);

	private SessionFactory sf;

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		try {
			log.debug("Starting a database transaction");
			sf.getCurrentSession().beginTransaction();

			// Call the next filter (continue request processing)
			chain.doFilter(request, response);

			// Commit and cleanup
			log.debug("Committing the database transaction");
			//sf.getCurrentSession().getTransaction().commit();

		} catch (StaleObjectStateException staleEx) {
			log
					.error("This interceptor does not implement optimistic concurrency control!");
			log
					.error("Your application will not work until you add compensation actions!");
			// Rollback, close everything, possibly compensate for any permanent
			// changes
			// during the conversation, and finally restart business
			// conversation. Maybe
			// give the user of the application a chance to merge some of his
			// work with
			// fresh data... what you do here depends on your applications
			// design.
			throw staleEx;
		} catch (Throwable ex) {
			// Rollback only
			ex.printStackTrace();
			try {
				if (sf.getCurrentSession().getTransaction().isActive()) {
					log
							.debug("Trying to rollback database transaction after exception");
					sf.getCurrentSession().getTransaction().rollback();
				}
			} catch (Throwable rbEx) {
				log.error("Could not rollback transaction after exception!",
						rbEx);
			}

			// Let others handle it... maybe another interceptor for exceptions?
			throw new ServletException(ex);
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		log.debug("Initializing filter...");
		log
				.debug("Obtaining SessionFactory from static HibernateUtil singleton");
		sf = HibernateUtil.getSessionFactory();
	}

	public void destroy() {
	}

}

web.xml

<filter>
        <filter-name>HibernateSessionRequestFilter</filter-name>
        <filter-class>com.xx.aa.hibernate.filter.HibernateSessionRequestFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>HibernateSessionRequestFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

Não sei mais o que fazer e este erro ocorre quando tento abrir uma popup que acessa os valores de relacionamento. Tem alguma outra solução pra isso?

Valeu

Criado 25 de agosto de 2009
Respostas 0
Participantes 1