|
| 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 | + * https://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.jdbc.compositeid; |
| 17 | + |
| 18 | +import org.springframework.context.annotation.Bean; |
| 19 | +import org.springframework.context.annotation.Configuration; |
| 20 | +import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; |
| 21 | +import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; |
| 22 | +import org.springframework.data.relational.core.mapping.event.BeforeConvertCallback; |
| 23 | + |
| 24 | +import java.util.concurrent.atomic.AtomicLong; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Configuration for the demonstration of composite ids. |
| 29 | + * |
| 30 | + * Registers a {@link BeforeConvertCallback} for generating ids. |
| 31 | + * |
| 32 | + * @author Jens Schauder |
| 33 | + */ |
| 34 | +@Configuration |
| 35 | +@EnableJdbcRepositories |
| 36 | +public class CompositeConfiguration extends AbstractJdbcConfiguration { |
| 37 | + |
| 38 | + @Bean |
| 39 | + BeforeConvertCallback<Employee> idGeneration() { |
| 40 | + return new BeforeConvertCallback<>() { |
| 41 | + AtomicLong counter = new AtomicLong(); |
| 42 | + |
| 43 | + @Override |
| 44 | + public Employee onBeforeConvert(Employee employee) { |
| 45 | + if (employee.id == null) { |
| 46 | + employee.id = new EmployeeId(Organization.RND, counter.addAndGet(1)); |
| 47 | + } |
| 48 | + return employee; |
| 49 | + } |
| 50 | + }; |
| 51 | + } |
| 52 | +} |
0 commit comments