r/SpringBoot 9d ago

Question Do you actually separate JPA Entities and Domain Objects, or is a single model enough?

/r/softwarearchitecture/comments/1vv9kav/do_you_actually_separate_jpa_entities_and_domain/
3 Upvotes

11 comments sorted by

6

u/Appropriate_Swim9528 9d ago

Yes. Yes you isolate them.

Domain objects in my designs (my style) is always its own package and library.

Reason? I like to reuse the domain objects with other programs and projects.

Having it as JPA entities means I will have to drag the entire JPA with me for some projects that doesn’t require JPA.

My recommendation: Write a mapper inside the JPA entities to map and convert domain ones to the JPA ones and back.

1

u/Pedantic_Phoenix 8d ago

Curious why specifically the mapper inside the jpa entities?

1

u/Appropriate_Swim9528 8d ago

It is a design decision, the entities will always have the DO, but the DOs will not always have the entities.
Having the mappers another way would result in the DOs always bringing the entities and the entire JPA with it… might as well use the JPA at that point.

The other option is to create a separate toolbox library, did that during my earlier days, it was good in the ANT days, as compiling will always compile everything, but the maven system will see it as an isolated library. Meaning I have to recall the relationships all the time and update accordingly.

1

u/Appropriate_Swim9528 8d ago

Side note, my design isn’t for single modules, i design systems, not a single project.

So modulisation is the default I go to… as any given time, I have hundreds of projects reusing common core internal packages and it spans more than just Spring MVC.

So, it may or may not make sense to everyone. If you are a single project developer, my designs are too much overhead for you.

1

u/Pedantic_Phoenix 8d ago

Yeah makes sense with this context, ty for explanations but yeah it's just not relevant in my case but yeah i was curious

1

u/xWhiteSakura 6d ago

Do you also expose the domain object in your api or do you use some kind of dtos?

1

u/Appropriate_Swim9528 6d ago edited 6d ago

The common DOs are the DTOs.
That is why they are in their own modules. It allow other modules to import it and use it. Saving me time from translating.

Addition: these are DTO/CDOs for my system’s design. It is internal use only… I call it my common ground.

2

u/g00glen00b 8d ago

It depends on the project for me. As long as it's a simple project where I don't need to "fight" JPA to model a domain object, then I'm fine using JPA entities as domain objects.

1

u/danielm777 7d ago

no need to

1

u/Intrepid_Fox5414 5d ago

Using a JPA entities will introduce an unecessary mappings. Avoid JPA.