Do JPA entities and DTOs belong to Service or Spring Repository layer
<p>I want to write a JPA application, but I have problems understanding the repository and service concepts:</p>

<p>Consider we have 3 entities:
A, B and C.</p>

<p>Entity A needs to have B and C set: A.setB(B), A.setC(C) before it is saved.</p>

<p>Also for read, I want to return only DTOs since they will free me from <a href="https://vladmihalcea.com/the-best-way-to-handle-the-lazyinitializationexception/" rel="nofollow noreferrer">LazyInitializationException</a>, <a href="https://vladmihalcea.com/the-open-session-in-view-anti-pattern/" rel="nofollow noreferrer">Open Session In View Anti-Pattern</a>, etc, and for modifying data I will use entities ( these guidelines are also described in this book <a href="https://vladmihalcea.com/books/high-performance-java-persistence/" rel="nofollow noreferrer">High-Performance Java Persistence</a>).</p>

<p>First of all, the user will make a HTTP POST with some data which will be converted into ABCGuiObject. The REST controller will call serviceA.save(ABCGuiObject).</p>

<p><strong>Persist options:</strong></p>

<p><em>Option #1</em>
- The service will create 3 objects A, B ,C and pass them to repositoryA.save(A,B,C). The repositoryA will do inside a.setB and a.setC.</p>

<p><em>Option #2</em>
- The service will create 3 objects A, B ,C, do a.setB and a.setC and call repositoryA.save(A).</p>

<p><strong>DTO retrieval</strong> </p>

<p>I would like to write a JPQL query to get some DTO. I do not want to convert entities to DTO, but retrieve them directly with JPQL.</p>

<p><em>Option #1</em>
- repositoryA will return directly a DTO.</p>

<p><em>Option #2</em>
- the service will pass a JPQL query to the repositoryA, which will have a general query method which returns a DTO.</p>

<p>What approaches do you recommend? </p>