how can we use join concept in mongodb using spring framework - java

I am new to spring framework recent i have made small project on microservices, where i create two microservices
department service
User service
I need to know how can i use join in them, i have create one common field in both the service i.e departmentId,
when i use getmapping in user service containing department id fetching the data from department service in respective to that departmentId.
Using intellij, mongodb as database, spring framework,java

Since mongo is a document store type database.
It depends on how the data will be used. You'll need to think how the data will be queried, what will the response may be.
In a RDBMS, it is natural to denormalize your data and split it over several tables and use joins to create the views you need.
In a document store you do exactly the opposite you'll normalize your data and try to include as much as you can to satisfy most queries in one query.
When you use spring, you might also like to use https://spring.io/projects/spring-data-mongodb
If you want to gain in-depth knowledge on mongo, they have several courses available where they can teach you for free: https://university.mongodb.com/

Related

Spring boot, repository without entity

Is it possible to create a repository without an entity?I've been working on a project and I need to take data from different tables. So I can't create an entity because I don't have that kind of table in DB.
How can I do that, Please Help?
You need an aggregator. If you know exactly which data you do need to work with, you can create a view with columns from those different tables and define an entity class with those column names in the view. Hence you can implement the repository just as the regular way with that entity of aggregated view.
But, if you have to fetch and combine multiple entities and you actually don't know how much varieties are there in fetching data and entities, you can write a dao as service or component and access different entities through different repositories and then aggregate the data programmatically as your need. You can use native queries too to access various data from different tables.

Is there a way to get data from table without repository and entity?

We have 2 projects with the same database using JAVA Spring-boot. The main projects hold the entity and repository files. Which is different to the other project. Since we don't understand the whole system of the main project we create ours. The problem is I need to get the data from a table without using repositories function like get, save, etc. I just need to query the table. Is this possible?
Thank you.
Depends on what you want
Only simple Data
A complete Object representing the data or part of the data
For the first one I would take JDBC(https://www.javatpoint.com/java-jdbc)
For the second one I would take JPA/Hibernate.
You can create a Entity which represents only the data you need and make it read-only(How to make an Entity read-only?).
And then you can create a simple CrudRepository/JpaRepository where you fetch the data.
Here's some pointers for your help:
DB Name/Host/User/Pass should be found in one of the *.properties files.
In Repositories, the table names are usually with #Table annotation.
To know what Queries a particular repository can execute, you can check with #Query annotation. Or the methods inside the repository may donate queries, like findByStudentId means select from student where id=.
Now with the above clues, you can just write a simple JDBC connection (it really depends on what db it is in the backend), connect to the DB above, and execute the queries you may want to.

Hibernate Search Indexing a single tenant

The database I am working on has a multi-tenant design. I want to implement hibernate search on my application however I want hibernate search to index a certain tenant only. How do I achieve that?
You should be able to use dynamic sharding - http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#advanced-features-dynamic-sharding
This allows you to split (shard) the data into several Lucene indexes. Using dynamic sharding you can route the data at index and query time using a custom ShardIdentifierProvider. This requires of course that in the implementation you have access to the tenant id, for example via a ThreadLocal.

Using Spring Data JPA/MongoDB Cross Store, can a query go across both databases?

I'm using the mongodb-customer-service-data examples (available at https://github.com/SpringSource/spring-data-document-examples).
The example uses #RelatedDocument to associate domain objects across databases. It allows storing and updating objects well enough.
However, I want to do a Query that has Criteria against both the JPA database (in the example HSQLDB) and MongoDB.
Is this even possible? Is there any example anywhere that proves that it is possible?
Comments not directly related to the issue:
My use case is that our application is an Oracle DB, but we are now starting to receive semi-structured data. The JPA Entities are filterable by a user within certain constraints, but we'd like to expand this capability to the semi-structured data.
There also seems to be competing technologies QueryDSL and EclipseLink, but I can't find anything about doing queries across databases.
In JPA, it doesn't appear to be possible to do cross database queries because there are different PersistanceUnits that are associated with each database. However, mongodb cross-store seems to negate this particular problem.
I never used Spring Data JPA, but if JPA isn't capable of doing this I doubt that Spring Data will offer such capabilities.
I don't know what you understand by mongodb cross-store. So I'll explain shortly what mongodb can and can't do.
With mongodb you can't have queries across multiple collections (tables), so you can't do join with plain mongodb. So #RelatedDocument is a feature which Spring Data offers. Related documents may be either embedded (don't know if Spring Data supports this) or may be some simple DBRef to a document within a different collection (and database). With DBRef you can only query on #RelatedDocument fields like _id, collection and database, but none of the actual referenced documents property.
This said mongodb doesn't allow to search cross database in a single query, as you can't do queries across collections so you can't do this against databases either. The only cross-store functionality you have, is for sharded collections. With this you have you collection distributed across multiple nodes through a shardkey. MongoDB may need to query across all collection shards to fulfill your query. But beside this, what I wouldn't call actually a cross-database function, you have no such capabilities.
If this doesn't help, please explain in more detail what you want todo.

Filtering of data based on a DSL

Our's is a product specific to a domain.Here are some of the typical security use cases,
A normal User can edit only his profile data or anybody data based on permission
A user can see only data that belongs to his department while another user can data from all departments
These requirements changes from customer to customer so we can not just hard code the filtering logic in our application code or maintain multiple versions of the application.
We use Hibernate criteria based filtering and like to add additional Restrictions based on some expressions associated with a Permission. An expression for use case 1, mentioned above, may look like this, 'userId=user.userId' where 'user' is a keyword and represents current authenticated user and 'userId' is a property of the object for which criteria is created.
We want to define a dsl using which we can derive criteria restriction as well as 'where' clause for sql.
Are there any existing frameworks which will enable us to do it, without going for dsl? Based on the use cases mentioned above, do you think it can be achieved in different way?
Additional Info: We define only domain objects with some meta information to generate UI. Persistence, security etc are taken care by our core framework.
Have you tested Hibernate filters?
They are great mechanism for record level filtering, you can define multiple filters (for example two filters for your case) on each entity and enable or disable them according to current user.
Shall mention that filters can accept parameters (for example your current user id).
We have used this great feature of Hibernate 3 on a large project, which had a hierarchical organization unit and that worked fine.
By the way if you use Oracle as your database, consider Oracle's virtual private database feature.
We decided to go with our own dsl using antlr

Categories