8000 Objects, immutability and switch expressions · Daleb015/Java-Coding-Problems@92eb0f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 92eb0f8

Browse files
committed
Objects, immutability and switch expressions
1 parent 4b208b6 commit 92eb0f8

File tree

18 files changed

+422
-0
lines changed

18 files changed

+422
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
**[How To Implement Pagination Via `ROW_NUMBER()` Window Function](< B41A span class="pl-corl">https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootPaginationRowNumber)**
2+
3+
**Description:** This application is an example of using `ROW_NUMBER()` (and `COUNT(*) OVER()` for counting all elements) window function to implement pagination.
4+
5+
**Key points:**
6+
- use a native query relying on `ROW_NUMBER()`
7+
- we don't return a page as `Page` or `Slice`, we return it as `List`, therefore `Pageable` is not used
8+
9+
-----------------------------------------------------------------------------------------------------------------------
10+
<table>
11+
<tr><td><b>If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"</b></td><td><b>If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.</b></td></tr>
12+
<tr><td>
13+
<a href="https://www.apress.com/us/book/9781484256251"><p align="left"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Spring%20Boot%20Persistence%20Best%20Practices.jpg" height="500" width="450"/></p></a>
14+
</td><td>
15+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="right"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="500" width="450"/></p></a>
16+
</td></tr></table>
17+
18+
-----------------------------------------------------------------------------------------------------------------------
19+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>--enable-preview -classpath %classpath ${packageClassName}</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>process-classes</goal>
24+
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
25+
</goals>
26+
<properties>
27+
<exec.args>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address} --enable-preview -classpath %classpath ${packageClassName}</exec.args>
28+
<exec.executable>java</exec.executable>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>--enable-preview -classpath %classpath ${packageClassName}</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>BONUS_1_SpringAndJavaRecords</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>BONUS_1_SpringAndJavaRecords</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.2.6.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<maven.compiler.source>14</maven.compiler.source>
26+
<maven.compiler.target>14</maven.compiler.target>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>3.8.1</version>
51+
<configuration>
52+
<release>14</release>
53+
<compilerArgs>
54+
<arg>--enable-preview</arg>
55+
</compilerArgs>
56+
<forceJavacCompilerUse>true</forceJavacCompilerUse>
57+
<parameters>true</parameters>
58+
</configuration>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-surefire-plugin</artifactId>
63+
<version>3.0.0-M3</version>
64+
<configuration>
65+
<argLine>--enable-preview</argLine>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package modern.challenge;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class MainApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(MainApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package modern.challenge.controller;
2+
3+
import java.util.List;
4+
import modern.challenge.service.BookstoreService;
5+
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import modern.challenge.record.Author;
10+
11+
@RestController
12+
public class BookstoreController {
13+
14+
private final BookstoreService bookstoreService;
15+
16+
public BookstoreController(BookstoreService bookstoreService) {
17+
this.bookstoreService = bookstoreService;
18+
}
19+
20+
@GetMapping("/authors")
21+
public List<Author> fetchAuthors() {
22+
23+
return bookstoreService.fetchAuthors();
24+
}
25+
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package modern.challenge.record;
2+
3+
import java.util.List;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
public record Author(@JsonProperty("name") String name,
7+
@JsonProperty("genre") String genre,
8+
@JsonProperty("books") List<Book> books) {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package modern.challenge.record;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public record Book(@JsonProperty("title") String title,
6+
@JsonProperty("isbn") String isbn) {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package modern.challenge.service;
2+
3+
import java.util.List;
4+
import org.springframework.stereotype.Service;
5+
6+
import modern.challenge.record.Author;
7+
import modern.challenge.record.Book;
8+
9+
@Service
10+
public class BookstoreService {
11+
12+
public List<Author> fetchAuthors() {
13+
14+
List<Author> authors = List.of(
15+
new Author("Joana Nimar", "History", List.of(
16+
new Book("History of a day", "JN-001"),
17+
new Book("Prague history", "JN-002")
18+
)),
19+
new Author("Mark Janel", "Horror", List.of(
20+
new Book("Carrie", "MJ-001"),
21+
new Book("House of pain", "MJ-002")
22+
)
23+
));
24+
25+
return authors;
26+
}
27+
}

Chapter02/BONUS_1_SpringAndJavaRecords/src/main/resources/application.properties

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
**[How To Implement Pagination Via `ROW_NUMBER()` Window Function](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootPaginationRowNumber)**
2+
3+
**Description:** This application is an example of using `ROW_NUMBER()` (and `COUNT(*) OVER()` for counting all elements) window function to implement pagination.
4+
5+
**Key points:**
6+
- use a native query relying on `ROW_NUMBER()`
7+
- we don't return a page as `Page` or `Slice`, we return it as `List`, therefore `Pageable` is not used
8+
9+
-----------------------------------------------------------------------------------------------------------------------
10+
<table>
11+
<tr><td><b>If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"</b></td><td><b>If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.</b></td></tr>
12+
<tr><td>
13+
<a href="https://www.apress.com/us/book/9781484256251"><p align="left"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Spring%20Boot%20Persistence%20Best%20Practices.jpg" height="500" width="450"/></p></a>
14+
</td><td>
15+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="right"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="500" width="450"/></p></a>
16+
</td></tr></table>
17+
18+
-----------------------------------------------------------------------------------------------------------------------
19+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>--enable-preview -classpath %classpath ${packageClassName}</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>process-classes</goal>
24+
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
25+
</goals>
26+
<properties>
27+
<exec.args>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address} --enable-preview -classpath %classpath ${packageClassName}</exec.args>
28+
<exec.executable>java</exec.executable>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>--enable-preview -classpath %classpath ${packageClassName}</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>BONUS_2_SpringAndJavaRecordsInjection</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>BONUS_2_SpringAndJavaRecordsInjection</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.2.6.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<maven.compiler.source>14</maven.compiler.source>
26+
<maven.compiler.target>14</maven.compiler.target>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>3.8.1</version>
51+
<configuration>
52+
<release>14</release>
53+
<compilerArgs>
54+
<arg>--enable-preview</arg>
55+
</compilerArgs>
56+
<forceJavacCompilerUse>true</forceJavacCompilerUse>
57+
<parameters>true</parameters>
58+
</configuration>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-surefire-plugin</artifactId>
63+
<version>3.0.0-M3</version>
64+
<configuration>
65+
<argLine>--enable-preview</argLine>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package modern.challenge;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class MainApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(MainApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package modern.challenge.controller;
2+
3+
import java.util.List;
4+
import modern.challenge.service.BookstoreService;
5+
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import modern.challenge.record.Author;
10+
11+
@RestController
12+
public record BookstoreController(BookstoreService bookstoreService) {
13+
14+
@GetMapping("/authors")
15+
public List<Author> fetchAuthors() {
16+
17+
return bookstoreService.fetchAuthors();
18+
}
19+
20+
}

0 commit comments

Comments
 (0)
0