Spring boot, repository without entity - java

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.

Related

how can we use join concept in mongodb using spring framework

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/

Set table name in SpringData Jpa Repository instead of in entity

I have a JPA entity, which I want to store exactly with the same fields in two different tables for different purposes.
I assumed, that there is a possibility to set which table to actually store data in Spring Data Jpa repository, but couldn't find how to do it.
Is it possible?
If not, what is the best way to achieve my goal with spring data jpa and without copying the entity class?
You may want to define an abstract class for your entity using #MappedSuperclass annotation. You can specify your field mappings in this class. Then have two separate entity classes which inherits your abstract class and each of those two can map to their own table.

Android Studio Room Persistence - Should each entity have a seperate DAO, and how does that effect the repository class?

If I have a Database(DB) containing two tables/entities (A and B),
should I make a DAO for each entity? (Ie DAO_A and DAO_B), Or do I make a DAO for the whole DB containing these two tables?
Then as regards the repository, will this be a repository for the whole Database(whole of DB) or just a repository for the database with only the relevant DAO's I want the class for (ie DAO_A and DAO_B).
(Realy what I think I am asking here is will the Database have multiple repositories or just one repository, and will each entity have to have its own DAO, or can I make a general D
I would say go for every entity has its own DAO. Why? Because you properly separate them.
Let's say you would have a DAO which contains Entity A and B. In your repository you might only need Entity A, then it would not make any sense that this DAO uses Entity B as well. If a case occurs, where you need both Entities, just use both DAO's. A further reason for seperate DAO's is that you don't know how to couple Entities properly. How are you going to decide which Entities to couple into a DAO? Yes you could decide this depending on the Repository which makes use of it, but this can lead to code duplication (two DAO's make use of the same Entity, but each of those also use a second one - which is different for each DAO).
Regarding your second question: I believe it depends on your architecture how exactly your repository should be modeled.
For example when using MVVM:
Requirement: You have an Activity just displaying a list of images fetched from somewhere.
Then your ViewModel would offer a function like getAllImageModels or something similar. Each of those ImageModels would include an image (which will be displayed). Inside this function the Repository is called either an API call to retrieve a list of images to download or a database call to retrieve the list from the database (depending on internet connection). These images must be downloaded as well. Again images could be loaded from your local cache or downloaded via the API. Then the ViewModel wraps them in the desired model required by the View and thats it.
As you can see by this simple example, the Repository on its own just performs requests. Either to the local filesystem, database or API. It could have functions like getImageListFromDb and getImageListFromAPI in it. So that the classes which make use of it just have to decide when to use what.

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.

Spring Hibernate JPA - Using many schemas

The company I am working for stores their client data in a separate database schema for each client. They indicate that this cannot be changed at this time. Is there an efficient way to pull data and update data in all schemas without configuring a connection for each schema? Everything I can find when I search seems to be talking about using one or a couple of schemas, but I need to use many (100+) simultaneously.
In any given persistence context, each JPA entity class is mapped to a specific base table. Whether and how easily you can access multiple schemas via a single DB connection is a function of your DBMS, your JDBC driver, and perhaps your particular database, but even a combination that in general supports the kind of access you would need will still not allow you to map the same entity class to multiple distinct base tables in the same persistence context.
You might be able to use the same entity classes for different clients by associating a different persistence context with each client, but that will not allow you use the same DB connection for all of them. Thus, if using the same connection were possible for you at all, it would require different entity classes per client.
Have you considered creating a new DB user and creating SYNONYMS for each of the tables in the separate database schemas ?
You could then map JPA entitys to the SYNONYM names that you have created..
Using this approach you could still use the one DB connection but with SYNONYMS to the DB tables in the other schemas...

Categories