-1

I have several modules in my Spring Boot application (broken with DDD in mind), which would sometimes need to depend on another module not because of logic, but because the entities are related. For example, User is an entity needed in other modules, now I do not want my modules to be coupled except to core module.

I have experience in Django and Django solved this issue by, for example, using an interface function called get_user_model (which the same logic could be applied for other models). Now in Spring, if it was a service or some other class, we could use DI, but I could not figure out how DI would be applied here and do not find anything else to decouple the use of entities and their repositories (as I use hibernate with JPA).

user module:

@Entity
public class UserEntity implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    private String username;

    private String password;

    private String roles;

    private boolean isActive;

    private boolean isStaff;

    private boolean isSuperUser;
}
 

notification module:

@Entity
@Table(name = "notification")
public class NotificationEntity {
    @Id
    @GeneratedValue
    @Setter
    @Getter
    private Long id;

    @ManyToOne()
    @JoinColumn(name = "username")
    private UserEntity user; // want this to not reference UserEntity directly, to decouple this module from user module

}

Is there a way to reference other entities and yet maintain decoupling?

1
  • 2
    "User is an entity needed in other modules, now I do not want my modules to be coupled" - this is contradictory Commented Aug 21, 2024 at 11:46

2 Answers 2

0

To use an entity in another module without creating a direct dependency in Spring Boot, you can:

Use a Foreign Key: Instead of referencing the entity directly, store the id or other unique field (e.g., user_id) in the entity of the other module. Handle the association at the service layer.

Use DTO or Interface: Create a lightweight DTO or interface to represent the necessary data from the entity. The entity itself is not referenced, only the needed fields.

Custom Representation: Create a custom class (e.g., User) in the other module that mirrors the needed fields without referencing the actual entity. Store only these fields in your entity.

This way, you can maintain modularity and avoid tight coupling between modules.

Sign up to request clarification or add additional context in comments.

Comments

0

The best way for me is to use an interface representing the entity useful information. SpringBoot will then help you to inject the implementation easily.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.