How to include multitenant tenant id in join tables in spring boot 3 project - spring

I'm working on a Spring Boot 3 project, using the new #TenantId annotation for multitenant separation within my Hibernate entities.
This is all good, and I can see all my tables include the expected tenant column until I noticed that any join tables, such as those generated by #ManyToMany relationships, do not possess the tenant column.
Does anyone know how to also have Hibernate automatically manage tenancy through join tables in addition to regular entities?

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

JHipster create new Table entity without using initial schema

I recentely started using JHipster. I am facing problem while creating new tables (other than the jhipster provided). JHipster provided their tables through 00000000000000_initial_schema.xml even though same table has entity java classes.
But now i have created new #Entity class but tables are not getting created. What changes need to do ?
One more thing to ask, how we can disable initial schema totally and generate tables from Entity classes, even for existing jhi_* tables
Thanks in advance :)

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.

Spring boot - connect hibernate with one oracle huge table

I am creating a spring boot application and I only have one table in my oracle database (one table with more than 80 fields...), so, what is the best way to implement this in my spring boot-groovy app?
Do I need to have one entity with 80 attributes??
Can I have a hibernate entity model different from the awful oracle model? (having more than one entity in my app but connected with the same huge table).
Any ideas or tools are more than welcome.

how to manage dynamic tables with hibernate - multi tenant database

I am using hibernate 3 using spring 3.5 for a SaaS application. I am expecting upto 10-15 customers , not more. I do not want to implement separate db or schema per customer as its too complicated and costly for a small enterprise like mine. I am currently using a multi-tenant strategy which works fine for a host of small features. Here is the use case where my design fails:
For reporting feature each customer will have a different table for data (because of various reasons like legacy, source of data etc). Table structure differs and so does service/controller behaviors.
I am currently planning to create separate Controllers, Services (DAOs), etc for each customer, thus mapping each of such customer tables with a separate hibernate class. But this approach is not clean and for every new customer I add (which is not that often though), I would need to add its table, and also code a hibernate entity class mapped to the new table, which is not ideal as it needs coding. Is there a way to manage/map such dynamic tables using hibernate which gets added when a new customer is added ?
Use Hibernate 4 multi-tenancy support, see the documentation here. There is support for separate databases per tenant, separate schemas per tenant and partitioning of the same table per tenant.
Is there a way to manage/map such dynamic tables using hibernate which
gets added when a new customer is added ?
I don't know if this is directly supported by Hibernate. From the manual, the supported multi-tenant options are:
schema
database
discriminator
Discriminator is mentioned but is not supported in the current release of Hibernate (version 4.2). That leaves schema and database. You mentioned in your question that neither of these are currently applicable to your setup. So unless you're willing to do some major restructuring, you'll probably need to proceed with a different approach.
Option 1:
If I were you, I'd write a view that presents the data from each tenant's table. You can add the tenant ID as a column in the view. Map the reporting class to the view with Hibernate. When you run a query against the view, set the current tenant's ID as a query parameter.
If you go this route, you won't need to add new controllers and POJOs when you add a customer. Just modify the view to also include the new customer's data and it should work.
Option 2:
Hibernate can bind native SQL query results to entities. You can have one entity that represents the data in any reporting table (this assumes that the separate per-customer tables have a similar structure).
In your reporting DAO, you'd fetch a SQL query from a properties file or specify a named SQL query based on the current tenant identifier. Note that the named query approach will only meet your needs (no recompilation of Java classes) if you have things mapped with HBM files. If your mapping is done with annotations, you'd need to rebuild the project to add a named query.

Resources