Skip to content

Commit 4b0799d

Browse files
spring data jpa view index sample (#35)
1 parent ec716e4 commit 4b0799d

File tree

2 files changed

+136
-132
lines changed

2 files changed

+136
-132
lines changed

jdbc/spring-data-jpa/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<properties>
1313
<maven.compiler.release>17</maven.compiler.release>
1414
<kotlin.version>1.9.22</kotlin.version>
15-
<hibernate.ydb.dialect.version>0.9.2</hibernate.ydb.dialect.version>
15+
<hibernate.ydb.dialect.version>0.9.5</hibernate.ydb.dialect.version>
1616
<spring.boot.version>3.2.1</spring.boot.version>
1717
</properties>
1818
<dependencies>
Lines changed: 135 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package tech.ydb.jpa.simple;
22

3+
import jakarta.persistence.QueryHint
4+
import org.hibernate.jpa.HibernateHints
35
import org.springframework.data.domain.Limit;
46
import org.springframework.data.domain.Pageable;
57
import org.springframework.data.domain.Slice;
68
import org.springframework.data.domain.Sort;
79
import org.springframework.data.jpa.repository.Query;
10+
import org.springframework.data.jpa.repository.QueryHints
811
import org.springframework.data.repository.ListCrudRepository;
912
import org.springframework.scheduling.annotation.Async;
1013
import java.util.concurrent.CompletableFuture
@@ -16,135 +19,136 @@ import java.util.stream.Stream
1619
*/
1720
interface SimpleUserRepository : ListCrudRepository<User, Long> {
1821

19-
/**
20-
* Find the user with the given username. This method will be translated into a query using the
21-
* {@link jakarta.persistence.NamedQuery} annotation at the {@link User} class.
22-
*
23-
* @param username
24-
*/
25-
fun findByTheUsersName(username: String): User
26-
27-
/**
28-
* Uses {@link Optional} as return and parameter type.
29-
*
30-
* @param username
31-
*/
32-
fun findByUsername(username: String?): User?
33-
34-
/**
35-
* Find all users with the given lastname. This method will be translated into a query by constructing it directly
36-
* from the method name as there is no other query declared.
37-
*
38-
* @param lastname
39-
*/
40-
fun findByLastname(lastname: String): List<User>
41-
42-
/**
43-
* Find at most the number of users defined via maxResults with the given lastname.
44-
* This method will be translated into a query by constructing it directly from the method name as there is no other
45-
* query declared.
46-
*
47-
* @param lastname
48-
* @param maxResults the maximum number of results returned.
49-
*/
50-
fun findByLastname(lastname: String, maxResults: Limit): List<User>
51-
52-
/**
53-
* Returns all users with the given firstname. This method will be translated into a query using the one declared in
54-
* the {@link Query} annotation declared one.
55-
*
56-
* @param firstname
57-
*/
58-
@Query("select u from User u where u.firstname = :firstname")
59-
fun findByFirstname(firstname: String): List<User>
60-
61-
/**
62-
* Returns at most the number of users defined via {@link Limit} with the given firstname. This method will be
63-
* translated into a query using the one declared in the {@link Query} annotation declared one.
64-
*
65-
* @param firstname
66-
* @param maxResults the maximum number of results returned.
67-
*/
68-
@Query("select u from User u where u.firstname = :firstname")
69-
fun findByFirstname(firstname: String, maxResults: Limit): List<User>
70-
71-
/**
72-
* Returns all users with the given name as first- or lastname. This makes the query to method relation much more
73-
* refactoring-safe as the order of the method parameters is completely irrelevant.
74-
*
75-
* @param name
76-
*/
77-
@Query("select u from User u where u.firstname = :name or u.lastname = :name")
78-
fun findByFirstnameOrLastname(name: String): List<User>
79-
80-
/**
81-
* Returns the total number of entries deleted as their lastnames match the given one.
82-
*
83-
* @param lastname
84-
* @return
85-
*/
86-
fun removeByLastname(lastname: String): Long
87-
88-
/**
89-
* Returns a {@link Slice} counting a maximum number of {@link Pageable#getPageSize()} users matching given criteria
90-
* starting at {@link Pageable#getOffset()} without prior count of the total number of elements available.
91-
*
92-
* @param lastname
93-
* @param page
94-
*/
95-
fun findByLastnameOrderByUsernameAsc(lastname: String, page: Pageable): Slice<User>
96-
97-
/**
98-
* Return the first 2 users ordered by their lastname asc.
99-
*
100-
* <pre>
101-
* Example for findFirstK / findTopK functionality.
102-
* </pre>
103-
*/
104-
fun findFirst2ByOrderByLastnameAsc(): List<User>
105-
106-
/**
107-
* Return the first 2 users ordered by the given {@code sort} definition.
108-
*
109-
* <pre>
110-
* This variant is very flexible because one can ask for the first K results when a ASC ordering
111-
* is used as well as for the last K results when a DESC ordering is used.
112-
* </pre>
113-
*
114-
* @param sort
115-
*/
116-
fun findTop2By(sort: Sort): List<User>
117-
118-
/**
119-
* Return all the users with the given firstname or lastname. Makes use of SpEL (Spring Expression Language).
120-
*
121-
* @param user
122-
*/
123-
@Query("select u from User u where u.firstname = :#{#user.firstname} or u.lastname = :#{#user.lastname}")
124-
fun findByFirstnameOrLastname(user: User): Iterable<User>
125-
126-
/**
127-
* Sample default method.
128-
*
129-
* @param user
130-
*/
131-
fun findByLastname(user: User): List<User> {
132-
return findByLastname(user.lastname);
133-
}
134-
135-
/**
136-
* Sample method to demonstrate support for {@link Stream} as a return type with a custom query. The query is executed
137-
* in a streaming fashion which means that the method returns as soon as the first results are ready.
138-
*/
139-
@Query("select u from User u")
140-
fun streamAllCustomers(): Stream<User>
141-
142-
/**
143-
* Sample method to demonstrate support for {@link Stream} as a return type with a derived query. The query is
144-
* executed in a streaming fashion which means that the method returns as soon as the first results are ready.
145-
*/
146-
fun findAllByLastnameIsNotNull(): Stream<User>
147-
148-
@Async
149-
fun readAllBy(): CompletableFuture<List<User>>
22+
/**
23+
* Find the user with the given username. This method will be translated into a query using the
24+
* {@link jakarta.persistence.NamedQuery} annotation at the {@link User} class.
25+
*
26+
* @param username
27+
*/
28+
fun findByTheUsersName(username: String): User
29+
30+
/**
31+
* Uses {@link Optional} as return and parameter type.
32+
*
33+
* @param username
34+
*/
35+
@QueryHints(value = [QueryHint(name = HibernateHints.HINT_COMMENT, value = "use_index:username_index")])
36+
fun findByUsername(username: String?): User?
37+
38+
/**
39+
* Find all users with the given lastname. This method will be translated into a query by constructing it directly
40+
* from the method name as there is no other query declared.
41+
*
42+
* @param lastname
43+
*/
44+
fun findByLastname(lastname: String): List<User>
45+
46+
/**
47+
* Find at most the number of users defined via maxResults with the given lastname.
48+
* This method will be translated into a query by constructing it directly from the method name as there is no other
49+
* query declared.
50+
*
51+
* @param lastname
52+
* @param maxResults the maximum number of results returned.
53+
*/
54+
fun findByLastname(lastname: String, maxResults: Limit): List<User>
55+
56+
/**
57+
* Returns all users with the given firstname. This method will be translated into a query using the one declared in
58+
* the {@link Query} annotation declared one.
59+
*
60+
* @param firstname
61+
*/
62+
@Query("select u from User u where u.firstname = :firstname")
63+
fun findByFirstname(firstname: String): List<User>
64+
65+
/**
66+
* Returns at most the number of users defined via {@link Limit} with the given firstname. This method will be
67+
* translated into a query using the one declared in the {@link Query} annotation declared one.
68+
*
69+
* @param firstname
70+
* @param maxResults the maximum number of results returned.
71+
*/
72+
@Query("select u from User u where u.firstname = :firstname")
73+
fun findByFirstname(firstname: String, maxResults: Limit): List<User>
74+
75+
/**
76+
* Returns all users with the given name as first- or lastname. This makes the query to method relation much more
77+
* refactoring-safe as the order of the method parameters is completely irrelevant.
78+
*
79+
* @param name
80+
*/
81+
@Query("select u from User u where u.firstname = :name or u.lastname = :name")
82+
fun findByFirstnameOrLastname(name: String): List<User>
83+
84+
/**
85+
* Returns the total number of entries deleted as their lastnames match the given one.
86+
*
87+
* @param lastname
88+
* @return
89+
*/
90+
fun removeByLastname(lastname: String): Long
91+
92+
/**
93+
* Returns a {@link Slice} counting a maximum number of {@link Pageable#getPageSize()} users matching given criteria
94+
* starting at {@link Pageable#getOffset()} without prior count of the total number of elements available.
95+
*
96+
* @param lastname
97+
* @param page
98+
*/
99+
fun findByLastnameOrderByUsernameAsc(lastname: String, page: Pageable): Slice<User>
100+
101+
/**
102+
* Return the first 2 users ordered by their lastname asc.
103+
*
104+
* <pre>
105+
* Example for findFirstK / findTopK functionality.
106+
* </pre>
107+
*/
108+
fun findFirst2ByOrderByLastnameAsc(): List<User>
109+
110+
/**
111+
* Return the first 2 users ordered by the given {@code sort} definition.
112+
*
113+
* <pre>
114+
* This variant is very flexible because one can ask for the first K results when a ASC ordering
115+
* is used as well as for the last K results when a DESC ordering is used.
116+
* </pre>
117+
*
118+
* @param sort
119+
*/
120+
fun findTop2By(sort: Sort): List<User>
121+
122+
/**
123+
* Return all the users with the given firstname or lastname. Makes use of SpEL (Spring Expression Language).
124+
*
125+
* @param user
126+
*/
127+
@Query("select u from User u where u.firstname = :#{#user.firstname} or u.lastname = :#{#user.lastname}")
128+
fun findByFirstnameOrLastname(user: User): Iterable<User>
129+
130+
/**
131+
* Sample default method.
132+
*
133+
* @param user
134+
*/
135+
fun findByLastname(user: User): List<User> {
136+
return findByLastname(user.lastname);
137+
}
138+
139+
/**
140+
* Sample method to demonstrate support for {@link Stream} as a return type with a custom query. The query is executed
141+
* in a streaming fashion which means that the method returns as soon as the first results are ready.
142+
*/
143+
@Query("select u from User u")
144+
fun streamAllCustomers(): Stream<User>
145+
146+
/**
147+
* Sample method to demonstrate support for {@link Stream} as a return type with a derived query. The query is
148+
* executed in a streaming fashion which means that the method returns as soon as the first results are ready.
149+
*/
150+
fun findAllByLastnameIsNotNull(): Stream<User>
151+
152+
@Async
153+
fun readAllBy(): CompletableFuture<List<User>>
150154
}

0 commit comments

Comments
 (0)