|
| 1 | +package org.hibernate.orm.test.embeddable; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.hibernate.jpa.SpecHints; |
| 6 | + |
| 7 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 8 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 9 | +import org.hibernate.testing.orm.junit.Jpa; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import jakarta.persistence.Column; |
| 14 | +import jakarta.persistence.ElementCollection; |
| 15 | +import jakarta.persistence.Embeddable; |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.FetchType; |
| 18 | +import jakarta.persistence.Id; |
| 19 | +import jakarta.persistence.ManyToOne; |
| 20 | +import jakarta.persistence.TypedQuery; |
| 21 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 22 | +import jakarta.persistence.criteria.CriteriaQuery; |
| 23 | +import jakarta.persistence.criteria.Root; |
| 24 | + |
| 25 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 27 | + |
| 28 | +@Jpa( |
| 29 | + annotatedClasses = { |
| 30 | + ElementCollectionLazyToOneTest.TheEntity.class |
| 31 | + } |
| 32 | +) |
| 33 | +@JiraKey( "HHH-17383" ) |
| 34 | +public class ElementCollectionLazyToOneTest { |
| 35 | + |
| 36 | + private static final Long ENTITY_1 = 1L; |
| 37 | + private static final Long ENTITY_2 = 2L; |
| 38 | + |
| 39 | + @BeforeAll |
| 40 | + public void setUp(EntityManagerFactoryScope scope) { |
| 41 | + scope.inTransaction( |
| 42 | + entityManager -> { |
| 43 | + TheEntity e2 = new TheEntity( ENTITY_2, "e2" ); |
| 44 | + TheEntity e1 = new TheEntity( ENTITY_1, "e1" ); |
| 45 | + TheEmbeddable embeddable = new TheEmbeddable(); |
| 46 | + embeddable.setContent( "abc" ); |
| 47 | + embeddable.setEntity( e2 ); |
| 48 | + e1.setEmbeddables( List.of( embeddable ) ); |
| 49 | + entityManager.persist( e2 ); |
| 50 | + entityManager.persist( e1 ); |
| 51 | + } |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testInitializingCollection(EntityManagerFactoryScope scope) { |
| 57 | + scope.inTransaction( |
| 58 | + entityManager -> { |
| 59 | + TheEntity entity = entityManager.find( TheEntity.class, ENTITY_1 ); |
| 60 | + entityManager.createQuery( "from TheEntity e left join fetch e.embeddables" ).getResultList(); |
| 61 | + |
| 62 | + List<TheEmbeddable> embeddables = entity.getEmbeddables(); |
| 63 | + assertThat( embeddables.size() ).isEqualTo( 1 ); |
| 64 | + TheEmbeddable theEmbeddable = embeddables.get( 0 ); |
| 65 | + assertNotNull( theEmbeddable ); |
| 66 | + assertThat( theEmbeddable.getEntity() ).isNotNull(); |
| 67 | + } |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + @Embeddable |
| 72 | + public static class TheEmbeddable { |
| 73 | + @Column |
| 74 | + private String content; |
| 75 | + @ManyToOne(fetch = FetchType.LAZY) |
| 76 | + private TheEntity entity; |
| 77 | + |
| 78 | + public String getContent() { |
| 79 | + return content; |
| 80 | + } |
| 81 | + |
| 82 | + public void setContent(String content) { |
| 83 | + this.content = content; |
| 84 | + } |
| 85 | + |
| 86 | + public TheEntity getEntity() { |
| 87 | + return entity; |
| 88 | + } |
| 89 | + |
| 90 | + public void setEntity(TheEntity entity) { |
| 91 | + this.entity = entity; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Entity(name = "TheEntity") |
| 96 | + public static class TheEntity { |
| 97 | + @Id |
| 98 | + private Long id; |
| 99 | + private String name; |
| 100 | + |
| 101 | + @ElementCollection |
| 102 | + private List<TheEmbeddable> embeddables; |
| 103 | + |
| 104 | + public TheEntity() { |
| 105 | + } |
| 106 | + |
| 107 | + public TheEntity(Long id, String name) { |
| 108 | + this.id = id; |
| 109 | + this.name = name; |
| 110 | + } |
| 111 | + |
| 112 | + public Long getId() { |
| 113 | + return id; |
| 114 | + } |
| 115 | + |
| 116 | + public void setId(Long id) { |
| 117 | + this.id = id; |
| 118 | + } |
| 119 | + |
| 120 | + public String getName() { |
| 121 | + return name; |
| 122 | + } |
| 123 | + |
| 124 | + public void setName(String name) { |
| 125 | + this.name = name; |
| 126 | + } |
| 127 | + |
| 128 | + public List<TheEmbeddable> getEmbeddables() { |
| 129 | + return embeddables; |
| 130 | + } |
| 131 | + |
| 132 | + public void setEmbeddables(List<TheEmbeddable> embeddables) { |
| 133 | + this.embeddables = embeddables; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments