Association mapping in microservice - microservices

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)

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.

Spring data with R2DBC(Mysql) Example for one to many association

Can I know how to do association(One to Many) in Spring data JDBC with R2DBC(Mysql).Please provide small code example or git link if possible.
For Example, I have one employee table and address table is child of employee.
One employee can have multiple addresses.
If I want to retrieve employee, I want address as well to corresponding employee as JSON.
Note: Software stack is Spring Functional Reactive, R2DBC with Mysql
Spring Data R2DBC currently does not support aggregates.
This means every entity gets mapped to just one table and can't have object references to other entities.
Therefore the correct way to model your example is to have no object reference between Empolyee and Address but have an employeeId in the Address and use that to select the desired addresses.

Mongo db entity relationship implementation using Spring data

I am learning Spring with Mongo DB and I'm feeling difficulty in learning the entity-relationship model.
Can anyone teach me how can I implement the following design?
Person collection
A person class
id
name
List of the sports object
Sport collection
A Sport class
id (Auto-generated)
sport name
while I am saving the person class which contains sports class, Sports entity should be saved in Sports collection if it is not already present and Person entity should be stored in Person collection with Sports objects Reference.
While I am retrieving Person class, associated sports class should be fetched from the corresponding collection.
I have tried with #DBRef and it is not worked for me.
It will be very helpful if anyone teaches me this scenario or giving the reference to learning this concept.
Very thanks in advance.
while I am saving the person class which contains sports class, Sports entity should be saved in Sports collection if it is not already present and Person entity should be stored in Person collection with Sports objects Reference.
In Spring-data-mongo cascade save not supported. Therefore referencing object will not be saved to the database automatically. To achieve the same you have two option.
1) First, save sports collection (if that record not found in the collection) then save the reference of sports to person collection.
2) Make you custom cascade save implementation. For reference see this.

Linq doubts with DB context

Hi I have a question that is braking my mind for some days.
I have my SQL server Database and my C# application.
In the DB I have differemt tables, let me show you a simple ex
Tables:
Person
Relationship
City
Business Rules:
The person are from a City, so the person has IdCity
A person has a relationship with other person, and about that relationship you need to save the starting date.
In other projects I already did something like that, but in this proyect this is not working for me.
When I retrieved with LinQ the information about the person, the city is not coming, and an error appears when I try "person.city.description", for ex.
I try using Include("City") in the linq query, but it didn't work. Besides that, I don't know how to manage the circular reference to the person to person relationship.
One important thing, that I think that can be the problem, is that I rename all the tables from the DataModel, for example, the table in database is called Prd_City, so I change the Name and the Entity Set Name for City in c# project. So in the included I have to use the real table name, in other case the query fail, but if I use the real name nothing happens.
using (var context = new MyContext())
{
List<Person> oPeople = (from p in context.Person.Include("Prd_City")
select p).ToList();
return oPeople ;
}
Any help will be welcome.
Thanks!
"It didn't work" is never a good description of your problem. But from the rest of your question I can infer that Person has a navigation property named "Prd_City", while you expected it to be "City". The thing is: you renamed the entities, but not the navigation properties in the entities.
My advice (for what it's worth): it seems that your work database-first. If you can, change to code-first and manually map the POCO classes to their table names, and properties to their database columns. It may be a considerable amount of work (depending on the size of your data model), but after that you will never run the risk of EF "un-renaming" your entities. Besides, the DbContext API is easier to use than ObjectContext. Currently, it's the preferred EF API.

Resources