This is my first day in the Gwen Challenge. Check out the details [juejin.cn/post/696719…]

SpringData JPA, which is Spring’s encapsulation of JPA.

JPA aims to significantly improve the operation of the data access layer by reducing the amount of work to the actual amount needed, according to the website. As a developer, all you need to do is write your Repository interface and include custom methods that Spring automatically provides. But because of this convenience, some rules need to be strictly followed. This article mainly tells me in the process of using some pits and need to pay attention to the use of matters, I hope to be able to help you all.

1. When using ManyToMany annotations, there is an infinite loop containing the other party’s solution: the annotation @jsonBackReference needs to be written on the set method of the object of the many party, as shown in the figure

2. Use the @query annotation to customize the writing of new SQL incoming objects

Controller, as shown

Repository layer, as shown

The specific code is as follows:

Transactional @query (value = "insert into rk_sharegroup(group_name,start_time,end_time,create_time) Values (: # {# record. GroupName}, : # {# record. StartTime}, : # {# record. The endTime}, now ()), "nativeQuery int = true) AddShareGroup (@ Param (" record ") ShareGroup record);Copy the code

When using jPA’s own save method, the entity class attributes will be fully added and edited, and null will be added and modified, but sometimes the database may set default values, and we want to set only some fields, other fields still use the default values of the database. The @dynamicInsert annotation needs to be added to the entity class

Note: It is not recommended to use jPA’s own save method to prevent unnecessary data overwriting. You can write your own custom update SQL through @query annotation

4. The JPA operation is read-only by default. If you want to delete or modify a JPA operation, you must add @modifyying. Therefore, the @Transactional annotation needs to be added in front of @Pipelineto override the default transaction isolation level

5. Avoid comments mixed use, otherwise you will quote org. Hibernate. MappingException: Could not determine the type solution: annotation or write on the field, or written in the book of the getXX, software cannot be mixed use, otherwise you will quote this mistake!