Oi Paulo,
Fiz um código paralelo que reproduz o mesmo erro aqui, segue os detalhes…
Log do erro (ponto de falha no JSP)
[quote]2010-07-02 23:38:04,890 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/moduloWeb].[jsp]] (http-0.0.0.0-8080-1) Servlet.service() for servlet jsp threw exception
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.sandbox.domain.models.FacetObjectsVO.values, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
at org.apache.struts2.util.MakeIterator.convert(MakeIterator.java:82)
at org.apache.struts2.components.IteratorComponent.start(IteratorComponent.java:270)
at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:53)
at org.apache.jsp.WEB_002dINF.pages.index_jsp._jspx_meth_s_005fiterator_005f0(index_jsp.java:185)
at org.apache.jsp.WEB_002dINF.pages.index_jsp._jspService(index_jsp.java:96)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:638)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:444)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:382)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:310)
at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:154)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.sandbox.struts.filters.SessionIdFilter.doFilter(SessionIdFilter.java:87)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
[/quote]
Log de erro (ponto de falha na Action)
[quote]
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.sandbox.domain.models.FacetObjectsVO.values, no session or session was closed
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:119)
org.hibernate.collection.PersistentBag.size(PersistentBag.java:248)
com.sandbox.struts.controller.ActHome.execute(ActHome.java:21)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
com.sandbox.struts.filters.SessionIdFilter.doFilter(SessionIdFilter.java:87)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)[/quote]
Modulo Web, Action Home.do
[code]package com.sandbox.struts.controller;
import javax.naming.InitialContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sandbox.business.beans.FacetObjectsEJBIf;
import com.sandbox.domain.models.FacetObjectsVO;
import com.sandbox.utils.Constants;
public class ActHome extends ActionSupport
{
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception
{
InitialContext ic = new InitialContext();
FacetObjectsEJBIf ejb = (FacetObjectsEJBIf) ic.lookup("ejb/FacetObjects");
facet = ejb.fetchFacet("Gênero Musical");
facet.getValues().size();
return Constants.RESULT_HOME;
}
private FacetObjectsVO facet;
public FacetObjectsVO getFacet()
{
return facet;
}
public void setFacet(FacetObjectsVO pFacet)
{
facet = pFacet;
}
}[/code]
Hibernate Filter, ThreadLocal
[code]package com.sandbox.struts.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sandbox.utils.HibernateSession;
public class HibernateFilter implements Filter
{
private static final Logger logger = LoggerFactory.getLogger(HibernateFilter.class);
public HibernateFilter()
{
}
public void init(FilterConfig fConfig) throws ServletException
{
logger.info("Iniciando filtro.");
}
public void destroy()
{
logger.info("Finalizando filtro.");
}
public void doFilter(ServletRequest pRequest, ServletResponse pResponse, FilterChain pChain)
throws IOException, ServletException
{
logger.debug("Filtro executando...");
Session lSession = HibernateSession.getCurrentSession();
pChain.doFilter(pRequest, pResponse);
if (lSession.isOpen())
{
logger.debug("Closing session is " + lSession.hashCode());
}
else
{
logger.debug("AVISO: Sessao ja foi fechada!");
}
HibernateSession.closeSession();
logger.debug("Filtro executado com sucesso.");
}
}
[/code]
struts.xml
<struts>
<package namespace="/" name="Acts" extends="struts-default">
<action name="Home" class="com.sandbox.struts.controller.ActHome">
<result name="home">WEB-INF/pages/index.jsp</result>
</action>
</package>
</struts>
web.xml
[code] ModuloWeb
<session-config>
<session-timeout>600</session-timeout>
</session-config>
<filter>
<display-name>SessionIdFilter</display-name>
<filter-name>SessionIdFilter</filter-name>
<filter-class>com.sandbox.struts.filters.SessionIdFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionIdFilter</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>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>[/code]
index.jsp
[code]
<s:if test=“pageTitle != null”><s:property value=“pageTitle” /></s:if>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/styles/template.css" />
This is the header page.
O início de tudo.
Valores:
This is the footer page.
[/code]
Modulo EJB, Hibernate Session
[code]package com.sandbox.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class HibernateSession
{
public static final ThreadLocal session = new ThreadLocal();
public static Session getCurrentSession()
{
Session s = (Session) session.get();
if (s == null)
{
SessionFactory sf = HibernateUtil.getSessionFactory();
s = sf.openSession();
session.set(s);
}
return s;
}
public static void closeSession()
{
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
[/code]
EJB, BaseDAO
[code]package com.sandbox.domain.base;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Session;
import com.sandbox.utils.HibernateSession;
public abstract class BaseDAO<TM extends BaseVO, TF extends BaseFilter> implements BaseDAOIf<TM, TF>
{
/* Construtor */
@SuppressWarnings("unchecked")
public BaseDAO()
{
persistentClass = (Class<TM>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
session = HibernateSession.getCurrentSession();
}
/* Acesso protegido */
protected Session session;
/* Atributos */
private Class<TM> persistentClass;
/* Métodos de acesso */
public Class<TM> getPersistentClass()
{
return persistentClass;
}
/* Operações */
/* CRUD - Create, Retrieve, Update, Delete */
public void inserir(TM pModel)
{
session.save(pModel);
session.flush();
}
public void alterar(TM pModel)
{
session.update(pModel);
session.flush();
}
public void remover(TM pModel)
{
session.delete(pModel);
session.flush();
}
public BaseVO obterPorPK(TM pModel)
{
Object obj = session.load(getPersistentClass(), pModel.getId());
return (BaseVO) obj;
}
/* Misc */
@SuppressWarnings("unchecked")
public List<TM> obterTodos()
{
List<TM> lRet = session.createCriteria(getPersistentClass()).list();
return lRet;
}
}[/code]
EJB, FacetObjectsVO
[code]package com.sandbox.domain.models;
import java.util.Collection;
import com.sandbox.domain.base.BaseVO;
public class FacetObjectsVO extends BaseVO
{
private static final long serialVersionUID = 1L;
private String title;
private Collection<FacetValuesVO> values;
public String getTitle()
{
return title;
}
public void setTitle(String pTitle)
{
title = pTitle;
}
public Collection<FacetValuesVO> getValues()
{
return values;
}
public void setValues(Collection<FacetValuesVO> pValues)
{
values = pValues;
}
}
[/code]
EJB, FacetObjectsDAO
[code]package com.sandbox.domain;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import com.sandbox.domain.base.BaseDAO;
import com.sandbox.domain.filters.FacetObjectsFilter;
import com.sandbox.domain.models.FacetObjectsVO;
public class FacetObjectsDAO extends BaseDAO<FacetObjectsVO, FacetObjectsFilter>
{
public List<FacetObjectsVO> obterPorFiltro(FacetObjectsFilter pFiltro)
{
// Mapa de parametros e query inicial
HashMap<String, Object> lParams = new HashMap<String, Object>();
StringBuffer lQueryBuf = new StringBuffer("from FacetObjectsVO a where 1=1");
// Adicionar parametros na clausula WHERE
if (pFiltro.getTitle() != null)
{
lQueryBuf.append(" and a.title = :nome");
lParams.put("nome", pFiltro.getTitle());
}
// Compilar query
String lHqlQuery = lQueryBuf.toString();
Query lQuery = session.createQuery(lHqlQuery);
// Preencher parametros
Iterator<String> lIter = lParams.keySet().iterator();
while (lIter.hasNext())
{
String lName = lIter.next();
Object lValue = lParams.get(lName);
lQuery.setParameter(lName, lValue);
}
// Retornar valores
List<FacetObjectsVO> lRet = lQuery.list();
return lRet;
}
}
[/code]
FacetObjectsEJB
[code]package com.sandbox.business.beans;
import javax.ejb.Stateless;
import com.sandbox.domain.models.FacetObjectsVO;
import com.sandbox.utils.BOFactory;
@Stateless(mappedName = “ejb/FacetObjects”)
public class FacetObjectsEJB implements FacetObjectsEJBIf
{
public FacetObjectsVO fetchFacet(String pFacetTitle)
{
return BOFactory.getFacetObjectsBO().fetchFacet(pFacetTitle);
}
}
[/code]
FacetObjectsBO
[code]package com.sandbox.business;
import java.util.List;
import com.sandbox.domain.FacetObjectsDAO;
import com.sandbox.domain.filters.FacetObjectsFilter;
import com.sandbox.domain.models.FacetObjectsVO;
import com.sandbox.utils.DAOFactory;
public class FacetObjectsBO
{
public FacetObjectsVO fetchFacet(String pFacetTitle)
{
FacetObjectsFilter lFilter = new FacetObjectsFilter();
lFilter.setTitle(pFacetTitle);
FacetObjectsDAO lDao = DAOFactory.getUsuariosDAO();
List<FacetObjectsVO> lRet = lDao.obterPorFiltro(lFilter);
if (lRet.size() == 1)
{
return lRet.get(0);
}
return null;
}
}
[/code]
hibernate.cfg.xml
[code]
org.gjt.mm.mysql.Driver
jdbc:mysql://localhost:3306/test
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.connection.release_mode">on_close</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
<property name="hibernate.show_sql">false</property>
<mapping resource="mapeamento.hbm.xml"/>
</session-factory>
[/code]
mapeamento.hbm.xml
[code]
<class name="FacetValuesVO" table="facetvalues">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="parent" />
<property name="title" />
</class>
[/code]