How to get limited random results using Spring Boot Repositories - spring-boot

I have a database with many fornames and names packed in "Person" Entity. Also I use Spring Boot with #Repository to access the data.
I want to achieve something like that.
give random 5 results.
Is there an easy way to achieve it with Repository Annotations? Without writing native query?

Related

What is the best way to maintain queries in Spring boot application?

In My Application, Using the below technologies
Spring boot 2.7.x
Cassandra
spring batch 5. x
java 11
As part of this, I need to extract data from the Cassandra database and need to write out the file
so here I need to use queries to fetch data so
just want to know what is the best way to maintain all queries at one place so any query changes come in the future, I shouldn't build the app rather just need to modify the query.
Using a repository class is necessary. If you are using JPA i recommend using a repository for each Entity class. With JDBC it is possible to create a single repository which contains all the queries. To access the query methodes i would use a service class. In this way your code is structured well and maintainable for future changes.

How to call database on multiple environments using Springboot JPA

I am new to Springboot and trying to build a small rest-service. We have a DB deployed on different environments (e.g. DEV, TEST). The rest-service will make a call to the appropriate database based on the received query param (e.g. ?env=TEST). The schemas of the deployed database are the same, the difference is only in connection string. I have some questions related to this task.
I read a few articles how to work with multiple databases using Spring JPA (for example this one: https://www.baeldung.com/spring-data-jpa-multiple-databases). It did work, but in the given example they get different entites from different databases using different queries, in my case the entity and the query is the same, but I still have to duplicate repositories, transactionManagers, entityManagers etc because of different datasources. And this is just two environments and I have more of them.
I have another thought that I might need to recreate the repository each time I process a request (to make the repository non-singleton). I am not sure if it is a good practice.
Maybe it worth to use JDBCTemplate instead of Spring JPA in this case?
Could you please suggest something how to approach such a task?

Spring Data MongoDB vs. native MongoDB

I'm new to MongoDB and I need food for thought.
My project is a ddd-/clean architecture-like project, so i want my domain framework free.
Now I have repositories in my domain with some methods like loadAggregate(...) saveAggregate(...) etc.
And additionally there are query repositories with other data structure to read the data.
Now I need to implement that repositories and I want to use MongoDB.
But I don't know, what is the best strategy.
My Rest-Framework is Spring, so I can use String Data MongoDB. But how can I connect my simple repositories with the spring repositories?
Or I can use MongoDB natively or with KMongo...
Which choice is the best solution to start with?
I hope, my trouble became clear.
Big thx!

Should repositories in Spring Boot applications be tested directly?

Not sure if this will be considered a "legitimate question" or "purely opinion based", but is there a "best practice" with regards to directly testing a repository in a Spring Boot application? Or, should any integration testing simply target the associated service?
The reasoning for the question is simply the fact that, for the most part, a repository in a Spring Boot application contains no project-generated code. At best, it contains project-defined method signatures which Spring generates implementations for (assuming correct naming conventions).
Thanks...
If you can mess it up, you should test it. Here the opportunities to mess up can include:
Custom Queries (using #Query) might be wrong (there can be all kinds of logic mistakes or typos writing a query with no compile-time checking)
Repository methods where the query is derived from the method name might not be what you intended.
Arguments passed in, the type on the parameter list might not match the type needed in the query (nothing enforces this at compile time).
In all these cases you're not testing Spring Data JPA, you're testing the functionality you are implementing using Spring Data JPA.
Cases of using provided methods out of the box, like findOne, findAll, save, etc., where your fingerprints are not on it, don't need testing.
It's easy to test this stuff, and better to find the bugs earlier than later.
Yes, I think is a good pratice to do that. You could use #DataJpaTest annotation, it starts a in memory database. The official documentation says:
You can use the #DataJpaTest annotation to test JPA applications. By default, it configures an in-memory embedded database, scans for #Entity classes, and configures Spring Data JPA repositories. Regular #Component beans are not loaded into the ApplicationContext.
Link to the docs: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
Starting from the idea that repositories should be used only inside services and services are used to interact with the other layers of the system, I would say that testing services should be enough in the majority of cases.
I would not test standard repository methods like findAll, or findBy.., they were tested already and the purpose is not to test JPA, but rather the application.
The only repository methods that should have direct tests are the ones with custom queries. These queries may be located in a shared library and it is not efficient to write similar tests across different projects(in this case regression is a big concern)

Spring-Boot Custom Repository

I have been reading about Spring Boot custom repository. I have dozens of blogs explaining how to implement those but none of them explained scenario when we actually need it?
I mean one example where we cannot live without custom repo. I mean if there is case of complex query, we can anyhow achieve it using #Query.
Please explain.
Lets say I want strongly typed query instead of #Query. I would create a custom repo, autowire EntityManager and use QueryDSL with it so I can use strongly typed references.
You can use it to extend the repository with other libraries that aren't part of Spring.
I find them useful when working with a program generator like jHipster. They keep your code separate from the generated code.
The xxxRepositoryCustom.java xxxRepositoryImpl will not be overwritten when the entities are re-generated by a dumb programmer (me.) The queries themselves have some complex logic that can not be expressed in a simple #Query

Resources