|
| 1 | +/* |
| 2 | + * Copyright 2018 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 org.springframework.data.jpa.repository.projections; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import lombok.Data; |
| 21 | + |
| 22 | +import javax.persistence.Access; |
| 23 | +import javax.persistence.AccessType; |
| 24 | +import javax.persistence.CascadeType; |
| 25 | +import javax.persistence.Entity; |
| 26 | +import javax.persistence.GeneratedValue; |
| 27 | +import javax.persistence.GenerationType; |
| 28 | +import javax.persistence.Id; |
| 29 | +import javax.persistence.OneToOne; |
| 30 | +import javax.persistence.Table; |
| 31 | + |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.springframework.beans.factory.annotation.Autowired; |
| 35 | +import org.springframework.data.repository.CrudRepository; |
| 36 | +import org.springframework.test.context.ContextConfiguration; |
| 37 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 38 | +import org.springframework.transaction.annotation.Transactional; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Reda.Housni-Alaoui |
| 42 | + */ |
| 43 | +@Transactional |
| 44 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 45 | +@ContextConfiguration(classes = ProjectionsIntegrationTests.Config.class) |
| 46 | +public class ProjectionJoinIntegrationTests { |
| 47 | + |
| 48 | + @Autowired private UserRepository userRepository; |
| 49 | + |
| 50 | + @Test |
| 51 | + public void findByIdPerformsAnOuterJoin() { |
| 52 | + User user = userRepository.save(new User()); |
| 53 | + |
| 54 | + UserProjection projection = userRepository.findById(user.getId(), UserProjection.class); |
| 55 | + |
| 56 | + assertThat(projection).isNotNull(); |
| 57 | + assertThat(projection.getId()).isEqualTo(user.getId()); |
| 58 | + assertThat(projection.getAddress()).isNull(); |
| 59 | + } |
| 60 | + |
| 61 | + @Data |
| 62 | + private static class UserProjection { |
| 63 | + |
| 64 | + private final int id; |
| 65 | + private final Address address; |
| 66 | + |
| 67 | + public UserProjection(int id, Address address) { |
| 68 | + this.id = id; |
| 69 | + this.address = address; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public interface UserRepository extends CrudRepository<User, Integer> { |
| 74 | + |
| 75 | + <T> T findById(int id, Class<T> projectionClass); |
| 76 | + } |
| 77 | + |
| 78 | + @Data |
| 79 | + @Table(name = "ProjectionJoinIntegrationTests_User") |
| 80 | + @Entity |
| 81 | + static class User { |
| 82 | + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Access(value = AccessType.PROPERTY) int id; |
| 83 | + |
| 84 | + @OneToOne(cascade = CascadeType.ALL) Address address; |
| 85 | + } |
| 86 | + |
| 87 | + @Data |
| 88 | + @Table(name = "ProjectionJoinIntegrationTests_Address") |
| 89 | + @Entity |
| 90 | + static class Address { |
| 91 | + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Access(value = AccessType.PROPERTY) int id; |
| 92 | + |
| 93 | + String streetName; |
| 94 | + } |
| 95 | +} |
0 commit comments