0

I have the @Controller, @Service and @Repository classes. The application works fine, but I think I'm not using the annotations properly for the "entity" and "repository" classes.

I'm actually not using a db(not even an in-memory db) and don't intend to.

I'm currently annotating the repository with @Repository and the entities with @Service and this is my concern: am I doing this correctly?

How should I design and use the Spring annotations to wire the entity and repository classes to the service if I don't want to persist the data?

Currently it looks like this:

Service class

@Service
public class ServiceClass{
    @Autowired
    RepositoryClass repositoryClass;
    
    public ServiceClass(RepositoryClass repositoryClass) {
        this.repositoryClass = repositoryClass;
    }
}

Repository class

@Repository
public class RepositoryClass{
    @Autowired
    private Entity entity;

    public DocumentRepository(Entity entity) {
        this.entity = entity;
    }
}

Entity class

    @Service
    public class Entity {
       private Map<String, List<Integer>> entityMap;

       public Entity (Map<String, List<Integer>> entityMap) {
           this.entityMap = entityMap;
       }
    }
1
  • 1
    There is no need to autowire Entity in your Repo. You just need some concurrent collection as class member in there, e.g.: private final Map<String, Entity> entities = new ConcurrentHashMap<>(); And add functions for CRUD-functionalities you need.
    – H3AR7B3A7
    Commented Sep 5, 2021 at 23:36

3 Answers 3

2

Annotating an entity class with @Service is wrong.

A class annotated with @Service is usually stateless, and for that reason, there is usually only one object of such a class.

A class annotated with @Entity is usually stateful, and for that reason, there are usually many objects of such a class.

An example scenario is a simple news service:

  • There is one NewsService that contains interesting code to fetch news from the repository.
  • For each news item, there is a NewsEntity object, holding the data of the individual news item.
2
  • Considering this case that I'm not going to persist the data, doesn't make sense to use annotation Entity, right? Considering the scenario above, if I mark the entity class with Entity annotation, the application won't start because RepositoryClass won't find a bean of type class Entity. How should I proceed?
    – jimmy
    Commented Sep 5, 2021 at 23:12
  • It is generally wrong to annotate an entity with @Autowired. As I said in my answer, there can be many entity objects, and @Autowired only works for classes that have very few objects, in most cases exactly 1. Just remove that annotation, and the whole field as well. It doesn't make sense that the repository has exactly one entity. Commented Sep 6, 2021 at 17:52
1

The question is: what's the role of each class here. Usually a repository is the point to access data. An entity is a data object, not a logic component, so it is usually created and managed by the repository, in this example, not Spring.

It's hard to firmly say anything with only that code (no information about how every component is used), but I would remove the @Service from the Entity class. The other classes are ok with those annotations.

2
  • I agree that entity classes shouldn't have @Service. But if I do so, the application won't start because it's autowired in the repository. How to design the app in this case? Instead of a db, I just want to store the data into a HashMap(or any other data structure).
    – jimmy
    Commented Sep 5, 2021 at 23:17
  • 1
    Then, just have that Map/List in your Repository, and maybe add methods like add, list, delete, update... To modify that Map/List. Instead of autowiring, simply modify that list of entities from the Repository. Something like List<Entity> for example
    – Iván
    Commented Sep 5, 2021 at 23:21
1

You can't use @Entity from data-jpa w/o setting up some db, so your entities don't need any annotation. They aren't beans that you need to wire in anywhere.

But the general idea that 'when you don't know' it's probably a service is a good one. XD

The other annotations are right.

You can basically annotate them with @Component, @Service, @Config, @Repository ... It wouldn't break your code, the names are mostly just to be more clear for the people working on the code.

Not the answer you're looking for? Browse other questions tagged or ask your own question.