|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.jpa; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.FetchType; |
| 10 | +import jakarta.persistence.GeneratedValue; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import jakarta.persistence.JoinColumn; |
| 13 | +import jakarta.persistence.ManyToOne; |
| 14 | +import jakarta.persistence.MapKeyJoinColumn; |
| 15 | +import jakarta.persistence.OneToMany; |
| 16 | +import jakarta.persistence.OneToOne; |
| 17 | +import jakarta.persistence.Table; |
| 18 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 19 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 20 | +import org.hibernate.testing.orm.junit.Jpa; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Réda Housni Alaoui |
| 32 | + */ |
| 33 | +@Jpa(annotatedClasses = {EmptyMapTest.User.class, EmptyMapTest.Identity.class, EmptyMapTest.UserIdentity.class}) |
| 34 | +public class EmptyMapTest { |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setupData(EntityManagerFactoryScope scope) { |
| 38 | + scope.inTransaction( em -> em.persist( new User( 1 ) ) ); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + public void cleanupData(EntityManagerFactoryScope scope) { |
| 43 | + scope.inTransaction(em -> em.createQuery( "delete from User u" ).executeUpdate() ); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + @JiraKey(value = "HHH-18658") |
| 48 | + public void test(EntityManagerFactoryScope scope) { |
| 49 | + scope.inTransaction( em -> { |
| 50 | + assertThat( em.find( User.class, 1 ) ).isNotNull(); |
| 51 | + } ); |
| 52 | + } |
| 53 | + |
| 54 | + @Entity(name = "User") |
| 55 | + @Table(name = "user_tbl") |
| 56 | + public static class User { |
| 57 | + @Id |
| 58 | + private int id; |
| 59 | + private String name; |
| 60 | + |
| 61 | + @OneToMany( |
| 62 | + mappedBy = "user", |
| 63 | + fetch = FetchType.EAGER, |
| 64 | + cascade = CascadeType.ALL, |
| 65 | + orphanRemoval = true) |
| 66 | + @MapKeyJoinColumn |
| 67 | + private final Map<Identity, UserIdentity> userIdentityByIdentity = new HashMap<>(); |
| 68 | + |
| 69 | + public User() { |
| 70 | + } |
| 71 | + |
| 72 | + public User(int id) { |
| 73 | + this.id = id; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Entity(name = "Identity") |
| 78 | + @Table(name = "identity_tbl") |
| 79 | + public static class Identity { |
| 80 | + @Id |
| 81 | + @GeneratedValue |
| 82 | + private int id; |
| 83 | + private String name; |
| 84 | + } |
| 85 | + |
| 86 | + @Entity(name = "UserIdentity") |
| 87 | + @Table(name = "user_identity_tbl") |
| 88 | + public static class UserIdentity { |
| 89 | + @Id |
| 90 | + @GeneratedValue |
| 91 | + private int id; |
| 92 | + |
| 93 | + @ManyToOne(fetch = FetchType.LAZY) |
| 94 | + @JoinColumn(nullable = false, updatable = false) |
| 95 | + private User user; |
| 96 | + |
| 97 | + @OneToOne(fetch = FetchType.EAGER) |
| 98 | + @JoinColumn(nullable = false, updatable = false) |
| 99 | + private Identity identity; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments