In fact, this isn’t making much sense:
@Autowired
private AtividadeRepository atividadeRepository;
private AtividadeDAO atividadeRepository = new AtividadeDAO();
In fact, you wouldn’t even pass the compilation, since you are declaring two different objects with the same name within the scope of the same class.
Another thing is that you are probably making some confusion of concepts. Either you use a repository, or you use a DAO. Repositories are entities working at the collection level. Daos are entities working at the level of database technology.
Also, the difference between DAO (Data Access Object) and DAL (Data Access Layer) not just a letter: DAO is the object that is part of a DAL. Usually, both the DAO and a repository return objects of type DTO (Data Transfer Object).
Having understood this, we can move on to the @Autowired
.
@Autowired
is the annotation for Dependency injection. It indicates to the framework that the marked object will be managed by framework (its entire life cycle). It makes sense for a Service
, but it doesn’t make much sense for an entity, since usually an entity (most of the time) is an anemic object that doesn’t require life cycle management (definition of DTO).
So for this case, I think it would be interesting to remove the DAO statement.
Doesn’t make any sense
@Autowired
for entities, at least I can’t find a context that does. I already made this grotesque mistake, obviously the states of the entity was shared among all users.– Daniela Morais
@Danielamorais I think this question is a case of complete confusion of concepts :)
– Leonel Sanches da Silva