1

We have a Spring application that connects with no problems to a MongoDB, with @Autowire and all the rest.

Now we also need the app to connect also to an SQL database.

So we crated an @entity class:

@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;
....

And a @repository interface:

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

And a @Configuraion class like the example here https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-one-configuration/

And in the applicationContext we added <jpa:repositories base-package="path.to.interface.package" />

In the pom.xml we already have

        <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>

and we added:

        <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>

And in the @Service class we added:

    ....

    @Autowired
    private SqlCarsRepository carsRepository;

The project is built successfully, but when we try to run it, we get this error:

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!

Some of the things We tried:

  • change the different versions of spring in the pom,
  • we tried to comment some of them,
  • we tried to change the interface to extend CrudRepository,
  • tried to add an empty constructor to the entity and some other things

with no luck.

Will appriciate help.

Thanks in advance.

t-rex-50
  • 201
  • 1
  • 4
  • 10

1 Answers1

1

I solved the same error message by changing the @SpringBootApplication annotation to this:

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

If you are using the @EnableAutoConfiguration directly, try this:

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

It seems that the root problem is that the Spring Boot is trying to add something that is already on the classpath.

Most of answer taken from https://stackoverflow.com/a/30597861/7470360

A Jar of Clay
  • 5,622
  • 6
  • 25
  • 39