The application should use Spring boot JPA to map the model classes into mongo documents. To do so, it adds the following dependencies:
grade.build:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.mongodb:mongo-java-driver:3.12.14'
The properties.yml file:
spring:
datasource:
url: mongodb://psvdev01:27017/test //no authentication needed
driverClassName: com.mongodb.Mongo
When starting the program I got these error:
2024-03-01T17:36:11.258Z INFO 31596 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-03-01T17:36:11.460Z INFO 31596 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 170 ms. Found 1 JPA repository interface.
2024-03-01T17:36:12.578Z INFO 31596 --- [ Test worker] org.mongodb.driver.cluster : Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=50}
2024-03-01T17:36:12.678Z INFO 31596 --- [127.0.0.1:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
...
2024-03-01T17:36:12.884Z WARN 31596 --- [ Test worker] c.zaxxer.hikari.util.DriverDataSource : Failed to create instance of driver class com.mongodb.Mongo, trying jdbcUrl resolution
...
java.lang.ClassCastException: class com.mongodb.Mongo cannot be cast to class java.sql.Driver (com.mongodb.Mongo is in unnamed module of loader 'app'; java.sql.Driver is in module java.sql of loader 'platform')
...
Caused by: java.sql.SQLException: No suitable driver
From this log it looks that Spring JPA is not talking to the driver, but the driver tries, by itselt, to connect to localhost. Then, it looks the JPA try's to connect itself to SQL but there is no SQL dependency declared. Probably SQL is the default implementation for JPA.
The problem should be simple once the configuration is explained, but I couldn't find so far any documentation for this scenario. Almost everything I could find only explains how to use spring-data-mongodb. Also, I can't be sure what kind of mongodb driver to use for this purpose.
How to set up spring data JPA so it uses an mongodb driver?
I think you mess a lot of things up, let's go one by one.
The application should use Spring boot JPA
There is no such thing as "Spring boot JPA". There is a Spring (or springframework if we prefer), on top of which we have Spring Boot as the auto-configuration framework that is here to just ease the use of Spring and reduce the amount of configuration Spring Developers usually have to create.
Spring Data is the umbrella project inside Spring that is here to ease the work with the underlying storage engine. Spring Data JPA is the project that is designed to work with Relational Databases using JPA approach.
Caused by: java.sql.SQLException: No suitable driver
This error you see above is just because you are trying to use MongoDB client to connect to relational database. In order to connect to relational database, you need a java.sql.Driver
implementation in runtime classpath, which you do not have, because you did not include one. Moreover, you do not need it, because you want to work with Mongo which does not require any of that. So just remove Spring Data JPA module, you do not need it.
How to set up spring data JPA so it uses an mongodb driver?
No way, because Spring Data JPA is a project that is designed on top of Hibernate to work with RDBMS. It is simply not intend to work with anything other then Relational Databases. For Mongo, there is another, dedicated module in spring-data
project, called Spring Data MongoDB. You should use it instead of Spring Data JPA.