Hibernate-OGM JpaRepository:具有不兼容返回类型的getSessionFactory()

时间:2019-04-03 08:37:48

标签: mongodb hibernate hibernate-ogm

尝试在Spring-Boot项目中为MongoDB配置Hibernate-ogm时,由于两种方法INSTEAD OF具有相同的签名但返回了不兼容的类型,因此应用程序无法启动:

UPDATE

这些是我的依赖项:

getSessionFactory()

目前唯一涉及的实体是:

interface org.hibernate.ogm.OgmSessionFactory
interface org.hibernate.engine.spi.SessionFactoryImplementor

与此存储库配对:

<dependencyManagement>
    <dependencies>
        <!--This will make sure that you are using matching versions
        of the Hibernate OGM modules and their 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>

        <!--Due to the different inception dates of individual Spring Data modules,
        most of them carry different major and minor version numbers.
        The easiest way to find compatible ones is to rely on the Spring Data Release
        Train BOM that we ship with the compatible versions defined.
        -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>${release-train}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!--Spring-Boot-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!--Lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>provided</scope>
    </dependency>

    <!--Mail-->
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
    </dependency>

    <!--Hibernate-OGM-->
    <dependency>
        <groupId>org.hibernate.ogm</groupId>
        <artifactId>hibernate-ogm-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jboss.narayana.jta</groupId>
        <artifactId>narayana-jta</artifactId>
        <version>5.9.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.1.6.RELEASE</version>
    </dependency>

</dependencies>

持久性的整个配置是通过以下配置类完成的:

import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Indexed;

import javax.persistence.*;
import java.util.List;

@Entity
@Indexed
@Getter
@Setter
public class Email {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;
    @ElementCollection
    private List<String> from;
    @ElementCollection
    private List<String> to;
    private String object;
    private byte[] eml;
    @Column(unique = true)
    private byte[] digest;
}

当我尝试注入上述存储库时,出现标题中提到的错误;这是它的整个日志:

import my.project.domain.Email;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmailRepository extends JpaRepository<Email, Long> {
}

该错误表明可能存在某些依赖冲突,但是我找不到错误的依赖关系。有这个想法吗?

更新 向问题添加其他信息; 也许正确的类型应该是:

import com.mongodb.MongoClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableJpaRepositories(basePackages = "my.project.repository")
@EnableTransactionManagement
public class DatabaseConfiguration extends AbstractMongoConfiguration {

    @Value("${spring.data.mongodb.database}")
    private String databaseName;

    @Value("${spring.data.mongodb.host}")
    private String host;

    @Value("${spring.data.mongodb.port}")
    private String port;

    @Override
    public MongoClient mongoClient() {
        System.out.println("Port: " + port);

        MongoClient client = new MongoClient(host, Integer.parseInt(port));

        return client;
    }

    @Override
    protected String getDatabaseName() {
        return databaseName;
    }

    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean emf() {
        Map<String, Object> properties = new HashMap<>();
        properties.put("javax.persistence.transactionType", "resource_local");
        properties.put("javax.persistence.provider", "org.hibernate.ogm.jpa.HibernateOgmPersistence");

        properties.put("hibernate.ogm.datastore.provider", "mongodb");
        properties.put("hibernate.ogm.datastore.host", "localhost");
        properties.put("hibernate.ogm.datastore.port", "27017");
        properties.put("hibernate.ogm.datastore.database", "myproject");
        properties.put("hibernate.ogm.datastore.create_database", "true");

        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setPersistenceProviderClass(org.hibernate.ogm.jpa.HibernateOgmPersistence.class); //If your using eclipse or change it to whatever you're using
        emf.setPackagesToScan("my.project.domain"); //The packages to search for Entities, line required to avoid looking into the _persistence.xml
        emf.setPersistenceUnitName("primary");
        emf.setJpaPropertyMap(properties);
        //emf.setLoadTimeWeaver(new ReflectiveLoadTimeWeaver()); //required unless you know what your doing
        return emf;
    }

    @Bean(name = "mongoTransactionManager")
    public PlatformTransactionManager transactionManager() throws Throwable {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf().getNativeEntityManagerFactory());
        return transactionManager;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为Spring Boot和Hibernate OGM在您的项目中包括两个不同的Hibernate ORM版本,这引起了冲突。您需要检查它们是否都使用相同的版本,这意味着可能降级了两者之一。

通过这种方式,OGM正在创建自己的MongoClient,如果在连接过程中遇到问题,则可能需要更新配置或覆盖自定义方言中的方法MongoDBDatastoreProvider#createMongoClient。 然后,您可以使用自定义方言代替带有属性的默认方言: hibernate.ogm.datastore.provider