Skip to content

Commit 1bff0e7

Browse files
Merge pull request #20 from KirillKurdyukov/spring_data_jpa_example
Spring Data JPA Example
2 parents 58b76eb + 18bab38 commit 1bff0e7

File tree

17 files changed

+956
-0
lines changed

17 files changed

+956
-0
lines changed

jdbc/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@
3838
</dependency>
3939
</dependencies>
4040
</dependencyManagement>
41+
42+
<profiles>
43+
<profile>
44+
<id>jdk17-examples</id>
45+
<activation>
46+
<jdk>[17</jdk>
47+
</activation>
48+
<modules>
49+
<module>spring-data-jpa</module>
50+
</modules>
51+
</profile>
52+
</profiles>
4153
</project>

jdbc/spring-data-jpa/pom.xml

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>tech.ydb.jdbc.examples</groupId>
5+
<artifactId>ydb-jdbc-examples</artifactId>
6+
<version>1.1.0-SNAPSHOT</version>
7+
</parent>
8+
9+
<artifactId>spring-data-jpa</artifactId>
10+
<name>Spring Data JPA Example</name>
11+
<description>Basic example for SpringBoot3 and Hibernate 6</description>
12+
<properties>
13+
<maven.compiler.release>17</maven.compiler.release>
14+
<kotlin.version>1.9.22</kotlin.version>
15+
<hibernate.ydb.dialect.version>0.9.1</hibernate.ydb.dialect.version>
16+
<ydb.jdbc.driver.version>2.0.5</ydb.jdbc.driver.version>
17+
<spring.boot.version>3.2.1</spring.boot.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-data-jpa</artifactId>
23+
<version>${spring.boot.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.data</groupId>
27+
<artifactId>spring-data-commons</artifactId>
28+
<version>${spring.boot.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.jetbrains.kotlin</groupId>
32+
<artifactId>kotlin-reflect</artifactId>
33+
<version>${kotlin.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.jetbrains.kotlin</groupId>
37+
<artifactId>kotlin-stdlib</artifactId>
38+
<version>${kotlin.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>tech.ydb.dialects</groupId>
42+
<artifactId>hibernate-ydb-dialect</artifactId>
43+
<version>${hibernate.ydb.dialect.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>tech.ydb.jdbc</groupId>
47+
<artifactId>ydb-jdbc-driver</artifactId>
48+
<version>${ydb.jdbc.driver.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.github.javafaker</groupId>
52+
<artifactId>javafaker</artifactId>
53+
<version>1.0.2</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.apache.logging.log4j</groupId>
57+
<artifactId>log4j-slf4j-impl</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.jetbrains.kotlinx</groupId>
61+
<artifactId>kotlinx-coroutines-core</artifactId>
62+
<version>1.7.3</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.postgresql</groupId>
66+
<artifactId>postgresql</artifactId>
67+
<version>42.7.1</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.testcontainers</groupId>
71+
<artifactId>postgresql</artifactId>
72+
<version>1.19.1</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>tech.ydb.test</groupId>
77+
<artifactId>ydb-junit5-support</artifactId>
78+
<scope>test</scope>
79+
<exclusions>
80+
<exclusion>
81+
<groupId>org.junit.jupiter</groupId>
82+
<artifactId>junit-jupiter-api</artifactId>
83+
</exclusion>
84+
</exclusions>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-starter-test</artifactId>
89+
<version>${spring.boot.version}</version>
90+
<scope>test</scope>
91+
</dependency>
92+
</dependencies>
93+
<build>
94+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
95+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
96+
<plugins>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-surefire-plugin</artifactId>
100+
<configuration>
101+
<environmentVariables>
102+
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
103+
</environmentVariables>
104+
</configuration>
105+
</plugin>
106+
<plugin>
107+
<groupId>org.springframework.boot</groupId>
108+
<artifactId>spring-boot-maven-plugin</artifactId>
109+
<version>${spring.boot.version}</version>
110+
</plugin>
111+
<plugin>
112+
<groupId>org.jetbrains.kotlin</groupId>
113+
<artifactId>kotlin-maven-plugin</artifactId>
114+
<version>${kotlin.version}</version>
115+
<executions>
116+
<execution>
117+
<id>compile</id>
118+
<phase>compile</phase>
119+
<goals>
120+
<goal>compile</goal>
121+
</goals>
122+
</execution>
123+
<execution>
124+
<id>test-compile</id>
125+
<goals>
126+
<goal>test-compile</goal>
127+
</goals>
128+
</execution>
129+
</executions>
130+
<configuration>
131+
<args>
132+
<arg>-Xjsr305=strict</arg>
133+
</args>
134+
<compilerPlugins>
135+
<plugin>spring</plugin>
136+
<plugin>jpa</plugin>
137+
</compilerPlugins>
138+
</configuration>
139+
<dependencies>
140+
<dependency>
141+
<groupId>org.jetbrains.kotlin</groupId>
142+
<artifactId>kotlin-maven-allopen</artifactId>
143+
<version>${kotlin.version}</version>
144+
</dependency>
145+
<dependency>
146+
<groupId>org.jetbrains.kotlin</groupId>
147+
<artifactId>kotlin-maven-noarg</artifactId>
148+
<version>${kotlin.version}</version>
149+
</dependency>
150+
</dependencies>
151+
</plugin>
152+
</plugins>
153+
</build>
154+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tech.ydb.jpa.pagination
2+
3+
import jakarta.persistence.Entity
4+
import jakarta.persistence.Id
5+
import jakarta.persistence.Table
6+
7+
@Entity
8+
@Table(name = "authors")
9+
class Author {
10+
11+
@Id
12+
lateinit var id: String
13+
14+
lateinit var firstName: String
15+
16+
lateinit var lastName: String
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tech.ydb.jpa.pagination
2+
3+
import jakarta.persistence.Entity
4+
import jakarta.persistence.Id
5+
import jakarta.persistence.ManyToOne
6+
import jakarta.persistence.Table
7+
import java.util.*
8+
9+
@Entity
10+
@Table(name = "books")
11+
class Book {
12+
13+
@Id
14+
lateinit var id: String
15+
16+
lateinit var title: String
17+
lateinit var isbn10: String
18+
lateinit var publicationDate: Date
19+
20+
@ManyToOne
21+
lateinit var author: Author
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package tech.ydb.jpa.pagination
2+
3+
import org.springframework.data.domain.*
4+
import org.springframework.data.repository.ListCrudRepository
5+
import org.springframework.data.repository.query.Param
6+
7+
interface BookRepository : ListCrudRepository<Book, String> {
8+
9+
/**
10+
* Uses an offset based pagination that first sorts the entries by their [ publication_date][Book.getPublicationDate]
11+
* and then limits the result by dropping the number of rows specified in the
12+
* [offset][Pageable.getOffset] clause. To retrieve [Page.getTotalElements] an additional count query
13+
* is executed.
14+
*
15+
* @param title
16+
* @param pageable
17+
*/
18+
fun findByTitleContainsOrderByPublicationDate(@Param("title") title: String, pageable: Pageable): Page<Book>
19+
20+
/**
21+
* Uses an offset based slicing that first sorts the entries by their [ publication_date][Book.getPublicationDate] and then limits the result by dropping the number of rows specified in the
22+
* [offset][Pageable.getOffset] clause.
23+
*
24+
* @param title
25+
* @param pageable
26+
*/
27+
fun findBooksByTitleContainsOrderByPublicationDate(title: String, pageable: Pageable): Slice<Book>
28+
29+
/**
30+
* Depending on the provided [ScrollPosition] either [ offset][org.springframework.data.domain.OffsetScrollPosition]
31+
* or [keyset][org.springframework.data.domain.KeysetScrollPosition] scrolling is possible. Scrolling
32+
* through results requires a stable [org.springframework.data.domain.Sort] which is different from what
33+
* [Pageable.getSort] offers. The limit is defined via the Top keyword.
34+
*
35+
* @param title
36+
* @param scrollPosition
37+
*/
38+
fun findTop2ByTitleContainsOrderByPublicationDate(title: String, scrollPosition: ScrollPosition): Window<Book>
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package tech.ydb.jpa.pagination
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
5+
@SpringBootApplication
6+
class PagingApplication

0 commit comments

Comments
 (0)