|
| 1 | +/* |
| 2 | + * Copyright 2019-2023 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 org.springframework.data.jdbc.repository; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.context.annotation.Bean; |
| 21 | +import org.springframework.context.annotation.Configuration; |
| 22 | +import org.springframework.context.annotation.Import; |
| 23 | +import org.springframework.data.annotation.Id; |
| 24 | +import org.springframework.data.domain.Persistable; |
| 25 | +import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; |
| 26 | +import org.springframework.data.jdbc.testing.DatabaseType; |
| 27 | +import org.springframework.data.jdbc.testing.EnabledOnDatabase; |
| 28 | +import org.springframework.data.jdbc.testing.IntegrationTest; |
| 29 | +import org.springframework.data.jdbc.testing.TestConfiguration; |
| 30 | +import org.springframework.data.relational.core.mapping.MappedCollection; |
| 31 | +import org.springframework.data.repository.CrudRepository; |
| 32 | + |
| 33 | +import java.util.UUID; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * Testing one-to-one relations with different id-types using {@link MappedCollection} annotation in Entities. |
| 39 | + * |
| 40 | + * @author Johan Blomgren |
| 41 | + */ |
| 42 | +@IntegrationTest |
| 43 | +@EnabledOnDatabase(DatabaseType.POSTGRES) |
| 44 | +public class JdbcRepositoryOneToOneCollectionIntegrationTests { |
| 45 | + |
| 46 | + @Configuration |
| 47 | + @Import(TestConfiguration.class) |
| 48 | + static class Config { |
| 49 | + |
| 50 | + @Bean |
| 51 | + DummyEntityRepository dummyEntityRepository(JdbcRepositoryFactory factory) { |
| 52 | + return factory.getRepository(DummyEntityRepository.class); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Autowired |
| 58 | + DummyEntityRepository repository; |
| 59 | + |
| 60 | + @Test // GH-1684 |
| 61 | + public void saveAndLoadAnEntity() { |
| 62 | + |
| 63 | + DummyEntity entity = repository.save(createDummyEntity()); |
| 64 | + |
| 65 | + assertThat(repository.findById(entity.getId())).hasValueSatisfying(it -> { |
| 66 | + assertThat(it.getId()).isEqualTo(entity.getId()); |
| 67 | + assertThat(it.getTest()).isEqualTo(entity.getTest()); |
| 68 | + assertThat(it.getDummyEntity2().getId()).isEqualTo(entity.getDummyEntity2().getId()); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + private static DummyEntity createDummyEntity() { |
| 73 | + DummyEntity entity = new DummyEntity(); |
| 74 | + entity.setTest("test"); |
| 75 | + |
| 76 | + final DummyEntity2 dummyEntity2 = new DummyEntity2(); |
| 77 | + |
| 78 | + entity.setDummyEntity2(dummyEntity2); |
| 79 | + |
| 80 | + return entity; |
| 81 | + } |
| 82 | + |
| 83 | + interface DummyEntityRepository extends CrudRepository<DummyEntity, UUID> { |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + static class DummyEntity implements Persistable<UUID> { |
| 88 | + |
| 89 | + @Id |
| 90 | + UUID id = UUID.randomUUID(); |
| 91 | + |
| 92 | + @MappedCollection(idColumn = "dummy_entity_id") |
| 93 | + DummyEntity2 dummyEntity2; |
| 94 | + |
| 95 | + String test; |
| 96 | + |
| 97 | + // Simply setting it to new not having to add `DEFAULT uuid_generate_v4()` in db-script |
| 98 | + @Override |
| 99 | + public boolean isNew() { |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + public UUID getId() { |
| 104 | + return id; |
| 105 | + } |
| 106 | + |
| 107 | + public void setId(UUID id) { |
| 108 | + this.id = id; |
| 109 | + } |
| 110 | + |
| 111 | + public DummyEntity2 getDummyEntity2() { |
| 112 | + return dummyEntity2; |
| 113 | + } |
| 114 | + |
| 115 | + public void setDummyEntity2(DummyEntity2 dummyEntity2) { |
| 116 | + this.dummyEntity2 = dummyEntity2; |
| 117 | + } |
| 118 | + |
| 119 | + public String getTest() { |
| 120 | + return test; |
| 121 | + } |
| 122 | + |
| 123 | + public void setTest(String test) { |
| 124 | + this.test = test; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + static class DummyEntity2 { |
| 129 | + @Id |
| 130 | + Long id; |
| 131 | + |
| 132 | + public Long getId() { |
| 133 | + return id; |
| 134 | + } |
| 135 | + |
| 136 | + public void setId(Long id) { |
| 137 | + this.id = id; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments