application.properties
file. It is used because Spring Boot picks up these settings at the earliest initialization stage, and some default settings should be replaced.schema.sql
and data.sql
from application resources. Also, the default is hibernate.hbm2ddl.auto=create-drop
, after which we get a pristine-free database with tables generated from entities. Why this is done is a mystery, but this auto spring.jpa.hibernate.ddl-auto=none
should be turned off by the parameter in the application.properties
file: spring.jpa.hibernate.ddl-auto=none
@Configuration @EnableTransactionManagement @EnableJpaAuditing public class AppConfig { @Bean public AuditorAware<User> auditorProvider() { return new AuditorAwareImpl(); } }
@Configuration
- tells Spring Boot that this file contains bins for setting up the application;@EnableTransactionManagement
- includes transaction support and creates the necessary bins with default settings;@EnableJpaAuditing
- includes support for auditing, but the bin for this support will still have to be written to explain spring where the user is taken from;AuditorAwareImpl
: public class AuditorAwareImpl implements AuditorAware<User> { @Autowired private CurrentUserService currentUserService; @Override public User getCurrentAuditor() { return currentUserService.getCurrentUser(); } }
CurrentUserService
is a service that will return a User object, create it a little later. @Entity @EntityListeners({AuditingEntityListener.class}) public class User extends AbstractAuditable<User, Long> { @Basic @Column private String name; public String getName() { return name; } public void setName(String data) { this.name = data; } @Version @Column private Long version; public Long getVersion() { return version; } public void setVersion(Long version) { this.version = version; } @Override public String toString() { return "User {" + "id='" + getId() + "', " + "name='" + getName() + "'} "; } }
AbstractAuditable<U, PK>
abstract class from Spring Data, where U
is the type that represents the user, and PK
is the type of the primary key. As a result of this inheritance, the following properties will already be in essence: id
, createdBy
, createdDate
, lastModifiedBy
and lastModifiedDate
.name
property, and for the version number, the version property that Spring Data will manage. Actually Spring Data will manage all fields except name
.@Entity
- @Entity
JPA that this is an entity class@EntityListeners({AuditingEntityListener.class})
- add the default listener class from Spring Data for the entity. Because of this line, there is no need for an orm.xml
file with the same setting.Foo
, instead of its name
data
property. public interface UserRepository extends CrudRepository<User, Long> { }
CrudRepository<T, ID>
, where T
is the type of the entity, ID
is the type of the main key. The rest of the implementation of the repository takes Spring Data.CurrentUserService
service, which is needed only to demonstrate the work of the audit with two different users and is designed for order and beauty. @Service public class CurrentUserService { private Long currentUserID = 1L; @Autowired private UserRepository userRepository; public User getCurrentUser() { return userRepository.findOne(currentUserID); } public void setCurrentUserToJohn() { currentUserID = 1L; } public void setCurrentUserToDoe() { currentUserID = 2L; } }
@Service
- tells Spring that this class implements an internal service.@Autowired
- creates a user repository instance using dependency injection. @ComponentScan @EnableAutoConfiguration public class App implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Autowired private FooRepository fooRepository; @Autowired private CurrentUserService currentUserService; @Override public void run(String... args) { Foo o = new Foo(); o.setData("test data"); fooRepository.save(o); fooRepository.findAll().forEach(System.out::println); currentUserService.setCurrentUserToDoe(); o.setData("New test data"); fooRepository.save(o); fooRepository.findAll().forEach(System.out::println); } }
insert into USER (ID, NAME, VERSION) values (1, 'John', 0);
Source: https://habr.com/ru/post/238487/
All Articles