服务器群集中的JPA实体

问题描述 投票:2回答:1

我们开始使用JSF(TomEE上的MyFaces)和JPA(Eclipselink)开发新的Web应用程序。 为了缩短开发时间,我们计划不开发DTO层,主要是因为我们不需要它。继JSF和Java EE专家建议像Bauke Scholtz How to use DTO in JSF + Spring + Hibernate和Adam Bien How evil are Data Transfer Objects之后,我们将直接在表示层使用JPA实体。 但是,此应用程序必须在具有粘性会话的服务器群集中运行当服务器因维护而关闭或者从群集中排除以进行应用程序部署时,该服务器的用户会话必须由其他服务器提供服务,而不会丢失会话。 我们面临的问题是在会话中保存的JPA实体(例如在@ViewScoped bean中)不会在其他服务器上“完全”复制。事实上,使用延迟加载的JPA实体的集合属性在其他服务器上不可用。在具有会话副本的服务器上访问集合attibute(@OneToMany使用延迟加载)时,异常

org.eclipse.persistence.exceptions.ValidationException 
Exception Description: An attempt was made to traverse a relationship 
using indirection that had a null Session.  
This often occurs when an entity with an uninstantiated LAZY 
relationship is serialized and that relationship is traversed 
after serialization. 
To avoid this issue, instantiate the LAZY relationship 
prior to serialization

被扔了。

我知道EntityManager不可序列化,并且在会话迁移期间序列化时JPA实体完全分离,请参阅Struberg's Blog。 所以问题是,有没有办法在服务器集群中以一致的方式维护JPA实体,而不使用EAGER加载?

session jpa jsf eclipselink
1个回答
0
投票

您只能为某些实体配置缓存(一次更改非常罕见)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="name" transaction-type="JTA">
        <!-- disable shared cache because we are in multi instance environment -->
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <validation-mode>CALLBACK</validation-mode>

        <properties> 
            <!-- disable object caching because we are in multi instance environment -->
            <property name="eclipselink.cache.shared.default" value="true"/>
            <property name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE"/>
            </properties>
    </persistence-unit>

</persistence>

您还可以在这里看到如何在共享环境https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Query_Cache中执行此操作

© www.soinside.com 2019 - 2024. All rights reserved.