Flex + SBI + dpHibernate,我想我错过了什么

时间:2010-08-04 11:41:41

标签: flex spring integration blazeds dphibernate

所以,我的问题是:我的所有项目都配置好了吗?但是我没有延迟加载!

这是我准备的一个示例项目,用于准备刚刚发布的真实项目。 简单来说,我有3个表:Category,Product,Client,以及最后两个之间的关联,Buy。

在Flex方面,我有两个简单的数据网格,第一个包含产品,第二个包含与所选产品关联的客户端。 在服务器端,Spring-BlazeDS-Integration架构和检索所有产品的服务。

我的配置文件: 的web.xml

enter code here

<web-app>
    <display-name>Spring BlazeDS Integration</display-name>
    <description>Spring BlazeDS Integration</description>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.properties</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/applicationContext.xml</param-value>
    </context-param>

    <filter>  
        <filter-name>openSessionInViewFilter</filter-name>  
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>ALWAYS</param-value>
        </init-param>    
    </filter>

    <filter-mapping>  
        <filter-name>openSessionInViewFilter</filter-name>  
        <url-pattern>/messagebroker/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Spring MVC Servlet Dispatcher</servlet-name>
        <display-name>Spring MVC Servlet Dispatcher</display-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/webApplicationContext.xml</param-value>
       </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>RDSDispatchServlet</servlet-name>
        <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
        <init-param>
            <param-name>messageBrokerId</param-name>
            <param-value>_messageBroker</param-value>
        </init-param>        
        <init-param>
            <param-name>useAppserverSecurity</param-name>
            <param-value>false</param-value>
        </init-param>        
        <load-on-startup>10</load-on-startup>
    </servlet>

    <servlet-mapping id="RDS_DISPATCH_MAPPING">
        <servlet-name>RDSDispatchServlet</servlet-name>
        <url-pattern>/CFIDE/main/ide.cfm</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Spring MVC Servlet Dispatcher</servlet-name>
        <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>

的applicationContext.xml

<beans xmlns="..">
    <bean id="transactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="txManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="create*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
                <value>tuto/Product.hbm.xml</value>
                <value>tuto/Client.hbm.xml</value>
                <value>tuto/Category.hbm.xml</value>
                <value>tuto/Buy.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/tuto" />
        <property name="username" value="root" />
    </bean>
</beans>

webApplicationContext.xml

<beans xmlns=".....">
    <bean id="hibernate-lazy-adapter" class="org.springframework.flex.core.ManageableComponentFactoryBean">
        <constructor-arg value="net.digitalprimates.persistence.hibernate.HibernateAdapter"/>
        <property name="properties">
            <value>
                {
                    "hibernate": {
                        "sessionFactory": {"class":"net.digitalprimates.persistence.hibernate.utils.SpringSessionUtil", "getCurrentSessionMethod":"getCurrentSession"
                        }, "loadMethod":"load"
                    }
                }
            </value>
        </property>
    </bean>

    <flex:message-broker>
        <flex:remoting-service default-adapter-id="hibernate-lazy-adapter" default-channels="my-amf" />
    </flex:message-broker>

    <bean id="productService" parent="transactionProxy">
        <property name="target">
            <bean class="tuto.ServiceProductImpl">
                <property name="productDAO"><ref bean="productDAO"/></property>
            </bean>
        </property>
        <flex:remoting-destination/>
    </bean>

    <bean id="productDAO" class="tuto.DAOProductHibernate" lazy-init="default">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>

product.hbm.xml

<hibernate-mapping package="tuto">
    <class name="Product" table="product">
        <id name="productId" type="long" column="product_id">
            <generator class="increment"/>
        </id>

        <property name="name" column="name" type="string" not-null="true" length="40"/>

        <many-to-one name="category" column="category_id" class="Category" not-null="true" lazy="false">
        </many-to-one>

        <set name="clients" table="buy" cascade="delete">
            <key column="product_id"/>
            <many-to-many column="client_id" class="Client"/>
        </set>
    </class>
</hibernate-mapping>

这是ProductDAO.java中的代码

public Collection<Product> findAll() {
    Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
    try {
        return session.createQuery("from Product").list();
    } catch (HibernateException e) {
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}

如果您需要更多信息来提供帮助,请询问,我只是不想在第一条消息中发布多少代码:)

因为您可以看到我只是从数据库中检索产品,但是当我在数据网格中选择产品时,会填充客户端的数据网格,因此也会加载客户端! 另外,我有一个日志跟踪,我可以看到Product表上有多个请求,Client表上还有几个请求。 在我看来,只有一个应该在这里!不是吗?!

我在等你的建议,非常感谢!

阿诺。

2 个答案:

答案 0 :(得分:1)

问题解决了!

改变这个:

<flex:message-broker>
    <flex:remoting-service default-adapter-id="hibernate-lazy-adapter" default-channels="my-amf" />
</flex:message-broker>

到此:

<flex:message-broker>
</flex:message-broker>

在所有豆子中,改变这个:

<flex:remoting-destination/>

到此:

<flex:remoting-destination service-adapter="hibernate-lazy-adapter" channels="my-amf"/>

现在有效:)

答案 1 :(得分:0)

BlazeDS中没有延迟加载的机制。如果需要,您必须编写自己的框架,或使用Livecycle DataServices中的数据管理。还有一个open source project旨在做到这一点,但我从未玩过它。

另一种选择是使用拆分模型并使用远程方法,以便在选择相应的产品时获取客户端。