Spring batch jpa and schema questions - spring-batch

In documentation it says "JPA doesn't have a concept similar to the Hibernate StatelessSession so we have to use other features provided by the JPA specification." - what does this mean? Hibernate is one of the jpa impl so bit confused here
Looking for example where we use jpa infra that we have (entity/crud repo) and we want to use that to read data and write data. Most examples talk about file reading and writing and some about jdbc cursor reader. But since we are using other feature of hibernate like envers we want to use same jpa way that we are using for our online transactions. We are using spring boot/jpa (hibernate) out of the box with oracle and in memory h2 db for dev.
In prod we use oracle, we have user that access to some schemas, how we can inform spring batch to use particular schema to create tables. Right now for some time same application will be use for batch and online so we dont want to use second datasource and different user for batch if possible. Isnt this very basic requirement for all?
Good documentation of spring batch and also liked java/xml config toggle.
We use springboot 2.x with batch.

In documentation it says "JPA doesn't have a concept similar to the Hibernate StatelessSession so we have to use other features provided by the JPA specification." - what does this mean?
The direct equivalent of the Hibernate Session API in JPA is the EntityManager. So this simply means there is no API like StatelessEntityManager in JPA, and we need to find a way to achieve the same functionality with JPA APIs only, which is explained in the same section: After each page is read, the entities become detached and the persistence context is cleared, to allow the entities to be garbage collected once the page is processed.
we want to use same jpa way that we are using for our online transactions.
You can use the same DAOs or repositories for both your web app and batch app. For example, the ItemWriterAdapter lets you adapt your hibernate/JPA DAO/repository to the item writer interface and use it to persist entities.
In prod we use oracle, we have user that access to some schemas, how we can inform spring batch to use particular schema to create tables. Right now for some time same application will be use for batch and online so we dont want to use second datasource and different user for batch if possible. Isnt this very basic requirement for all?
You can use the same data source for both your web app and batch app. Then it is up to you to choose the schema for Spring Batch tables. I would recommend using the same schema so that data and meta-data are always in sync (when a Spring Batch transaction fails for example).
Hope this helps.

Related

Howto use a database create with a single big model with Entity Framwork with separate Services or Microservices

we have a big monolithic project that use EF to manipulates a database.
Now I want to start to create different services or micro-services that use a part of the big-model.
As first step read-only data may be good, but I have to reference all big-model or there is some different way ?
Any help are wellcome.
You can start with all the microservices sharing the same large DbContext, and maintaining the single shared database. This allows you to build new services without complicated refactoring of the existing application or database schema.

How to create a hibernate view of other databases?

Good Morning, I'm in a problem!
My project is multitenant, I use SPRING JPA + SPRINGBOOT + POSTGRES.
Where it contains a database manager and Each tenant has its own database.
The problem starts when inside the database of the tenant I have views of the data manager database.
For example, inside the database manager I have information to make the login of the users of a tenant. But in the database of each tenant should be able to have the information of their users. Where users within the tenant database could have more data!
What is the best way to do this? Should I create the view?
In addition, this application is very popular, so the idea is that the use of the database is quite dynamic.
Thank you in advance! any contribution will be helpful.
Yes the best solution will be to create views where you get all the data that you need and the data is already filtered for the tenant.
Edit:
You have to create the views on the database and then map it to read only Entities or just use it with SQL.
To have read only entities you can use Hibernates #Immutable annotation.
Please find more information in the Hibernate documentation: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#entity-immutability

concurrency issues in eclipselink jpa due to cache data

I am using eclipseLink JPA and i am using same db for two different EJB applications deployed in two different servers. I am getting concurrency issues due to JPA cache if other application is doing some modification on the same object. Is there any way i can manage this concurrency issues. Using native query for db operations is not an option. I am using oracle db and glassfish server. is there any solution available for this.
Let me explain why you have issues:
Imagine you have cached data for object. So the first application will work with that cached data and there are no need to this application to go to database (as it already have the value in memory). It can upgrade that cached data if you will perform another modifying query on the same entityManager. It in no way can know that the other application changed state of your data.
So isolation level won't help as there will be no read query from first application as it already have cached value. And as far as entityManager know is only the operations from that same manager. It have no way to know about another entityManager on different application.
The solution is to not to cache. Or you can configure external cache as your second level cache which you can evict from both applications (like hazelcast cache for example).

spring-cloud-starter-stream-source-jdbc Eample Application?

I am trying to run "spring-cloud-starter-stream-source-jdbc" application. As my Source is RDBMS and I want to store the RDBMS data into RDBMS sink. I would like to know any best demo application based on "spring-cloud-starter-stream-source-jdbc".
Is there support for Incremental & Full load while performing Data Stream From RDBMS Source to RDBMS Sink using "spring-cloud-starter-stream-jdbc".
Please share any reference blogs to understand "spring-cloud-starter-stream-source-jdbc" demo application.
You can use OOTB jdbc source/sink apps (with the binder of your choice rabbit, kafka). The spring-cloud-starter-stream projects are the ones you would want to use inside your application if you want to extend/build custom applications based on the jdbc starters.
For the OOTB apps, you can refer here. For instance, jdbc source app with rabbit binder can be found here

Spring data jpa - modifying query before execution

I'm working on a project that keeps some access control information in the database. We're using that access control information to filter what the user can see. The filter that we're using is based on roles that the user has. We would like to use the Repository abstraction provided by Spring Data, but we would like to be able to adjust the generated queries before they are executed. Is there a way to attach a listener or an interceptor that will be called before a query is executed? That way we can get a reference to the query object and make whatever adjustments to the query we need to before the query is executed.
What we're thinking about doing is creating our own JpaRepositoryFactoryBean so we can override SimpleJpaRepository as described here. We would override SimpleJpaRepository.getQuery to make adjustments to the query. Then for all the generated finder methods we were thinking about extending PartTreeJpaQuery and overriding the PartTreeJpaQuery$QueryPreparer. In the QueryPreparer we would override QueryPreparer.createQuery methods. We weren't sure if that was the simplest way to get access to all queries before they get executed.
We thought about adding a org.springframework.data.repository.core.support.QueryCreationListener, but it would only get executed when the query is created. I think we need something more dynamic.
I'm not sure if you already know, but Spring Data team is working on that feature for the next release, to make it possible for Spring Security Team to add the support for ACLs then.
Add infrastructure for generic query augmentation
Spring Security issue that is blocked by the previous: Spring Security / Spring Data Acl Integration
In my company, we created a JpaRepositoryFactoryBean that would create a custom Repository to allow us to add the filter for the ACLs, but we just did that for the findAll and findOne methods, so we were loosing a lot of features of Spring Data and decided to revert that change and we're still thinking in a way to do it automatically. If we find out that it'll be too difficult, I think that we'll delegate that responsibility on the clients of the repositories and wait for the support for it in Spring Security/Data.
The original question already has 1 year. Did you find a clean way to do it?