|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package example.springdata.aot; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.boot.CommandLineRunner; |
| 22 | +import org.springframework.data.domain.Page; |
| 23 | +import org.springframework.data.domain.PageRequest; |
| 24 | +import org.springframework.data.domain.Slice; |
| 25 | +import org.springframework.stereotype.Component; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Christoph Strobl |
| 29 | + * @since 2025/01 |
| 30 | + */ |
| 31 | +@Component |
| 32 | +public class CLR implements CommandLineRunner { |
| 33 | + |
| 34 | + @Autowired UserRepository repository; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void run(String... args) throws Exception { |
| 38 | + |
| 39 | + User luke = new User("id-1", "luke"); |
| 40 | + luke.setFirstname("Luke"); |
| 41 | + luke.setLastname("Skywalker"); |
| 42 | +// Post lukeP1 = new Post("I have a bad feeling about this."); |
| 43 | +// em.persist(lukeP1); |
| 44 | +// luke.setPosts(List.of(lukeP1)); |
| 45 | + |
| 46 | + User leia = new User("id-2", "leia"); |
| 47 | + leia.setFirstname("Leia"); |
| 48 | + leia.setLastname("Organa"); |
| 49 | + |
| 50 | + User han = new User("id-3", "han"); |
| 51 | + han.setFirstname("Han"); |
| 52 | + han.setLastname("Solo"); |
| 53 | +// Post hanP1 = new Post("It's the ship that made the Kessel Run in less than 12 Parsecs."); |
| 54 | +// em.persist(hanP1); |
| 55 | +// han.setPosts(List.of(hanP1)); |
| 56 | + |
| 57 | + User chewbacca = new User("id-4", "chewbacca"); |
| 58 | + User yoda = new User("id-5", "yoda"); |
| 59 | + Post yodaP1 = new Post("Do. Or do not. There is no try."); |
| 60 | + Post yodaP2 = new Post("Decide you must, how to serve them best. If you leave now, help them you could; but you would destroy all for which they have fought, and suffered."); |
| 61 | +// em.persist(yodaP1); |
| 62 | +// em.persist(yodaP2); |
| 63 | +// yoda.setPosts(List.of(yodaP1, yodaP2)); |
| 64 | + |
| 65 | + User vader = new User("id-6", "vader"); |
| 66 | + vader.setFirstname("Anakin"); |
| 67 | + vader.setLastname("Skywalker"); |
| 68 | +// Post vaderP1 = new Post("I am your father"); |
| 69 | +// em.persist(vaderP1); |
| 70 | +// vader.setPosts(List.of(vaderP1)); |
| 71 | + |
| 72 | + User kylo = new User("id-7", "kylo"); |
| 73 | + kylo.setFirstname("Ben"); |
| 74 | + kylo.setLastname("Solo"); |
| 75 | + |
| 76 | + repository.saveAll(List.of(luke, leia, han, chewbacca, yoda, vader, kylo)); |
| 77 | + |
| 78 | + System.out.println("------- annotated multi -------"); |
| 79 | + System.out.println(repository.usersWithUsernamesStartingWith("l")); |
| 80 | + |
| 81 | + System.out.println("------- derived single -------"); |
| 82 | + System.out.println(repository.findUserByUsername("yoda")); |
| 83 | + |
| 84 | +// System.out.println("------- derived nested.path -------"); |
| 85 | +// System.out.println(repository.findUserByPostsMessageLike("father")); |
| 86 | + |
| 87 | + System.out.println("------- derived optional -------"); |
| 88 | + System.out.println(repository.findOptionalUserByUsername("yoda")); |
| 89 | + |
| 90 | + System.out.println("------- derived count -------"); |
| 91 | + Long count = repository.countUsersByLastnameLike("Sky"); |
| 92 | + System.out.println("user count " + count); |
| 93 | + |
| 94 | + System.out.println("------- derived exists -------"); |
| 95 | + Boolean exists = repository.existsByUsername("vader"); |
| 96 | + System.out.println("user exists " + exists); |
| 97 | + |
| 98 | + System.out.println("------- derived multi -------"); |
| 99 | + System.out.println(repository.findUserByLastnameStartingWith("Sky")); |
| 100 | + |
| 101 | + System.out.println("------- derived sorted -------"); |
| 102 | + System.out.println(repository.findUserByLastnameStartingWithOrderByFirstname("Sky")); |
| 103 | + |
| 104 | + System.out.println("------- derived page -------"); |
| 105 | + Page<User> page0 = repository.findUserByLastnameStartingWith("S", PageRequest.of(0, 2)); |
| 106 | + System.out.println("page0: " + page0); |
| 107 | + System.out.println("page0.content: " + page0.getContent()); |
| 108 | + |
| 109 | + Page<User> page1 = repository.findUserByLastnameStartingWith("S", PageRequest.of(1, 2)); |
| 110 | + System.out.println("page1: " + page1); |
| 111 | + System.out.println("page1.content: " + page1.getContent()); |
| 112 | + |
| 113 | + System.out.println("------- derived slice -------"); |
| 114 | + Slice<User> slice0 = repository.findUserByUsernameAfter("luke", PageRequest.of(0, 2)); |
| 115 | + System.out.println("slice0: " + slice0); |
| 116 | + System.out.println("slice0.content: " + slice0.getContent()); |
| 117 | + |
| 118 | + System.out.println("------- derived top -------"); |
| 119 | + System.out.println(repository.findTop2UsersByLastnameStartingWith("S")); |
| 120 | + |
| 121 | +// System.out.println("------- derived with fields -------"); |
| 122 | +// System.out.println(repository.findJustUsernameBy()); |
| 123 | + } |
| 124 | +} |
0 commit comments