Java persistence API when switching to mongoDB? - java

I am using Spring JPA with hibernate as implementation . DB vendor is oracle.
I am doing POC how can migrate existing platform to Mongo DB. Some related questions to that ?
Can I continue to use JPA API when switching to Mongo DB?
If Yes to point 1, Can I continue to use hibernate and just migrate DB oracle to Mongo DB or is there any better JPA implementation for Mongo DB ?
At present hibernate takes care saving objects in their respected tables/relation with proper connection. For example :- user object has 1 to many
mapping to address object as user can have multiple address. Now once i save user entity, hibernate first save user object in user table. Get userId
and save it address entity. Now persist address entity in address table
Similarly in mongoDB , I believe there must be API's which must be doing this stuff(under collection instead of table) out of shelf where I don't have to do manually like in JDBC era?
I know mongoDB does not support ACID properties. But then how will I handle atomicity here ?Say user document is saved but some error occurred while saving address object. How as application developer I can revert the entry under user table also ?

Related

Retrieving table metadata via Hibernate

Is there a simple way to retrieve table metadata (e.g. primary key and foreign keys) from a relational database, via Hibernate?
There are numerous XInformation interfaces in the package org.hibernate.tool.schema.extract.spi (e.g., TableInformation, PrimaryKeyInformation, ForeignKeyInformation), as well as an InformationExtractor interface, but no documented ways of how to instantiate them from a database session.
Using plain vanilla JSBC allows the retrieval of the metadata via
DatabaseMetaData metadata = connection.getMetaData();
Apparently the corresponding mechanism in Hibernate 5 is to register an org.hibernate.integrator.spi.Integrator object in the properties used to create SessionFactory objects to connect to the database, as discussed in this questiοn:
How to discover fully qualified table column from Hibernate MetadataSources
Is there any way of getting the above information as in JDBC, i.e. without modifying the session configuration?

Can we use Spring Data JPA and JDBC together in same Spring Boot project?

I have 1 table FEE and created a FeeRepository for database operations on this table.
Also, I need some data from another table 'PAYMENTS'.
'FEE' and 'PAYMENTS' are in the same DB schema.
I plan to query PAYMENTS table using JDBC now.
Intention is for better code readability.
Fee Queries will be at FeeRepository.java
Payments Query will be at PaymentsDao.java
Would it be a good decision to use Spring JPA and JDBC together in same project ?

Multi-Tenant with shared database and discriminator column

Hibernate Multi-tenant Shared Database (discriminator column) until now still not implemented.
I'm working on a Quarkus RestEasy JAX-RS endpoint.
I want to use a Multi-Tenant Architecture (not separate database or separate schema but shared database with discriminator column like tenantId). Quarkus also only support separate database and separate schema like Hibernate.
So, how can I implement a custom resolver to write my endpoint resources methods (get, post, put, delete) according to a tenantId?
I thought about an interceptor or filter, but I have no idea how to implement it with Quarkus.
The idea is (SaaS):
Create customer account (set him a tenant ID)
When a user login, data are fetched according to his tenantId.
The use case:
Tenant1 sends HTTP request containing his tenantId, from endpoint resources, get the tenantId and implement save(), getAll(), get(), delete() methods according to the current tenantId value, and so every tenant will only get or add his own data.
Other possibility:
Is it possible to use a separate schema and write a service (manager dashboard) which can pickup data from all tenants schemas?
Example:
Tenant1 = StackOverflow US (number of employees = 50) (employee table in SCHEMA 1)
Tenant2 = StackOverflow France (number of employees = 30) (employee table in SCHEMA 2)
StackOverFlow Global Manager Dashboard (Total employees = 80, sum of Schema 1 employee table and Schema 2 employee table) and he can also have statistics by subsidiary.
Quarkus currently only support separate database and separate schema for multi-tenancy, see https://quarkus.io/guides/hibernate-orm#multitenancy
What you want is Partitioned (discriminator) data, see https://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html/ch16.html#d5e4808
So, if you can use separate schema, it's doable OOTB with Quarkus.
For your global dashboard, you can for example create a view on a dedicated schema that is the union of the corresponding table on "by tenant" schema.

Get data through Hibernate without Entity classes

We use Hibernate and annotations to map our db and entities. But for some data tables I don't want entity classes (Because these table names and all are keep changing) so that the application will be more dynamic
So is it possible using hibernate to load data from a table without a entity class?
If so how?
Hibernate provides a way to execute SQL query and to map it to an entity or any class : native sql queries.
Use plain JDBC. I'm not sure what you mean by "table names and all are keep changing" but it sounds like a bad idea to me.
What you could do is create the sql query using string concatenation then use plain JDBC to execute it. That way you can keep table names dynamic.
If Persistence class won't be used, then the data encapsulation won't occur thus data can be accessed directly.
Hibernate Queries interact with the POJO class to fetch data.
Query, Criteria, HQL all the classes use the POJO for fetching data.
Hibernate Framework was mainly designed for the ORM Mapping.
Thus without POJO class, not possible to interact with the database.
Thus using JDBC connection would be the option left.
Use Dynamic models introduced in Hibernate 5 version - 5.4.0.Final
Hibernate Dynamic Models
To achieve this you will need HBM files created.
Session s = openSession();
Transaction tx = s.beginTransaction();
Session s = openSession();
// Create a customer
Map david = new HashMap();
david.put("name", "David");
// Create an organization
Map foobar = new HashMap();
foobar.put("name", "Foobar Inc.");
// Link both
david.put("organization", foobar);
// Save both
s.save("Customer", david);
s.save("Organization", foobar);
tx.commit();
s.close();
Here Customer & Organization are table names
Organization is Parent of Customer.
Click on the above link for more details

How shall I generate an id for each record in a database table?

I am following https://www.javatpoint.com/crud-in-servlet
to create an CRUD application in servlets and mysql.
Each user inputs his information in a webpage and submit to the web server, which then invokes a servlet SaveServlet to save the information as a record in a database table. The database table however has an additional "id".
SaveServlet.java doesn't create an id for each record. So I was wondering how to create an id for each record?
Thanks.
If you are using jpa/hibernate then your entity must be having the #Id annotation on the record id field (you dont need to set it, it will be done automatically based on you Id generation mechanism defined in the entity class and database schema).
if you are using plain jdbc for persisting records then you need to check the database how primary key is defined. for oracle you can your sequence.nextvalue to set the primary key.

Categories