Spring Data JPA to Spring Data Redis - what all changes are needed? - spring

I am new to Redis and trying to implement change Postgres DB into the Redis DB. Current implementation we've used Spring Data JPA (Entity Classes + JPA Repository). Now I wanted to use Spring Data Redis and backend as Redis In Memory DB.
Could you please suggest what changes do I need to make in all Entity classes ? How the persistence will happen ? Do I need to make any changes in Join Table etc.. where I have #ManyToOne and #ManyToMany relationship.

Redis a nosql solution so you will not get the befit of foreign keys as in a RDBMS. You will instead need to convert you data into data-structures supported by Redis. https://redis.io/topics/data-types-intro

Related

Different Springboot services using a same tables

We have a running Springboot service A that created some relational entities using Spring JPA with Hibernate ORM.
We need to create a new Springboot service B that needs to access A's tables but with different queries.
There are few options I though of:
Making service B use Spring JPA and Hibernate and copy the same entity models from service A
But I'm not sure if this method causes any synchronization issues caused by Hibernate's first level caching.
Both services will not be using 2nd level cache.
Same like option 1, making service B use Spring JPA and Hibernate but import service A as a dependency in service B instead of copying the entity models.
Making service B use Spring JdbcTemplate if we are not creating any new entities in service B.
I also like to know how service B's table can have a unidirectional foriegn key relationship (#ManyToOne or #OneToOne) with service A's table.
Please suggest me which option is better or if there's a better way.
If it is a bad pratice to use other service's tables, please suggest the correct design. \
Thanks

Is it possible to implement read through and write through in apache ignite using jpa?

I wanted to read and write data from underlying db with Apache ignite . I'm doing a spring boot app so i just want to know whether i can use JPA features for it ?
As far as my understanding goes, Apache Ignite does not implement any JPA APIs. So you can't access data stored in Ignite in JPA fashion.
However, you can probably use JPA to cache data in Ignite while writing it to, and reading from, underlying JPA store. In this case you will need to implement your own CacheStore. Example: https://github.com/gridgain/gridgain-advanced-examples/tree/master/src/main/java/org/gridgain/examples/datagrid/store (this is based on Mongo but you can rewrite it to use JPA).

Is Repository the only way to save entity data on DB?

I'm studying Spring boot, and i have create several entity, my problem now is to retrieve the data from controller to save it on db.
Surfing on the web I have learned that i have to use JPARepositery or CrudRepositery in this way:
An example with User entity
public interface UserRepository extends CrudRepository<User, Integer> {
}
and to save
User user = new User();
userRepository.save(user);
But if I have many entities, Do I need to create a repository for each entity?
I have read about Session, FactorySession and Transaction they are compatible with Spring boot? How it works? and #Transactional tag how it works?
Thanks in advance
Yes, usually you need to create one Repository per Entity. This is also a good practice because you are placing operations and queries related to the same Entity in the same Repository.
Yes, you can obtain a Session and a FactorySession object (Hibernate), but I would advice you against using Hibernate directy. Instead, it is better to use Spring Data or JPA mechanisms to access your database (just as you do using a Repository). The reason is because Hibernate is an implementation of the JPA standard and today we use JPA to access databases (because it masks the exact implementation of the ORM). This way you can later (potentially) replace Hibernate with another JPA implementation (such as EclipseLink) without the need to change your code (in theory). In most projects you will find Hibernate being used, however.
Yes, Transaction is a Spring annotation and an important database transaction mechanism in Spring Boot, too.

How to insert/update native queries in DB using spring JPA?

Am working on a Spring boot application using spring data JPA. The DB is a legacy one. There is a staging table which contains insert/update queries as CLOB data.
I have written a pojo for the staging table. The pojo implements CRUDRepository interface. I fetched the records from list() method and iterate the CLOB data. The queries present in the CLOB needs to be executed in other DB. The queries are insert/update queries related to 20+ tables.
Without creating pojos for that 20+ tables, how can I execute those SQL's.
The SQL's need no modifications, just need to execute the same that I fetch from the staging table. Is
EntityManager.createNativeQuery("insert/update")
a possible solution, or is there a better approach to handle it.
You could do that with JPA native queries for sure. Or even with plain JDBC.
But I recommend having a look at jOOQ. jOOQ generates POJOs for accessing and modifying the data and has a DSL that leads to compile time checked data access:
https://www.jooq.org/
jOOQ is free for OpenSource databases like MySQL, PostgreSQL etc. and affordable for commercial ones like Oracle.

Javers - What are advantages of using Javers instead of Envers?

I am developing a RESTful API using Spring Data REST. Now for auditing, Spring does have the option to auditing meta data like created_date and modified_date but they don't provide entity versioning.
Currently there are two popular libraries for entity version which are Envers and Javers. I have looked over for a comparison of both but there arent any articles on this matter.
So what are the benefits and drawbacks of using Javers over Envers?
There are two big difference between JaVers and Envers:
Envers is the Hibernate plugin.
It has good integration with Hibernate but you can use it only with traditional SQL databases.
If you choosed NoSQL database or SQL but with other persistence framework like
JOOQ — Envers is not an option.
On the contrary, JaVers can be used with any kind of database and any kind of
persistence framework. For now, JaVers comes with repository implementations for MongoDB and
popular SQL databases. Other databases (like Cassandra, Elastic) might be added in the future.
Envers’ audit data model is a copy of application’s data model. As the doc says:
For each audited entity, an audit table is created.
By default, the audit table name is created by adding a _AUD suffix to the original name.
It can be advantage, you have audit data close to your live data. Envers’ tables look familiar.
It’s easy to query them with SQL.
JaVers uses its own Snapshot model for audit data.
Snapshots are decoupled from live data,
JaVers saves them to the single table (jv_snapshots) as JSON documents with unified structure.
Advantages? You can choose where to store audit data.
By default JaVers uses the same database as application does,
but you can point another database. For example, SQL for application and MongoDB for JaVers
or centralized JaVers database shared for all applications in your company).
Read this blogpost with full JaVers vs Envers comparison:
https://javers.org/blog/2017/12/javers-vs-envers-comparision.html
Enver is like git for a database.
I do not know Javers but a complete Envers databinding has this advantages:
A table is created in the database called REVINFO having a timestamp and a PK.
To every entity that is audited, one shadow-copy is created. Theese shadow-copies have every field nullable and the PK is not a PK. Theese shadow-copies have a new field, the reference to the table REVINFO.
This gives Enver the possibility to record changes that has been made in the past in this shadow-copies. You can move that shadow-tables into an different database.

Resources