How can we store BinaryDocument in springData - spring-data

I am able to create BinaryDocuemnt in CouchBase dB using couch base java library.
JsonDocument doc = JsonDocument.create("document_id", JsonObject.create().put("some", "value"));
System.out.println(bucket.insert(doc));
Can we store JsonDocument using Spring-data repository method ?

Spring Data offers an abstraction over the JsonDocument, storing POJO as JSON, so one could say JsonDocument is supported indirectly. There is no support for BinaryDocument, as this is orthogonal to Spring Data's concerns.
However you can transitively access the SDK from the Spring Data Couchbase CouchbaseRepository and CouchbaseOperations:
CouchbaseRepository<String> couchRepo;
// ^ note this is the explicit couchbase interface for repositories
Bucket bucket = couchRepo.getCouchbaseOperations().getCouchbaseBucket();
//work directly with the couchbase Bucket SDK from here

Related

Is it possible to have single repository for both relational and non-relation db in spring data?

I Want to make my microservice compatible with both relational and non-relational databases,
Currently, I have five small modules which make a single microservice:
Controller-module - This contains all my controllers and has a dependency on the service module
Service module - This contains services which communicate with DB layer to get data or post data, and obviously has a dependency on data layer (common-db-modules)
Common-db-modules- this module defines contracts (interfaces only) for datalayers to follow in order to be compatible with service
Data-layer - this module provides concrete implementation of contracts defined by common-db-module. There could be multiple data-layers depending on the database. which means one for Mongo DB, one for MySQL, etc
Packaging: number of packaging module is defined based on db used, packing packages
controller-module
Service-Module
One Data-Layer (DB for which this packaging is made)
My question: is there a way to eliminate this common contract for DB and have a single implementation of the Data layer which will serve both relational and non-relational DB?
you don't have to build data layer by your self, Spring already creates Data access object (DAO) for you. use JPA repository which are capable for fetching data from both sql and nosql.
kindly refer https://docs.spring.io/spring-data/jpa/docs/2.2.6.RELEASE/reference/html/#repositories
and you also needed to configure.. Database

Spring Data Crate custom queries

I am trying to implement Spring Data Crate API in a project. Following the instructions provided here:
https://crate.io/a/using-sprint-data-crate-with-your-java-rest-application/
Inserts/Updates/FindById methods are covered. My question is how to create custom queries using this API.
Have you looked at our Spring Data adapter?
Declared Queries
It's also possible to use the #Query annotation to define queries:
public interface UserRepository extends CrateRepository<User, String> {
#Query("select * from users")
List<User> getAllUsers();
}
https://github.com/crate/spring-data-crate#declared-queries
please note that the crate java-client is not supported anymore since v0.57 unfortunately.
https://crate.io/docs/clients/java/
This leaves us with the java-jdbc:
https://github.com/crate/crate-sample-apps/blob/master/java/documentation.md
Spring data adapter is using the java-client.
Here is a Spring Boot application that is using JDBC in order to access CrateDB (> v0.57.0) https://github.com/klearchos/crate
And here are the official samples in order to access CreateDB through JDBC (using the Spark framework). https://github.com/crate/crate-sample-apps/tree/master/java

EventListener like AbstractMongoEventListener in Spring Data JPA?

We are migrating from a Spring Data MongoDB repository to a Spring Data JPA repository. We were using the AbstractMongoEventListener to capture onBeforeConvert and onAfterLoad events to enhance the data objects before returning them from our Service layer.
I cannot find similar EventListeners in Spring Data JPA. Are there hooks in Spring Data JPA that I can use to do the same thing? It would be great if I can avoid modifying our service layer to make this change from MongoDB to JPA.
Thanks!
The #PrePersist annotation is exactly what I was looking for.

How to retrieve persisted POJOs with Cassandra and Hector Object Mapper

I'm trying to play a little bit with the Hector Object Mapper to map pojos to/from Cassandra. Since I'm a newbie in JPA standard, I'm simply trying to persist some basic beans to Cassandra and then get them back. I'm using Hector-object-mapper 3.0-04, Hectore-core 1.0-5, Cassandra server 1.2.2.
So I save my SimpleBean pojo to cassandra with
EntityManagerImpl em = new EntityManagerImpl(keyspace, "it.nosql.tests");
SimpleBean simpleBean = new SimpleBean();
simpleBean.setCassandraId(UUID.randomUUID());
simpleBean.setInteger(1000);
simpleBean.setStr("cassandraCrossing");
em.persist(simpleBean);
and then i retrieve it specifying the UUID
SimpleBean newSB = em.find(SimpleBean.class, simpleBean.getCassandraId());
Is there a way to retrieve all the SimpleBean instances persisted to Cassandra without specifying an ID? I expected to have something similar
List<SimpleBean> simpleBeanList = em.find(SimpleBean.class).asList();
Thanks,
F.

How to read the schema used by a JPA implementation

My EntityManager is using a persistence unit that uses a data source provided by our Websphere configuration. The DS configuration includes an environment specific DB to use.
The EM successfully uses this schema, but I can't figure out a way to log or display the schema being used. I was thing something like em.getCurrentSchema would be available..
Any help would be great, thanks.
No API to do this (in JPA). You could do it via JDBC and use of DatabaseMetaData.
JPA is to provide an object view of the data and ease persistence of those objects, not to just present datastore specifics to the user.