|
| 1 | +package org.hibernate.orm.test.annotations.collectionelement; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 7 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 8 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import jakarta.persistence.CollectionTable; |
| 14 | +import jakarta.persistence.Column; |
| 15 | +import jakarta.persistence.ElementCollection; |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.Id; |
| 18 | +import jakarta.persistence.JoinColumn; |
| 19 | +import jakarta.persistence.Table; |
| 20 | + |
| 21 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +@DomainModel( |
| 25 | + annotatedClasses = { |
| 26 | + ElementCollectionReferencingIdColumnsTest.Company.class, |
| 27 | + ElementCollectionReferencingIdColumnsTest.Employee.class, |
| 28 | + ElementCollectionReferencingIdColumnsTest.Phone.class, |
| 29 | + } |
| 30 | +) |
| 31 | +@SessionFactory |
| 32 | +@JiraKey("HHH-17695") |
| 33 | +public class ElementCollectionReferencingIdColumnsTest { |
| 34 | + private static final Long COMPANY_ID = 1L; |
| 35 | + |
| 36 | + private static final Long EMPLOYEE_ID = 3l; |
| 37 | + private static final String USER_ID = "4"; |
| 38 | + private static final Long EMPLOYEE_ID_2 = 5l; |
| 39 | + private static final String USER_ID_2 = "6"; |
| 40 | + |
| 41 | + private static final Long PHONE_ID = 7l; |
| 42 | + private static final Long PHONE_ID_2 = 8l; |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + public void setUp(SessionFactoryScope scope) { |
| 46 | + scope.inTransaction( |
| 47 | + session -> { |
| 48 | + |
| 49 | + Phone phone = new Phone( PHONE_ID, "1234567", EMPLOYEE_ID ); |
| 50 | + Phone phone2 = new Phone( PHONE_ID_2, "8910112", EMPLOYEE_ID_2 ); |
| 51 | + |
| 52 | + Employee employee = new Employee( EMPLOYEE_ID, USER_ID, "and", COMPANY_ID ); |
| 53 | + Employee employee2 = new Employee( EMPLOYEE_ID_2, USER_ID_2, "and", COMPANY_ID ); |
| 54 | + Company company = new Company( COMPANY_ID, "acme" ); |
| 55 | + |
| 56 | + session.persist( company ); |
| 57 | + |
| 58 | + session.persist( employee ); |
| 59 | + session.persist( employee2 ); |
| 60 | + |
| 61 | + session.persist( phone ); |
| 62 | + session.persist( phone2 ); |
| 63 | + } |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testSelect(SessionFactoryScope scope) { |
| 69 | + scope.inTransaction( |
| 70 | + session -> { |
| 71 | + List<Company> companies = session.createQuery( |
| 72 | + "select c from Company c left join fetch c.employeeUserIds", |
| 73 | + Company.class |
| 74 | + ) |
| 75 | + .getResultList(); |
| 76 | + assertThat( companies.size() ).isEqualTo( 1 ); |
| 77 | + Company company = companies.get( 0 ); |
| 78 | + assertThat( company.getEmployeeUserIds().size() ).isEqualTo( 2 ); |
| 79 | + assertTrue( company.getEmployeeUserIds().contains( USER_ID ) ); |
| 80 | + assertTrue( company.getEmployeeUserIds().contains( USER_ID_2 ) ); |
| 81 | + } |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + @Entity(name = "Company") |
| 86 | + @Table(name = "COMPANY") |
| 87 | + public static class Company { |
| 88 | + |
| 89 | + @Id |
| 90 | + private Long id; |
| 91 | + |
| 92 | + private String name; |
| 93 | + |
| 94 | + @ElementCollection |
| 95 | + @CollectionTable(name = "EMPLOYEE", |
| 96 | + joinColumns = @JoinColumn(name = "COMPANY_ID")) |
| 97 | + @Column(name = "USER_ID") |
| 98 | + private Set<String> employeeUserIds; |
| 99 | + |
| 100 | + public Company() { |
| 101 | + } |
| 102 | + |
| 103 | + public Company(Long id, String name) { |
| 104 | + this.id = id; |
| 105 | + this.name = name; |
| 106 | + } |
| 107 | + |
| 108 | + public Long getId() { |
| 109 | + return id; |
| 110 | + } |
| 111 | + |
| 112 | + public void setId(Long id) { |
| 113 | + this.id = id; |
| 114 | + } |
| 115 | + |
| 116 | + public String getName() { |
| 117 | + return name; |
| 118 | + } |
| 119 | + |
| 120 | + public void setName(String name) { |
| 121 | + this.name = name; |
| 122 | + } |
| 123 | + |
| 124 | + public Set<String> getEmployeeUserIds() { |
| 125 | + return employeeUserIds; |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + @Entity(name = "Employee") |
| 131 | + @Table(name = "EMPLOYEE") |
| 132 | + public static class Employee { |
| 133 | + |
| 134 | + @Id |
| 135 | + private Long id; |
| 136 | + |
| 137 | + @Column(name = "USER_ID") |
| 138 | + private String userId; |
| 139 | + |
| 140 | + private String name; |
| 141 | + |
| 142 | + @Column(name = "COMPANY_ID") |
| 143 | + private Long companyId; |
| 144 | + |
| 145 | + @ElementCollection |
| 146 | + @CollectionTable(name = "PHONE", |
| 147 | + joinColumns = @JoinColumn(name = "EMPLOYEE_ID")) |
| 148 | + @Column(name = "PHONE_NUMBER") |
| 149 | + private List<String> phoneNumbers; |
| 150 | + |
| 151 | + public Employee() { |
| 152 | + } |
| 153 | + |
| 154 | + public Employee(Long id, String userId, String name, Long companyId) { |
| 155 | + this.id = id; |
| 156 | + this.userId = userId; |
| 157 | + this.name = name; |
| 158 | + this.companyId = companyId; |
| 159 | + } |
| 160 | + |
| 161 | + public Long getId() { |
| 162 | + return id; |
| 163 | + } |
| 164 | + |
| 165 | + public String getUserId() { |
| 166 | + return userId; |
| 167 | + } |
| 168 | + |
| 169 | + public void setUserId(String userId) { |
| 170 | + this.userId = userId; |
| 171 | + } |
| 172 | + |
| 173 | + public String getName() { |
| 174 | + return name; |
| 175 | + } |
| 176 | + |
| 177 | + public void setName(String name) { |
| 178 | + this.name = name; |
| 179 | + } |
| 180 | + |
| 181 | + public Long getCompanyId() { |
| 182 | + return companyId; |
| 183 | + } |
| 184 | + |
| 185 | + public void setCompanyId(Long companyId) { |
| 186 | + this.companyId = companyId; |
| 187 | + } |
| 188 | + |
| 189 | + public List<String> getPhoneNumbers() { |
| 190 | + return phoneNumbers; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + @Entity(name = "Phone") |
| 195 | + @Table(name = "PHONE") |
| 196 | + public static class Phone { |
| 197 | + |
| 198 | + @Id |
| 199 | + private Long id; |
| 200 | + |
| 201 | + @Column(name = "PHONE_NUMBER") |
| 202 | + private String phoneNumber; |
| 203 | + |
| 204 | + @Column(name = "EMPLOYEE_ID") |
| 205 | + private Long employeeId; |
| 206 | + |
| 207 | + public Long getId() { |
| 208 | + return id; |
| 209 | + } |
| 210 | + |
| 211 | + public Phone() { |
| 212 | + } |
| 213 | + |
| 214 | + public Phone(Long id, String phoneNumber, Long employeeId) { |
| 215 | + this.id = id; |
| 216 | + this.phoneNumber = phoneNumber; |
| 217 | + this.employeeId = employeeId; |
| 218 | + } |
| 219 | + |
| 220 | + public Long getEmployeeId() { |
| 221 | + return employeeId; |
| 222 | + } |
| 223 | + |
| 224 | + public void setEmployeeId(Long employeeId) { |
| 225 | + this.employeeId = employeeId; |
| 226 | + } |
| 227 | + |
| 228 | + } |
| 229 | +} |
0 commit comments