|
| 1 | +package org.hibernate.orm.test.jpa.compliance; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.hibernate.testing.TestForIssue; |
| 6 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 7 | +import org.hibernate.testing.orm.junit.Jpa; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import jakarta.persistence.Embeddable; |
| 12 | +import jakarta.persistence.Embedded; |
| 13 | +import jakarta.persistence.Entity; |
| 14 | +import jakarta.persistence.Id; |
| 15 | +import jakarta.persistence.Table; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.junit.Assert.assertNotSame; |
| 19 | + |
| 20 | +@Jpa( |
| 21 | + annotatedClasses = { |
| 22 | + EmbeddableInQueryResultTest.Person.class, |
| 23 | + } |
| 24 | +) |
| 25 | +@TestForIssue( jiraKey = "HHH-15223") |
| 26 | +public class EmbeddableInQueryResultTest { |
| 27 | + |
| 28 | + @BeforeEach |
| 29 | + public void setUp(EntityManagerFactoryScope scope) { |
| 30 | + scope.inTransaction( |
| 31 | + entityManager -> { |
| 32 | + Country italy = new Country( "Italy", "ITA" ); |
| 33 | + Person person = new Person( 1, "Ines", italy ); |
| 34 | + |
| 35 | + entityManager.persist( person ); |
| 36 | + } |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testSelectEmbeddableIsNotInTheManagedState(EntityManagerFactoryScope scope) { |
| 42 | + scope.inTransaction( |
| 43 | + entityManager -> { |
| 44 | + List<Object[]> results = entityManager.createQuery( "SELECT p, p.country FROM Person p " ) |
| 45 | + .getResultList(); |
| 46 | + assertThat( results.size() ).isEqualTo( 1 ); |
| 47 | + |
| 48 | + Object[] result = results.get( 0 ); |
| 49 | + Person person = (Person) result[0]; |
| 50 | + Country country = (Country) result[1]; |
| 51 | + assertNotSame( country, person.getCountry() ); |
| 52 | + country.setCode( "ITA_1" ); |
| 53 | + } |
| 54 | + ); |
| 55 | + |
| 56 | + scope.inTransaction( |
| 57 | + entityManager -> { |
| 58 | + Person person = entityManager.find( Person.class, 1 ); |
| 59 | + assertThat( person.getCountry().getCode() ).isEqualTo( "ITA" ); |
| 60 | + } |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + @Entity(name = "Person") |
| 65 | + @Table(name = "PERSON_TABLE") |
| 66 | + public static class Person { |
| 67 | + @Id |
| 68 | + private Integer id; |
| 69 | + |
| 70 | + private String name; |
| 71 | + |
| 72 | + @Embedded |
| 73 | + private Country country; |
| 74 | + |
| 75 | + public Person() { |
| 76 | + } |
| 77 | + |
| 78 | + public Person(Integer id, String name, Country country) { |
| 79 | + this.id = id; |
| 80 | + this.name = name; |
| 81 | + this.country = country; |
| 82 | + } |
| 83 | + |
| 84 | + public Integer getId() { |
| 85 | + return id; |
| 86 | + } |
| 87 | + |
| 88 | + public void setId(Integer id) { |
| 89 | + this.id = id; |
| 90 | + } |
| 91 | + |
| 92 | + public String getName() { |
| 93 | + return name; |
| 94 | + } |
| 95 | + |
| 96 | + public void setName(String name) { |
| 97 | + this.name = name; |
| 98 | + } |
| 99 | + |
| 100 | + public Country getCountry() { |
| 101 | + return country; |
| 102 | + } |
| 103 | + |
| 104 | + public void setCountry(Country country) { |
| 105 | + this.country = country; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Embeddable |
| 110 | + public static class Country { |
| 111 | + |
| 112 | + private String country; |
| 113 | + |
| 114 | + private String code; |
| 115 | + |
| 116 | + public Country() { |
| 117 | + } |
| 118 | + |
| 119 | + public Country(String country, String code) { |
| 120 | + this.country = country; |
| 121 | + this.code = code; |
| 122 | + } |
| 123 | + |
| 124 | + public String getCountry() { |
| 125 | + return country; |
| 126 | + } |
| 127 | + |
| 128 | + public void setCountry(String country) { |
| 129 | + this.country = country; |
| 130 | + } |
| 131 | + |
| 132 | + public String getCode() { |
| 133 | + return code; |
| 134 | + } |
| 135 | + |
| 136 | + public void setCode(String code) { |
| 137 | + this.code = code; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments