Article From:
spring boot + jpa The project is quickly developed and comfortable (at least in this way), from a development point of view (small companies have no database maintainers), and annotated query methods are the fastest and most convenient.

jpaDo not support dynamic SQL, to know that the strong Java is to deal with complex business logic, the background system general java development is most, do not support the dynamic SQL feel like a broken hand general helplessness.
so,I would like to ask the gods if it is possible for JPA to gracefully implement dynamic SQL in its own encapsulation layer, as is known at present:
1:Custom annotations, combined with interceptor implementation:… Advantages: fully customizing, high flexibility, and fully retaining all the interfaces provided by JPA native, such as JpaRepository, and other disadvantages:AOP uses a lot of reflection, and the performance may be low (it’s all virtual). This idea is my own writing, security, and reliability. To be honest, I’m not afraid to use it even though the demo test passes.
2:Overwriting the interface of JPA, adding a new interface to support dynamic SQL, using SimpleJpaRepository to implement some native interfaces, and supporting dynamic SQL. Advantages: Native Writing, supporting dynamic SQL, shortcomings: if SQL is not defined in the Dao layerTo destroy the MVC design pattern and put it on the Dao layer, I feel this is a tool class, and the class is still so much, how fast?. because it’s overwriting, the newly used JPA interface is also overwritten (of course, not too much), but don’t want to inherit like JPA to help me.

All I know is that these are not very elegant. I believe there must be a more elegant way to realize the great God’s advice.

Answer 0:

CriteriaQuery API can be a good dynamic query.

Answer 1:

You can’t open the URL. You mean JPA repository? It can be written directly in the interface of JpaRepository

@Query("select * from abc")
public List<Abc> selectAll();

Or create a interface – AbcRepositoryCustom, AbcRepository extends AbcRepositotyCustom, class AbcRepositoRyImpl implements AbcRepositoryCustom. The logic is written in AbcRepositoryImpl.

Answer 2:

Complex queries can be usedQueryDSL,Or spring-data-japSpecification

jpaThe function is to make your database operation more object oriented and easier to maintain. SQL readability is difficult to maintain. So complex business should be written in the corresponding domain model instead of SQL, and I don’t think dynamic SQL is elegant.

Answer 3:

It is simple to use JPA’s parsing rules directly to customize query methods in several of the JPA’s inheritance, with a complex @Query handwriting of SQL with little code, but a bit against MVC

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *