diff --git a/.gitignore b/.gitignore index 14520fb38..428f55c73 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ build.xml manifest.mf nbactions.xml nb-configuration.xml +out/ +*.iml +.idea diff --git a/HibernateSpringBootAudit/pom.xml b/HibernateSpringBootAudit/pom.xml index 7718c8b60..45f202699 100644 --- a/HibernateSpringBootAudit/pom.xml +++ b/HibernateSpringBootAudit/pom.xml @@ -14,14 +14,14 @@ org.springframework.boot spring-boot-starter-parent - 2.1.4.RELEASE + 2.2.1.RELEASE UTF-8 UTF-8 - 1.8 + 12 12 12 @@ -31,10 +31,6 @@ org.springframework.boot spring-boot-starter-data-jpa - - org.springframework.boot - spring-boot-starter-jdbc - org.springframework.boot spring-boot-starter-web diff --git a/HibernateSpringBootAudit/src/main/java/com/bookstore/MainApplication.java b/HibernateSpringBootAudit/src/main/java/com/bookstore/MainApplication.java index 0c94a135a..b2153e526 100644 --- a/HibernateSpringBootAudit/src/main/java/com/bookstore/MainApplication.java +++ b/HibernateSpringBootAudit/src/main/java/com/bookstore/MainApplication.java @@ -1,47 +1,42 @@ package com.bookstore; -import com.bookstore.auditor.AuditorAwareImpl; import com.bookstore.service.BookstoreService; -import org.springframework.boot.ApplicationRunner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; -import org.springframework.data.domain.AuditorAware; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @SpringBootApplication @EnableJpaAuditing(auditorAwareRef = "auditorAware") public class MainApplication { - - private final BookstoreService bookstoreService; - public MainApplication(BookstoreService bookstoreService) { - this.bookstoreService = bookstoreService; - } - - public static void main(String[] args) { - SpringApplication.run(MainApplication.class, args); - } + private static final Logger logger = LoggerFactory.getLogger(MainApplication.class); - @Bean - public AuditorAware auditorAware() { - return new AuditorAwareImpl(); + public static void main(String... args) throws Exception { + var ctx = SpringApplication.run(MainApplication.class, args); + + logger.info("Started ..."); + System.in.read(); + ctx.close(); } @Bean - public ApplicationRunner init() { + public CommandLineRunner scheduleRunner(BookstoreService bookstoreService) { return args -> { System.out.println("Register new author ..."); bookstoreService.registerAuthor(); Thread.sleep(5000); - + System.out.println("Update an author ..."); - bookstoreService.updateAuthor(); - + bookstoreService.updateAuthor(); + Thread.sleep(5000); System.out.println("Update books of an author ..."); - bookstoreService.updateBooks(); + bookstoreService.updateBooks(); }; } } diff --git a/HibernateSpringBootAudit/src/main/java/com/bookstore/auditor/AuditorAwareImpl.java b/HibernateSpringBootAudit/src/main/java/com/bookstore/auditor/AuditorAwareImpl.java index 9a61a1133..89288eccf 100644 --- a/HibernateSpringBootAudit/src/main/java/com/bookstore/auditor/AuditorAwareImpl.java +++ b/HibernateSpringBootAudit/src/main/java/com/bookstore/auditor/AuditorAwareImpl.java @@ -4,14 +4,15 @@ import java.util.Optional; import java.util.Random; import org.springframework.data.domain.AuditorAware; +import org.springframework.stereotype.Component; +@Component("auditorAware") public class AuditorAwareImpl implements AuditorAware { @Override public Optional getCurrentAuditor() { - // use Spring Security to retrive the currently logged-in user(s) - return Optional.of(Arrays.asList("mark1990", "adrianm", "dan555") - .get(new Random().nextInt(3))); + // use Spring Security to retrieve the currently logged-in user(s) + return Optional.of(Arrays.asList("mark1990", "adrianm", "dan555").get(new Random().nextInt(3))); } } diff --git a/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/Author.java b/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/Author.java index 7553264db..c17d0c151 100644 --- a/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/Author.java +++ b/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/Author.java @@ -1,6 +1,5 @@ package com.bookstore.entity; -import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -9,7 +8,7 @@ import javax.persistence.OneToMany; @Entity -public class Author extends BaseEntity implements Serializable { +public class Author extends BaseEntity { private static final long serialVersionUID = 1L; diff --git a/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/BaseEntity.java b/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/BaseEntity.java index 15f91b8c8..3995f2d4f 100644 --- a/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/BaseEntity.java +++ b/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/BaseEntity.java @@ -1,5 +1,6 @@ package com.bookstore.entity; +import java.io.Serializable; import java.time.LocalDateTime; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; @@ -14,7 +15,7 @@ @MappedSuperclass @EntityListeners({AuditingEntityListener.class}) -public abstract class BaseEntity { +public abstract class BaseEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/Book.java b/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/Book.java index 84a5e8f51..443ca0d05 100644 --- a/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/Book.java +++ b/HibernateSpringBootAudit/src/main/java/com/bookstore/entity/Book.java @@ -1,13 +1,12 @@ package com.bookstore.entity; -import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity -public class Book extends BaseEntity implements Serializable { +public class Book extends BaseEntity { private static final long serialVersionUID = 1L; diff --git a/HibernateSpringBootAudit/src/main/java/com/bookstore/repository/AuthorRepository.java b/HibernateSpringBootAudit/src/main/java/com/bookstore/repository/AuthorRepository.java index 827809739..f6a8733cd 100644 --- a/HibernateSpringBootAudit/src/main/java/com/bookstore/repository/AuthorRepository.java +++ b/HibernateSpringBootAudit/src/main/java/com/bookstore/repository/AuthorRepository.java @@ -7,5 +7,5 @@ @Repository public interface AuthorRepository extends JpaRepository { - public Author findByName(String name); + Author findByName(String name); } diff --git a/HibernateSpringBootAudit/src/main/java/com/bookstore/service/BookstoreService.java b/HibernateSpringBootAudit/src/main/java/com/bookstore/service/BookstoreService.java index bfeadfae0..29476b106 100644 --- a/HibernateSpringBootAudit/src/main/java/com/bookstore/service/BookstoreService.java +++ b/HibernateSpringBootAudit/src/main/java/com/bookstore/service/BookstoreService.java @@ -20,25 +20,25 @@ public BookstoreService(AuthorRepository authorRepository) { @Transactional public void registerAuthor() { - Author a1 = new Author(); + var a1 = new Author(); a1.setName("Quartis Young"); a1.setGenre("Anthology"); a1.setAge(34); - Author a2 = new Author(); + var a2 = new Author(); a2.setName("Mark Janel"); a2.setGenre("Anthology"); a2.setAge(23); - Book b1 = new Book(); + var b1 = new Book(); b1.setIsbn("001"); b1.setTitle("The Beatles Anthology"); - Book b2 = new Book(); + var b2 = new Book(); b2.setIsbn("002"); b2.setTitle("A People's Anthology"); - Book b3 = new Book(); + var b3 = new Book(); b3.setIsbn("003"); b3.setTitle("Anthology Myths"); @@ -52,14 +52,13 @@ public void registerAuthor() { @Transactional public void updateAuthor() { - Author author = authorRepository.findByName("Mark Janel"); - + var author = authorRepository.findByName("Mark Janel"); author.setAge(45); } @Transactional public void updateBooks() { - Author author = authorRepository.findByName("Quartis Young"); + var author = authorRepository.findByName("Quartis Young"); List books = author.getBooks(); for (Book book : books) { diff --git a/HibernateSpringBootAudit/src/main/java/com/bookstore/service/UserService.java b/HibernateSpringBootAudit/src/main/java/com/bookstore/service/UserService.java index 887544feb..936835a37 100644 --- a/HibernateSpringBootAudit/src/main/java/com/bookstore/service/UserService.java +++ b/HibernateSpringBootAudit/src/main/java/com/bookstore/service/UserService.java @@ -8,8 +8,7 @@ public class UserService { public String getCurrentUserName() { - // use Spring Security to retrive the current user - return Arrays.asList("mark1990", "adrianm", "dan555") - .get(new Random().nextInt(3)); + // use Spring Security to retrieve the current user + return Arrays.asList("mark1990", "adrianm", "dan555").get(new Random().nextInt(3)); } } diff --git a/HibernateSpringBootAudit/src/main/resources/application.properties b/HibernateSpringBootAudit/src/main/resources/application.properties deleted file mode 100644 index fae721052..000000000 --- a/HibernateSpringBootAudit/src/main/resources/application.properties +++ /dev/null @@ -1,12 +0,0 @@ -spring.datasource.url=jdbc:mysql://localhost:3306/bookstoredb?createDatabaseIfNotExist=true&useLegacyDatetimeCode=false -spring.datasource.username=root -spring.datasource.password=root - -spring.jpa.hibernate.ddl-auto=create -spring.jpa.show-sql=true - -spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect - -spring.jpa.open-in-view=false - -spring.jpa.properties.hibernate.jdbc.time_zone=UTC diff --git a/HibernateSpringBootAudit/src/main/resources/application.yml b/HibernateSpringBootAudit/src/main/resources/application.yml new file mode 100644 index 000000000..96e1d0023 --- /dev/null +++ b/HibernateSpringBootAudit/src/main/resources/application.yml @@ -0,0 +1,23 @@ +spring: + datasource: + url: jdbc:mysql://localhost:3306/bookstoredb?createDatabaseIfNotExist=true&useLegacyDatetimeCode=false + username: root + password: root + + jpa: + hibernate.ddl-auto: create-drop + show-sql: true + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL5Dialect + jdbc: + time_zone: UTC + open-in-view: false + +logging: + pattern: + console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" + level: + root: INFO + org.springframework: DEBUG + com.apress.cems.reactive: DEBUG \ No newline at end of file