Closed JustinCRL closed 11 months ago
Confirmed it doesn't work in 3.2.0-SNAPSHOT either.
Why are you declaring Spring Data KeyValue as a dependency, but then use Spring Data JPA?
First, Spring Data KeyValue is not meant to be used directly as a Spring Data store implementation.
As described, the Spring Data KeyValue module and framework provides a common abstraction for Key/Value stores, such as Redis using Spring Data Redis, or Hazelcast using Spring Data Hazelcast. In other words, Spring Data KeyValue builds on the core concepts and foundation from Spring Data (Commons) for Key/Value stores specifically.
NOTE: While Spring Data KeyValue "provides" a
Map
implementation (here, and specifically) of the Spring Data CommonsCrudRepository
interface and DAO abstraction (Javadoc, and this), it is not intended to be used as your Spring Boot/Data application's DAO. It is useful, perhaps, for limited testing purposes.
However, because this "Map" implementation was provided by Spring Data KeyValue (in your Spring Boot/Data application classpath), you have entered "multi-module" mode (see documentation).
Now, you must explicitly declare and distinguish, precisely which persistent entity is mapped to which underlying data store (an RDBMS, as you have configured, along with here, or the cheap, Map-based implementation provided by SD KeyValue; I am certain you wanted H2 in your case).
As described in documentation, this is done by either 1) declaring an ("identifying") mapping annotation on your application persistent entity types or 2) declaring your Repository
interface to extend a Spring Data module-specific CrudRepository
type, such as JpaRepository
from Spring Data JPA.
NOTE: The
@KeySpace
entity annotation is not enough by itself to make this distinction, unlike Spring Data module-specific "mapping" annotations, such as JPA's@Entity
annotation for SD JPA, or the@Document
annotation for SD MongoDB, or the@RedisHash
annotation for Spring Data Redis, and so on.TIP: This is apparent in the SD module, store-specific implementation of
RepositoryConfigurationExtension
, overriddengetIdentifyingAnnotations()
method; for example, in Spring Data Redis, unlike SD KeyValue'sRepositoryConfiguraitonExtension
, which does not declare any "identifying" annotations, only identifying (Repository) types (source).
So, in fact, because your application Repo
extends the "generic" (non-store specific, Spring Data Commons) CrudRepository
, you do (!) need to declare the JPA @Entity
annotation on your application, persistent entity class (KeyValue
, source) in order to clarify which underlying data store will persist and store your entity.
To be clear, you can either
1) (Recommended) Keep your Repo
definition as is, extending CrudRepository<KeyValue, String>
, and simply annotate your KeyValue
entity class with JPA's @Entity
annotation, as you have done before. So...
Given:
@Entity
public KeyValue {
// ...
}
And:
public interface Repo extends CrudRepository<KeyValue, String> { }
Then:
@SpringBootApplication
@EntityScan(basePackageClasses = KeyValue.class)
@EnableJpaRepositories(basePackageClasses = Repo.class)
public class DemoApplication {
// ...
}
2) Alternatively, you can simply do the following (keeping all else the same, i.e. @KeySpace
on KeyValue
, and @EnableMapRepositories
on your DempApplication
class, as you have done):
public interface Repo extend KeyValueRepository<KeyValue, String> { }
This will resolve the Spring Data specific Exception above, but still, you are going to run into JPA (configuration) Exception at runtime.
Arguably, neither @EnableMapRepositories
and @KeySpace
annotations from Spring Data KeyValue make any sense in the context of JPA.
That makes a lot of sense. We have Spring Data JPA is a dependency for other reasons. Thanks for this extensive explanation.
On Mon, Oct 23, 2023, 6:40 PM John Blum @.***> wrote:
Closed #541 https://github.com/spring-projects/spring-data-keyvalue/issues/541 as completed.
— Reply to this email directly, view it on GitHub https://github.com/spring-projects/spring-data-keyvalue/issues/541#event-10747323236, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFX22Z2SULF5HTJZVNK4HTYA355PAVCNFSM6AAAAAA6MQZ53KVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJQG42DOMZSGMZDGNQ . You are receiving this because you commented.Message ID: <spring-projects/spring-data-keyvalue/issue/541/issue_event/10747323236@ github.com>
You are welcome. Good luck!
Reading https://docs.spring.io/spring-data/keyvalue/docs/current/reference/html/#key-value.keyspaces or https://www.baeldung.com/spring-data-key-value it seems that you do not need @Entity however I get an the following error with H2 as my repository
2023-10-21T10:17:06.706-05:00 INFO 3536 --- [ main] .RepositoryConfigurationExtensionSupport : Spring Data Map - Could not safely identify store assignment for repository candidate interface com.example.demo.sql.Repo; If you want this repository to be a Map repository, consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository
I am using Java 17 with Spring Boot 3.1.5
Code: https://github.com/JustinCRL/spring-data-keyvalue StackOverflow: https://stackoverflow.com/questions/77336626/why-is-enitity-required-for-keyspace-enitity-for-spring-data-keyvalue