Preload data in spring boot?

0

I am starting with a project and I want to start inserting the application insert some tables, I would do it in the BD but I have attributes "created_At" and "update_At" :

package com.example.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

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

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
        value={"cratedAt","updateAt"},
        allowGetters=true

        )

public abstract class AuditModel {
@Temporal(TemporalType.TIMESTAMP)
@Column(name="created_at",nullable=false,updatable=false)
@CreatedDate()
private Date createdAt;

@Temporal(TemporalType.TIMESTAMP)
@Column(name="update_at",nullable=false)
@LastModifiedDate
private Date updateAt;

public Date getCreatedAt() {
    return createdAt;
}

public void setCreatedAt(Date createdAt) {
    this.createdAt = createdAt;
}

public Date getUpdateAt() {
    return updateAt;
}

public void setUpdateAt(Date updateAt) {
    this.updateAt = updateAt;
}

}

which I inherit from all my entities. spring boot handles the insertions to these attributes, some way to perform the pre-insertion?

    
asked by liryco 25.11.2018 в 17:51
source

1 answer

1

You can try implementing the ApplicationRunner interface, everything you put in the run method will be executed when you initialize the spring context. link

@Configuration
public class OnBoot implements ApplicationRunner{

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //tu lógica
    }
}
    
answered by 23.01.2019 в 23:13