2010-05-16 240 views
0

我在项目中使用GWT 2.0作为UI层。在服务器端,我使用Hibernate。例如,这是2级域的实体,我有:Hibernate与GWT 2.0集成中Gilead的惰性初始化异常问题

public class User { 
     private Collection<Role> roles; 
     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "users", targetEntity = Role.class) 
    public Collection<Role> getRoles() { 
     return roles; 
    } 
     ... 
} 

public class Role { 
     private Collection<User> users; 
     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = User.class) 
    public Collection<User> getUsers() { 
     return users; 
    }  
     ... 
} 

在我DAO层,我用UserDAO的从弹簧伸长的HibernateDaoSupport。 UserDAO有getAll方法返回所有用户。

而在我的DAO服务上,我使用UserService,它使用userDAO来获取所有用户。

因此,当我从UsersService获取所有用户时,将返回的Users实体从Hibernate会话中分离出来。出于这个原因,我不想在我的服务中获得的Users实例上使用getRoles()方法。

我想什么是只是为了感谢用户的列表转移到RPC服务可以使用用户的其他信息在客户端与GWT。

因此,我的主要问题是要能够PersistentBag转换成Users.roles在简单的列表,以便能够通过RPC的用户转移。为此,我看到Gilead框架可能是一个解决方案。

为了使用Gilead,我更改了我的域实体。现在,他们扩展了net.sf.gilead.pojo.gwt.LightEntity并且他们尊重JavaBean规范。

在服务器上,我通过RPC感谢GwtRpcSpring框架(http://code.google.com/p/gwtrpc-spring/)暴露我的服务。这个框架有一个建议,可以让Gilead更容易集成。

我的applicationContext包含吉利德以下配置:

<bean id="gileadAdapterAdvisor" class="org.gwtrpcspring.gilead.GileadAdapterAdvice" /> 
<aop:config> 
    <aop:aspect id="gileadAdapterAspect" ref="gileadAdapterAdvisor"> 
     <aop:pointcut id="gileadPointcut" 
      expression="execution(public * com.google.gwt.user.client.rpc.RemoteService.*(..))" /> 
     <aop:around method="doBasicProfiling" pointcut-ref="gileadPointcut" /> 
    </aop:aspect> 
</aop:config> 

<bean id="proxySerializer" class="net.sf.gilead.core.serialization.GwtProxySerialization" /> 

<bean id="proxyStore" class="net.sf.gilead.core.store.stateless.StatelessProxyStore"> 
    <property name="proxySerializer" ref="proxySerializer" /> 
</bean> 

<bean id="persistenceUtil" class="net.sf.gilead.core.hibernate.HibernateUtil"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<bean class="net.sf.gilead.core.PersistentBeanManager"> 
    <property name="proxyStore" ref="proxyStore" /> 
    <property name="persistenceUtil" ref="persistenceUtil" /> 
</bean> 

的方法doBasicProfiling的代码如下:

@Around("within(com.google.gwt.user.client.rpc.RemoteService..*)") 
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { 
    if (log.isDebugEnabled()) { 
     String className = pjp.getSignature().getDeclaringTypeName(); 
     String methodName = className 
       .substring(className.lastIndexOf(".") + 1) 
       + "." + pjp.getSignature().getName(); 
     log.debug("Wrapping call to " + methodName 
       + " for PersistentBeanManager"); 
    } 
    GileadHelper.parseInputParameters(pjp.getArgs(), beanManager, 
      RemoteServiceUtil.getThreadLocalSession()); 
    Object retVal = pjp.proceed(); 
    retVal = GileadHelper.parseReturnValue(retVal, beanManager); 

    return retVal; 
} 

通过这种结构,当我跑我的申请,使用我的RPC服务可以获得所有的用户,我从Users.roles的Hibernate中获得一个懒惰的初始化异常。

我很失望,因为我认为,吉利德会让我连载即使这些实体包含PersistentBag我的域实体。

这不是Gilead的目标之一吗?

那么,有人会知道如何配置吉利德(与GwtRpcSpring或其他解决方案)将能够在不懒例外转移域实体?

非常感谢您的帮助。

Sylvain

回答

1

那么答案很简单Object retVal = pjp.proceed(); 返回平原Hibernate对象,而不是Gilead处理的对象。 也许有人知道如何在服务中添加适当的Gilead建议?