嵌入式 infinispan 的休眠 ogm 依赖问题

hibernate ogm dependency issue with embedded infinispan

我正在尝试使用 Infinispan 嵌入式模式测试 OGM,使用简单的实体、存储库和服务 class。

@Entity
@Table(name="test_person")
public class PersonEntity implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "person_id", nullable=false, updatable=false)
    private Long id;
    
    @Column(name = "person_code", nullable=false, updatable=false)
    private String person_code;
    
    @Column(name = "person_name", nullable=true, updatable=true)
    private String person_name;
    ....
}

存储库 --

@Repository("personRepository")
public interface PersonRepository extends JpaRepository<PersonEntity, Long> {
    
    
    
}

服务--

@Service("personService")
public class PersonService {
    
    @Autowired
    @Qualifier("personRepository")
    private PersonRepository personRepo;
    
    public PersonEntity addPerson(PersonEntity person) {
        
        PersonEntity p = personRepo.save(person);
        
        return(p);
    }
    
}

联合测试--

@RunWith(SpringJUnit4ClassRunner.class)
@DataJpaTest
@ContextConfiguration(locations="/application-test-context.xml")
@SpringBootTest
public class PersonServiceTest {
    
    @Autowired
    @Qualifier("personService")
    private PersonService svc;
    
    @Test
    public void test_add1() {
        PersonEntity p = makePerson("123");
        svc.addPerson(p);
    }
    
    private PersonEntity makePerson(String uid) {
        PersonEntity p = new PersonEntity();
        p.setPerson_code("ABC_XYZ" + uid);
        p.setPerson_name("Abc Xyz" + uid);
        return(p);
    }
}

在 运行 测试时,出现以下异常 --

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.ogm.jpa.HibernateOgmPersistence]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/jpa/HibernatePersistenceProvider 
.....  
Caused by: java.lang.NoClassDefFoundError: org/hibernate/jpa/HibernatePersistenceProvider   
    at org.hibernate.ogm.jpa.HibernateOgmPersistence.<init>(HibernateOgmPersistence.java:37) ~[hibernate-ogm-core-5.4.0.Final.jar:5.4.0.Final]  
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_231]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_231]     
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_231]     
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_231]  
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]     ... 52 common frames omitted  
Caused by: java.lang.ClassNotFoundException: org.hibernate.jpa.HibernatePersistenceProvider     
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_231]   
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_231]    
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_231]    
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_231]    
... 58 common frames omitted

我的 pom 包括以下内容:

<dependency>
   <groupId>org.hibernate.ogm</groupId>
   <artifactId>hibernate-ogm-infinispan-embedded</artifactId>
   <version>5.4.0.Final</version>
</dependency>

<dependency>
   <groupId>org.jboss.narayana.jta</groupId>
   <artifactId>narayana-jta</artifactId>
   <version>5.9.2.Final</version>
</dependency>

看来,OGM 5.4.0.Final(甚至尝试过 5.4.1.Final )正在引入休眠核心 5.0.12.Final ,它很旧并且没有打包org.hibernate.jpa.HibernatePersistenceProvider。为此,我们还需要包含 hibernate-entitymanager。但是,即使在包含 entitymanager 之后,我仍然遇到以下问题:

Caused by: java.lang.NoClassDefFoundError: org/hibernate/resource/transaction/spi/TransactionCoordinatorBuilder
    at org.hibernate.ogm.boot.impl.OgmServiceRegistryInitializer.contribute(OgmServiceRegistryInitializer.java:73) ~[hibernate-ogm-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.applyServiceContributors(StandardServiceRegistryBuilder.java:305) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.build(StandardServiceRegistryBuilder.java:273) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:176) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:34) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:165) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:160) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:135) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.ogm.jpa.HibernateOgmPersistence.createContainerEntityManagerFactory(HibernateOgmPersistence.java:89) ~[hibernate-ogm-core-5.4.0.Final.jar:5.4.0.Final]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    ... 46 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_231]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_231]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_231]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_231]
        ... 60 common frames omitted

在 hibernate core 5.0.12.Final 中,T运行sactionCoordinatorBuilder 在“org.hibernate.resource.transaction”包中而不在“org.hibernate.resource.transaction.spi”包中。

有人可以帮我把正确的依赖项包含在 pom 中吗?我什至试图将 hibernate 核心升级到最近的版本,但我 运行 遇到了另一组依赖问题!

Hibernate OGM 5.4.1.Final 需要 Hibernate ORM 5.3.6.Finalhttp://hibernate.org/ogm/releases/5.4/

您项目中的一些其他依赖项正在拉取 Hibernate ORM 5.0.12.Final

好吧,找出兼容的版本真的很麻烦。但是这很有效 --

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

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.6.Final</version>
</dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search</artifactId>
        <version>5.9.3.Final</version>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.hibernate.ogm</groupId>
        <artifactId>hibernate-ogm-infinispan-embedded</artifactId>
    </dependency>