0

当我将 infinispan ogm 与文件存储配置一起使用时,我遇到了一个问题。我的问题如下。我在 wildfly 17 上部署了一个非常小的 REST 应用程序,能够持久保存和读取非常简单的数据模型。数据模型由双向多对一关联组成。

@Entity
public class Foo {
    @Id
    private String id;

    private String name;

    @OneToMany(cascade = CascadeType.ALL , orphanRemoval = true, mappedBy = "foo")
    private List<Bar> bars = new ArrayList<>();

    public void addBar(String name) {
        Bar bar = new Bar();
        bar.setName("Bar_"+name);
        String id_foo = "Foo_" + name + "_id";
        String id_bar = "Bar_" + id_foo;
        bar.setId(id_bar);
        bars.add(bar);
        bar.setFoo(this);

   //getter and setter
}
@Entity(name = "Bar")
public class Bar {

    @Id
    private String id;

    private String name;

    @ManyToOne
    private Foo foo;

    }

}

public class FooBarService {

    @Inject
    protected EntityManager entityManager;

    @Transactional
    String persisitFoo(String name) {


        Foo foo = new Foo();
        String id_foo = "Foo_" + name + "_id";
        foo.setId(id_foo);
        foo.setName("Foo_"+name);

        //add a bar
        foo.addBar(name);

        entityManager.persist(foo);
        entityManager.flush();

        Foo result = entityManager.find(Foo.class, id_foo);
        return result.toString();
    }

    @Transactional
    String getBar(String name) {

        String id_foo = "Foo_" + name + "_id";
        String id_bar = "Bar_" + id_foo;

        // build the EntityManagerFactory as you would build in in Hibernate ORM

        Bar result = entityManager.find(Bar.class, id_bar);
        entityManager.flush();

        return result.toString();
    }

    @Transactional
    String getFoo(String name) {


        String id_foo = "Foo_" + name + "_id";

        // build the EntityManagerFactory as you would build in in Hibernate ORM

        Foo result = entityManager.find(Foo.class, id_foo);
        entityManager.flush();

        return result.toString();
    }


当我坚持一个 Foo (由一个 Bar 组成)时,我可以 getFoo 并且 Foo 对象包含一个 Bar ,但是当我在获得 Foo 对象时重新启动其余应用程序时,我失去了 Bar 关联。当我得到 Bar 对象时,存在与 Foo 对象的关联。这是我的应用程序 presistence.properties 的配置(我使用生产者以编程方式构建基于 HibernateOgmPersistence 的 entityManager)。似乎双向仅以一种方式起作用

####################################################################
# Configure hibernate OGM based on infinispan provider
####################################################################
hibernate.ogm.datastore.provider=infinispan_embedded

#############################################
#  CACHE_PER_KIND: Three caches will be used: 
#  1) one cache for all entities
#  2) one cache for all associations 
#  3) one cache for all id sources
##############################################
hibernate.ogm.datastore.keyvalue.cache_mapping=CACHE_PER_KIND
########################################################################
# Should point to the resource name of an Infinispan configuration file.
# The referenced Infinispan configuration should define a CacheStore
# for entities, associations adn id sources 
#########################################################################
hibernate.ogm.infinispan.configuration_resource_name=ems-model-infinispan.xml

这是 infinispan 配置

<?xml version="1.0" encoding="UTF-8"?>
<!--
 ~ Hibernate OGM, Domain model persistence for NoSQL datastores
 ~
 ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later
 ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<infinispan
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:infinispan:config:9.4 http://infinispan.org/schemas/infinispan-config-9.4.xsd"
        xmlns="urn:infinispan:config:9.4">


    <jgroups>
        <!-- This is the default JGroups stack -->
         <stack-file name="ems-jgroups" path="ems-jgroups-udp.xml"/>
    </jgroups>

    <cache-container name="ems-model" default-cache="DEFAULT" statistics="false" shutdown-hook="DONT_REGISTER">

        <transport stack="ems-jgroups"  cluster="EMS"/>
        <jmx duplicate-domains="true"/>

        <!-- ***************************************************************** -->
        <!--     Default cache (no longer used as template, since infinispan 9 -->
        <!-- ***************************************************************** -->
        <replicated-cache name="DEFAULT" mode="SYNC" statistics="false" statistics-available="false">
            <locking isolation="REPEATABLE_READ"/>
            <!--  transaction mode="BATCH" locking="PESSIMISTIC" transaction-manager-lookup="org.infinispan.transaction.lookup.WildflyTransactionManagerLookup"/ -->
            <transaction mode="BATCH" locking="PESSIMISTIC"/>
            <expiration interval="-1"/>
            <persistence>
                <file-store fetch-state="true" preload="true" path="/root/infinispan"/>
            </persistence>
            <state-transfer timeout="480000"/>
        </replicated-cache>

        <!-- *************************************** -->
        <!--     Cache to store the OGM entities     -->
        <!-- *************************************** -->
        <replicated-cache name="ENTITIES" mode="SYNC" statistics="false" statistics-available="false">
            <locking isolation="REPEATABLE_READ"/>
            <transaction mode="BATCH" locking="PESSIMISTIC"/>
            <expiration interval="-1"/>
            <persistence>
                <file-store fetch-state="true" preload="true" path="/root/infinispan"/>
            </persistence>
            <state-transfer timeout="480000"/>
        </replicated-cache>

        <!-- *********************************************** -->
        <!--   Cache to store the relations across entities  -->
        <!-- *********************************************** -->
        <replicated-cache name="ASSOCIATIONS" mode="SYNC" statistics="false" statistics-available="false">
            <locking isolation="REPEATABLE_READ"/>
            <transaction mode="BATCH" locking="PESSIMISTIC"/>
            <expiration interval="-1"/>
            <persistence>
                <file-store fetch-state="true" preload="true" path="/root/infinispan"/>
            </persistence>
            <state-transfer timeout="480000"/>
        </replicated-cache>

        <!-- ***************************** -->
        <!--   Cache to store identifiers  -->
        <!-- ***************************** -->
        <replicated-cache name="IDENTIFIERS" mode="SYNC" statistics="false" statistics-available="false">
            <locking isolation="REPEATABLE_READ"/>
            <transaction mode="BATCH" locking="PESSIMISTIC"/>
            <expiration interval="-1"/>
            <persistence>
                <file-store fetch-state="true" preload="true" path="/root/infinispan"/>
            </persistence>
            <state-transfer timeout="480000"/>
        </replicated-cache>

    </cache-container>
</infinispan>

hibernate ogm 使用的版本是

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.hibernate.ogm</groupId>
                <artifactId>hibernate-ogm-bom</artifactId>
                <version>5.4.1.Final</version> 
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

请注意, 我已经使用 mongodb 更改了提供程序,并且它运行良好。

hibernate.ogm.datastore.provider=mongodb
hibernate.ogm.datastore.host:127.0.0.1:27017
hibernate.ogm.datastore.database:ems
hibernate.ogm.datastore.create_database:true
4

0 回答 0