What is JPA Auditing?

What is JPA Auditing?

When mapping Domain to RDBS table using JPA, there are fields and columns that have Domain in common.

It is typically as follows.

There are fields and columns like this, right? The fact that it exists for each domain means that the code is duplicated. This is because it is useful for maintenance to keep a record of who created the database and when. Therefore, columns like creation date and modification date are really important data.

That's why JPA offers a feature called Audit. Audit is a function that Spring Data JPA automatically populates the time in the sense of monitoring. When updating after saving the domain in the persistence context or making a query, you have to enter the time data every time, but by using audit, the time is automatically mapped and it is added to the database table. Will put it in.

practice

    1. Create a BaseTimeEntity class in the domain package. image.png
  1. Create a BaseTimeEntity as follows.


package jojoidu.boot.springboot.domain;

import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {

    @CreatedDate
    private LocalDateTime createdDate;

    @LastModifiedDate
    private LocalDateTime modifiedDate;
}

The BaseTimeEntity class becomes the parent class of all Entity classes, and ** plays the role of automatically managing createdDate and modifiedDate of Entity class. ** **

    1. Inherit BaseTimeEntity class to Entity class.

package jojoidu.boot.springboot.domain.posts;

import jojoidu.boot.springboot.domain.BaseTimeEntity;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Getter
@NoArgsConstructor
@Entity //Indicates the class linked to the table
public class Posts extends BaseTimeEntity {

    @Id //Indicates PK field
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(length = 500, nullable = false)
    private String title;

    @Column(columnDefinition = "TEXT", nullable = false)
    private String content;

    private String author;

    @Builder
    public Posts(String title, String content, String author) {
        this.title = title;
        this.content = content;
        this.author = author;
    }

    public void update(String title, String content) {
        this.title = title;
        this.content = content;
    }
}


Four. Finally, add the activation annotation to the main class so that the JPA Auditing annotation is activated.


package jojoidu.boot.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

test

Testing is essential, so I also added a test method.


@Test
    public void saveBaseTimeEntity() {
        // given
        LocalDateTime now = LocalDateTime.of(2020, 8,12,0,0,0);
        postsRepository.save(Posts.builder()
        .title("title")
        .content("content")
        .author("author")
        .build());

        // when
        List<Posts> postsList = postsRepository.findAll();

        //then
        Posts posts = postsList.get(0);

        System.out.println(">>>>>>>createdDate=" + posts.getCreatedDate() + ", modifiedDate=" + posts.getModifiedDate());

        assertThat(posts.getCreatedDate()).isAfter(now);
        assertThat(posts.getModifiedDate()).isAfter(now);
    }

result

image.png I was able to confirm that it works normally. I'm too happy to have such fun.

Recommended Posts

What is JPA Auditing?
What is Cubby
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Byte?
What is Tomcat
What is Maven Assembly?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??
[Java] What is ArrayList?
[Ruby] What is true?
What is HttpSession session = request.getSession ();
What is docker run -it?
[Memorandum] What is an error?
What is DI (Dependency Injection)
What is a wrapper class?
What is object-oriented programming? ~ Beginners ~
What is a boolean type?
What is a Ruby module?