Creating Entity CRUH on Spring JPA 2016

Database operations ordinary meaning to know the acronym CRUD Create (Create), Read (Read), Update (Modified), and Delete (Remove). These operations are commonly applied to everyday business. However, in some critical data, delete operations should not be completely done. Conducted only change so that data is not visible / active in the application, but physically still exist in the database.

JPA in general
For example, suppose there is an account entity classes are usually created by people..

At Account.java class as usual we made the usual columns. Then, suppose the Boot Spring JPA, made a Repository class that will be used to perform CRUD operations:

I'll show you later how to change the pattern into CRUD CRUD (Hide). Operation CRUH only term that means I only just hide, instead of deleting the data (un-hide).

CRUH dengan JPA

One solution to discreetly remove operation is to use a marker. Column marker is used to indicate that data has been erased. Thus, the client / application will only get data that is not marked. This solution can be achieved with minimal through Spring JPA.
In the JPA, which changed quite simply an entity class only. Diperlu things done:
  • Change the operation to remove the marking column marker. For example on line 4 SQL operation to remove deleted modified by changing the value to '1'.
  • Change the read operation to retrieve the data that the column marker has not been set. For example on line 5, sought deleted is not equal to '1'.
  • Create a column marker. For example at line 13, column added deleted.
  • On line 12 created an annotationJsonIgnore that this column does not appear in the data access via the Repository. So, this column is completely transparent.
  • Do not forget initialized the data deleted in the constructor.


Have nice day

No comments :