I'm new to JPA and Hibernate. I just saw an annotation called @Transient, which can mark a field to be non-persistent in the database. However, for the sake of "separation of concerns" described in the answer of What is the use of DTO instead of Entity?, we should remove non-persistent fields from entities.

Assuming we all follow the rule of "separation of concerns" and separate database entities and view objects, will there be valid use case of @Transient? Thanks.

  • 1
    Well no, but "Assuming we don't use transient fields, does it make sense to use transient fields" is kind of self-answering. The @Transient makes sense if your entities are used elsewhere (without DTOs).
    – sfiss
    Oct 27, 2020 at 9:29
  • @sfiss Thanks for the reply. If the assumption mentioned in my question is too strong, can you please show me an example where the entities can be used without the assumption?
    – johnlinp
    Oct 28, 2020 at 1:43
  • I annotate @Transient, entity methods addressed to hide/avoid traversals. I don't need everybody accessing the collections/attributes of my entities and tightly coupling everything in the code. I also do it with attributes that hold volatile states (counters, lookups, etc)
    – Laiv
    Oct 28, 2020 at 7:30


Your Answer

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