OneToMany and ManyToOne mapping in spring boot - spring

I need to create a group with multiple users (which are taken from user table) and single employee for every group in spring boot using rest api.
It means I want to perform OneToMany mapping between group to users and ManyToOne mapping between group and employee.
In fact 1 employee can handle more than one group, and all these data I want in a single table.

Related

How can I update database column values in Spring Boot as soon as the application starts?

I am developing a simple Spring Boot application to handle REST API requests using Postman. This is my project structure.
In my DiningReview entity, there are 4 attributes of concern, namely peanutScore, eggScore, dairyScore and restaurantId. The Restaurant entity also contains the same 4 attributes as DiningReview (the restaurantId in Restaurant is the primary key), but they are supposed store the average values of the respective attributes in DiningReview. I have not implemented any mapping relations in my entities.
For more clarification on the entities, here is the Restaurant entity and here is the DiningReview entity.
I want to implement the following logic in my code -
After the application starts, the scores in Restaurant should be updated to hold the average scores in the DiningReview entity.
I have created queries to access and calculate the average of the scores in the DiningReview entity, but I am unsure as to how I can save those averages in the respective columns in Restaurant table. I am using the H2 database engine for my database.

Spring Boot JPA - Entities

Hopefully, you can help me with the following doubts that I have.
So I have an entity called User that has some columns like Nationality, Gender, Professional Status. And I have an entity for each column referred.
I'm looking for the best way to define this.
Should I apply a ManyToMany relationship between those tables? Since Users can have the same Nationality and Gender?
When I try to implement this, Spring is creating a third table automatically and I don't understand the need.
Having the following shouldn't be enough?:
Table User:
id,
name,
id_nationality.
id_gender,
id_professional_status
Table Nationality:
id,
nationality
(etc)
Why Spring is creating another table called user_nationality?
Thanks in advance.
Whenever we are using the ManyToMany relationship in that case a separate table will be created containing both primary keys of the tables involved in the new relationship.

Association mapping in microservice

I have two Microservices. Let's say student & library.
student & library - eureka clients
The microservices have a common DB
The scenario is - A student can issue (or take) multiple books from the library.
In Monolithic Application:- This will be implemented by anotating#OneTOMany on List of LibraryBook in StudentEntity and adding one table where we will store each book id for student.
Currently what i have tried
I have loose mapping (or binding ) of data between the data. I have created a table in DB with two columns studentId and bookId and when a student takes a book from library I add the bookId in the table and delete the record when book is returned.
For saving -
StudentBook studentBook = new StudentBook();
studentBook.setStudentId(123);
studentBook.setBookId(456);
studentBookRepository.save(studentBook); // studentBookRepository extends JPA Repository
For Deleting-
studentBookRepository.deleteByBookIdAndStudentId(456,123);
The problem in this implementation is that I always have to verify the bookId before saving the records.
Is there any better way to implement this?
How to implement #OneToMany relationship in Microservices?
(Or can we even implement Association mapping in microservices)
(Or can we even implement Association mapping in microservices)

How to establish one to many relationship in Dynamo DB?

I have 2 different JSON Files. One with user details and other with order details. Order details table has a column user_id to match the user ordered for. I have to create a dynamo db table that has the order details nested inside the user details and insert the values from the Json files into this table using a spring-boot app. Can someone help me with this ? Do we have any example code ?
DynamoDB is NOT a relational DB so you can't have relations per se.
However, you have two ways (at least those come to my mind) to achieve what you want.
1) Have two tables: User and Order, the latter with userId field. When you load Order, get your userId and load also a User by the index id.
2) In your User.java you can have field List<Order> orders. Then you need to create Order.java and annotate this class with #DynamoDBDocument. This enables you to have custom objects in your #DynamoDBTable classes. Do not also forget about getters and setters since they are required.

Fetching list of records based on list of Primary Key using Spring Data JPA

I need to fetch multiple records by querying on multiple Primary Keys. For instance, fetching multiple Employee records from Oracle DB
having fields as EmployeeId, EmployeeName based on multiple Primary key employee_id = {1,2,3} which will result in 3 Employee records, all within a single DB session.
I can do it one by one :
Optional<EmployeeBean> eb = employeeRepo.findByEmployeeId(Id);
Here employeeRepo extends CrudRepository<Employee, Long>
Is it possible to do batch wise record fetch with list of Primary Keys in Spring 5?
Use findByEmployeeIdIn(List<Long> ids). Here is a list of keywords you can use with spring data jpa.

Resources