Home > OS >  Spring Boot best practices for handling Multiple Entities
Spring Boot best practices for handling Multiple Entities

Time:01-10

I have a DataBase that stores multiple tables related to each other. The tutorials I've seen on how to do implement this kind of thing in Spring Boot often make one Controller and Service per entity, for example.

What I mean to ask is: is this really necessary? I imagine this could be done with an abstract class, but wanted confirmation.

On that note, is it normal to not have a lot of stuff other than the basics in a Service or Controller application? My implementations seem terrifyingly simple in the sense of "there must be something missing. it can't be this easy".

If moderation is really against asking two questions, then please, answer the first one only, sorry for any troubles, but I believe they are simple enough questions to not warrant any trouble :)

CodePudding user response:

So actually, you are working for yourself, but in the bigger picture, most developers actually work alongside many others.

So if you get into Google or some , any development role on any company, your code will be read by others in the team, if there is any.

In such a case, which is generally the case everywhere, there is generally a development spec which employees are supposed to follow.

If you're building your own website, then you can surely go ahead with it! Maybe you won't need anyone to see your code, and maybe no one other than you will see your code.

But realise that in the normal world, people are employed to be working as teams, and not just alone, which means, you have to follow specifications for ease of work.

Anyway, your terribly simple implementation seems nice to be shown off on social media :)

CodePudding user response:

It is good if you are making different controllers and service layers for different entities. By doing this you can avoid confusion.

Example: You are making a blog application. In which you can post a story, anyone can comment on the story, tags can be added to a story. And you have to add security also, i.e only authenticated users can write a post, only the author or admin can delete and update the post, etc.

For this, you can make these Entity classes- Story, Comment, Role, Tag, User.

And you can make controllers for different operations. For authorization, login, and sign up you can make a controller like AuthorizationController. For CRUD operations related to the Story, you can make StoryController. For CRUD Operations related to comment, you can make CommentController. For managing Tags, you can make TagController.

Similarly, you can make the Service layer and DAO layer for respective Controllers.

In this, you can debug and maintain the code easily.

  • Related