2010-05-16 239 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建議?