### Spring Boot Offset Pagination Repository Calls Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/HibernateSpringBootOffsetPagination/README.md Examples of calling the built-in `findAll(Pageable)` method with and without sorting, and using Spring Data query creation for custom methods. ```java repository.findAll(PageRequest.of(page, size)); ``` ```java repository.findAll(PageRequest.of(page, size, new Sort(Sort.Direction.ASC, "name"))); ``` ```java Page findByName(String name, Pageable pageable); ``` ```java Page queryFirst10ByName(String name, Pageable pageable); ``` -------------------------------- ### Fetch Player Data with JavaScript Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/SpringBootPayaraMySqlKickoffApplication/src/main/webapp/index.html Use this function to send a GET request to retrieve player data. The server endpoint '/players/fetch' should be set up to handle player retrieval. ```javascript function fetchPlayer() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState === 4 && this.status === 200) { alert(this.responseText); } }; xhttp.open("GET", "/players/fetch", true); xhttp.setRequestHeader("Content-type", "application/json; charset=utf-8"); xhttp.send(); } ``` -------------------------------- ### Batch Inserts using saveAll in Spring Boot JPA Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/README.md This example demonstrates how to perform batch inserts efficiently using the `saveAll(Iterable entities)` method provided by `SimpleJpaRepository` in a Spring Boot application with MySQL. ```java List users = new ArrayList<>(); for (int i = 0; i < 10000; i++) { users.add(new User("User " + i)); } userRepository.saveAll(users); ``` -------------------------------- ### Setup Spring Data JPA Auditing Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/README.md Implement auditing to maintain history records and track user activities. Requires an abstract base entity with auditing annotations and an AuditorAware implementation. ```java abstract class BaseEntity { @CreatedDate @Column(nullable = false) @JsonIgnore protected LocalDateTime created; @LastModifiedDate @Column(nullable = false) @JsonIgnore protected LocalDateTime lastModified; @CreatedBy @Column(updatable = false) @JsonIgnore protected U createdBy; @LastModifiedBy @Column(nullable = false) @JsonIgnore protected U lastModifiedBy; } @Configuration @EnableJpaAuditing(auditorAwareRef = "auditorProvider") class AuditingConfig { @Bean public AuditorProvider auditorProvider(SpringSecurityAuditorAware auditorAware) { return auditorProvider; } } @Component class SpringSecurityAuditorAware implements AuditorAware { @Override public Optional getCurrentAuditor() { // Use Spring Security to return the currently logged-in user // Example: return Optional.of(SecurityContextHolder.getContext().getAuthentication().getName()); return null; // Placeholder } } @MappedSuperclass @EntityListeners({AuditingEntityListener.class}) abstract class BaseEntity { @CreatedDate @Column(nullable = false) @JsonIgnore protected LocalDateTime created; @LastModifiedDate @Column(nullable = false) @JsonIgnore protected LocalDateTime lastModified; @CreatedBy @Column(updatable = false) @JsonIgnore protected U createdBy; @LastModifiedBy @Column(nullable = false) @JsonIgnore protected U lastModifiedBy; } @Configuration @EnableJpaAuditing(auditorAwareRef = "auditorProvider") class AuditingConfig { @Bean public AuditorProvider auditorProvider(SpringSecurityAuditorAware auditorAware) { return auditorProvider; } } @Component class SpringSecurityAuditorAware implements AuditorAware { @Override public Optional getCurrentAuditor() { // Use Spring Security to return the currently logged-in user // Example: return Optional.of(SecurityContextHolder.getContext().getAuthentication().getName()); return null; // Placeholder } } ``` -------------------------------- ### Hibernate Envers Auditing Setup Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/README.md Configure Hibernate Envers for auditing by annotating entities with `@Audited`. Optionally, rename audit tables using `@AuditTable`. ```java @Audited @Entity class Book { // ... } @AuditTable("book_audits") @Audited @Entity class BookAudit { // ... } ``` -------------------------------- ### Build Project and Docker Image Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/Java EE/CMPCandCMT/README.md Use Maven to clean and package the project, then build a Docker image for the application. ```bash mvn clean package && docker build -t com.sample/quickstart . ``` -------------------------------- ### Create New Player with JavaScript Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/SpringBootPayaraMySqlKickoffApplication/src/main/webapp/index.html Use this function to send a POST request to create a new player. Ensure the server endpoint '/players/new' is correctly configured. ```javascript function newPlayer() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState === 4 && this.status === 200) { alert(this.responseText); } }; xhttp.open("POST", "/players/new", true); xhttp.setRequestHeader("Content-type", "application/json; charset=utf-8"); xhttp.send(); } ``` -------------------------------- ### Author Specification Example Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/HibernateSpringBootSliceAllCriteriaBuilderSortAndSpecificationAndQueryHints/README.md Defines a Spring Data `Specification` to filter `Author` entities by age greater than 45. This can be used with repository methods that accept `Specification`. ```java public static Specification isAgeGt45() { // ... } ``` -------------------------------- ### Fetch Slice with Specification, Sort, and Query Hints Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/HibernateSpringBootSliceAllCriteriaBuilderSortAndSpecificationAndQueryHints/README.md Demonstrates fetching a `Slice` using a `Specification`, `PageRequest` with custom `Sort`, and passing query hints. This method avoids the `SELECT COUNT` query. ```java public Slice fetchNextSlice(int page, int size) { // hint example Map hints = new HashMap<>(); hints.put("...", value); return authorRepository.findAll(isAgeGt45(), PageRequest.of(page, size, new Sort(Sort.Direction.ASC, "age")), LockModeType..., hints); } ``` -------------------------------- ### Fetch Next Page with Keyset Pagination Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/README.md Implement keyset pagination to efficiently determine if more records exist for a 'Next Page' button. This method fetches one extra record to check for the next page's existence. ```java public AuthorView fetchNextPage(long id, int limit) { List authors = authorRepository.fetchAll(id, limit + 1); if (authors.size() == (limit + 1)) { authors.remove(authors.size() - 1); return new AuthorView(authors, true); } return new AuthorView(authors, false); } ``` ```java public Map, Boolean> fetchNextPage(long id, int limit) { List authors = authorRepository.fetchAll(id, limit + 1); if(authors.size() == (limit + 1)) { authors.remove(authors.size() -1); return Collections.singletonMap(authors, true); } return Collections.singletonMap(authors, false); } ``` -------------------------------- ### Generate Two MySQL Databases and Match Entities Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/README.md Set up two distinct MySQL databases and map entities to their respective tables using the `@Table` annotation with a schema attribute. The JDBC URL should not include a specific database name. ```properties spring.datasource.url=jdbc:mysql://localhost:3306 ``` -------------------------------- ### Fetch Next Slice with Sorting Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/HibernateSpringBootSliceAllCriteriaBuilderAndSort/README.md This method demonstrates how to fetch the next slice of data with sorting applied. It uses `PageRequest.of` to specify the page number, size, and sort order. ```Java public Slice fetchNextSlice(int page, int size) { return authorRepository.findAll(PageRequest.of(page, size, new Sort(Sort.Direction.ASC, "age"))); } ``` -------------------------------- ### Run Docker Container Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/Java EE/CMPCandCMT/README.md Remove any existing container with the same name and then run the Docker image as a detached container, exposing ports 8080 and 4848. ```bash docker rm -f quickstart || true && docker run -d -p 8080:8080 -p 4848:4848 --name quickstart com.sample/quickstart ``` -------------------------------- ### Enable Log4j 2 Trace for Hibernate SQL Statements Source: https://github.com/anghelleonard/hibernate-springboot/blob/master/README.md Configure Log4j 2 to view prepared statement binding and extracted parameters. This requires excluding Spring Boot's default logging and adding the Log4j 2 dependency. ```xml ```