" IllegalArgumentException:必须至少存在一个JPA元模型" - 尝试将应用程序连接到mongo和sql时

时间:2017-08-30 14:08:42

标签: spring maven jpa spring-boot

我们有一个Spring应用程序,可以通过@Autowire和其他所有内容连接到没有问题的MongoDB。

现在我们还需要应用程序将连接到SQL数据库。

所以我们创建了一个 @entity 类:

@Entity(name = "SqlCarRecord")
@Table(name = "Cars")
final public class SqlCarRecord {

@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "name", nullable = false)
private String name;
....

@repository 界面:

@Repository 
public interface SqlCarsRepository extends JpaRepository<SqlCarRecord, Long> {
...

@Configuraion 类就像这里的示例https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-one-configuration/

applicationContext 中我们添加了 <jpa:repositories base-package="path.to.interface.package" />

pom.xml 中,我们已经有了

        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>

我们补充说:

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.0.0.M3</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
    </dependency>

    <!-- DataSource (HikariCP) -->
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.6.2</version>
    </dependency>

    <!-- JPA Provider (Hibernate) -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.10.Final</version>
    </dependency>

    <!-- Spring Data JPA -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.11.6.RELEASE</version>
    </dependency>

    <!-- adding this to avoid "java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal" -->
    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>1.4.01</version>
    </dependency>

    <!-- adding this to avoid "ClassNotFoundException: org.springframework.data.util.CloseableIterator" -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>1.13.6.RELEASE</version>
    </dependency>

@Service 类中,我们添加了:

    ....

    @Autowired
    private SqlCarsRepository carsRepository;

项目已成功构建,但是当我们尝试运行它时,我们收到此错误:

Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'jpaMappingContext': Invocation of init method failed; nested 
exception is java.lang.IllegalArgumentException: At least one JPA metamodel 
must be present!

我们尝试过的一些事情:

  • 更改pom中不同版本的弹簧,
  • 我们试图评论其中一些,
  • 我们尝试更改界面以扩展 CrudRepository
  • 尝试将空构造函数添加到实体和其他一些东西

没有运气。

将获得帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我通过将@SpringBootApplication注释更改为此来解决了相同的错误消息:

@SpringBootApplication(exclude = {JndiConnectionFactoryAutoConfiguration.class,DataSourceAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})

如果您直接使用@EnableAutoConfiguration,请尝试:

@EnableAutoConfiguration(exclude = {JndiConnectionFactoryAutoConfiguration.class,DataSourceAutoConfiguration.class,
                                    HibernateJpaAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})

似乎根本问题是Spring Boot正在尝试添加已经在类路径上的东西。

大部分答案来自https://stackoverflow.com/a/30597861/7470360