此文来自我的原博客:
如果Flex 与 Spring + Hibernate 通讯,那么Hibernate 的 Lazy 是会让
Blazeds报错的...因为cglib 代理的关系
即使你使用了 抓取 Fetch 来 取出 了延迟加载的内容,
传过去,仍然会报错,因为传过去的对象的类型,还是 cglib 代理的,而不是原来的类型
解决方案有2个:
dpHibernate ----- 这个家伙我有点想打它,搞了我半天,居然不支持JPA !
gilead ---- 虽然有评论说不看好,但是个人觉得比上面的好用
我只说 gilead 的方法:
jar:
gilead-1.3.1.1665 的包下所有的jar (上 google code 有下载 ,也有例子,如果你看英文没问题的话)
还有gilead-actionscript-1.3.1.1665.swc 放在 Flex 下
然后是2个必备的类,不用改的,是官方的,复制黏贴就行:
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class SpringApplicationContext {
private static SpringApplicationContext instance = null;
private SpringApplicationContext() {
super();
}
public static SpringApplicationContext getInstance() {
if (instance == null) {
return new SpringApplicationContext();
} else {
return instance;
}
}
public Object getSessionFactory() {
ApplicationContext appContext = WebApplicationContextUtils
.getWebApplicationContext(flex.messaging.FlexContext
.getServletConfig().getServletContext());
return appContext.getBean("sessionFactory");
}
}
这个类是用于适配的,取得sessionFactory
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;
public class SpringFactory implements FlexFactory {
private static final String SOURCE = "source";
public void initialize(String id, ConfigMap configMap) {
}
public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
SpringFactoryInstance instance = new SpringFactoryInstance(this, id,
properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance
.getId()));
return instance;
}
public Object lookup(FactoryInstance inst) {
SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
return factoryInstance.lookup();
}
static class SpringFactoryInstance extends FactoryInstance {
SpringFactoryInstance(SpringFactory factory, String id,
ConfigMap properties) {
super(factory, id, properties);
}
public String toString() {
return "SpringFactory instance for id=" + getId() + " source="
+ getSource() + " scope=" + getScope();
}
public Object lookup() {
ApplicationContext appContext = WebApplicationContextUtils
.getWebApplicationContext(flex.messaging.FlexContext
.getServletConfig().getServletContext());
String beanName = getSource();
try {
return appContext.getBean(beanName);
} catch (NoSuchBeanDefinitionException nexc) {
ServiceException e = new ServiceException();
String msg = "Spring service named '" + beanName
+ "' does not exist.";
e.setMessage(msg);
e.setRootCause(nexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
} catch (BeansException bexc) {
ServiceException e = new ServiceException();
String msg = "Unable to create Spring service named '"
+ beanName + "' ";
e.setMessage(msg);
e.setRootCause(bexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
}
}
}
这个类是用来返回实际的Spring管理的bean的
remote配置如下:
sevice-config.xml
加入:
<factories>
<factory id="spring" class="com.oa.web.SpringFactory" />
</factories>
上面的是spring的工厂配置,注意类路径!
remote-config.xml
<adapters>修改为:(注意看哦,别一贴上去就完了)
<adapters>
<adapter-definition id="java-object"
class="flex.messaging.services.remoting.adapters.JavaAdapter"
default="true" />
<!-- gilead blazeds -->
<adapter-definition id="persistent-adapter"
class="net.sf.gilead.blazeds.adapter.PersistentAdapter">
<properties>
<persistence-factory>
<class>
com.oa.web.SpringApplicationContext
</class>
<singleton>true</singleton>
<method>getSessionFactory</method>
</persistence-factory>
<stateless>false</stateless>
</properties>
</adapter-definition>
</adapters>
有部分标签是原来有的
还需加入服务:
<destination id="userDao">
<adapter ref="persistent-adapter" />
<properties>
<factory>spring</factory>
<source>UserDao</source>
<scope>session</scope>
</properties>
</destination>
这里的factory就是上面 刚配置的 <adapter ref="persistent-adapter" /> 也是刚配置的
source 就是 spring 的 bean 的名字
Flex 端:
package vo{
import mx.collections.ArrayCollection;
import net.sf.gilead.pojo.actionscript.LightEntity;
/**再现Java端的User类,
* 否则Hibernate无法存储信息
* *.as类必须和 *.java类 完全一样,字段个数,字段类型
* 否则出错
*/
[Bindable]
[RemoteClass(alias="com.oa.beans.User")]
public class UserVO extends LightEntity{
/**id*/
public var id:int;
/**用户名*/
public var username:String;
/**密码*/
public var password:String;
注意事项,注释上有了,主要是要建立一个和Java类完全一样的 as类
然后加上
[RemoteClass(alias="com.oa.beans.User")]
也就是为该类对应一个Java为别名
也就是为该类对应一个Java为别名
别忘了要继承
LightEntity 类哦~
然后就完事了
有什么说得不对的,请大家指出!