|
| 1 | +package org.hibernate.models.annotations; |
| 2 | + |
| 3 | +import java.util.Date; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.hibernate.models.SourceModelTestHelper; |
| 7 | +import org.hibernate.models.internal.SourceModelBuildingContextImpl; |
| 8 | +import org.hibernate.models.spi.ClassDetails; |
| 9 | +import org.hibernate.models.spi.FieldDetails; |
| 10 | +import org.hibernate.models.spi.TypeDetails; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import org.jboss.jandex.Index; |
| 15 | + |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.Id; |
| 18 | +import jakarta.persistence.MapKeyClass; |
| 19 | +import jakarta.persistence.MapKeyColumn; |
| 20 | +import jakarta.persistence.MapKeyTemporal; |
| 21 | +import jakarta.persistence.OneToMany; |
| 22 | +import jakarta.persistence.TemporalType; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +public class MapKeyTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + void testWithJandex() { |
| 30 | + final Index index = SourceModelTestHelper.buildJandexIndex( |
| 31 | + School.class, |
| 32 | + Person.class |
| 33 | + ); |
| 34 | + testFieldsAreResolved( index ); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testWithoutJandex() { |
| 39 | + testFieldsAreResolved( null ); |
| 40 | + } |
| 41 | + |
| 42 | + void testFieldsAreResolved(Index index) { |
| 43 | + final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext( |
| 44 | + index, |
| 45 | + School.class, |
| 46 | + Person.class |
| 47 | + ); |
| 48 | + |
| 49 | + final ClassDetails schoolClassDetails = buildingContext.getClassDetailsRegistry().getClassDetails( School.class.getName() ); |
| 50 | + |
| 51 | + final FieldDetails idField = schoolClassDetails.findFieldByName( "id" ); |
| 52 | + final TypeDetails idFieldType = idField.getType(); |
| 53 | + assertThat( idFieldType.isResolved() ).isTrue(); |
| 54 | + |
| 55 | + FieldDetails mapKeyField = schoolClassDetails.findFieldByName( "studentsByDate" ); |
| 56 | + TypeDetails typeDetails = mapKeyField.resolveRelativeType( schoolClassDetails ); |
| 57 | + assertThat( typeDetails.isResolved() ).isTrue(); |
| 58 | + |
| 59 | + FieldDetails mapKeyField2 = schoolClassDetails.findFieldByName( "teachersByDate" ); |
| 60 | + TypeDetails typeDetails2 = mapKeyField2.resolveRelativeType( schoolClassDetails ); |
| 61 | + assertThat( typeDetails2.isResolved() ).isFalse(); |
| 62 | + } |
| 63 | + |
| 64 | + @Entity |
| 65 | + public static class School<T> { |
| 66 | + @Id |
| 67 | + private int id; |
| 68 | + |
| 69 | + @OneToMany(mappedBy = "school") |
| 70 | + @MapKeyClass(Date.class) |
| 71 | + @MapKeyColumn(name = "THE_DATE") |
| 72 | + @MapKeyTemporal(TemporalType.DATE) |
| 73 | + private Map<Object, Person> studentsByDate; |
| 74 | + |
| 75 | + @OneToMany(mappedBy = "school") |
| 76 | + @MapKeyClass(Date.class) |
| 77 | + @MapKeyColumn(name = "THE_DATE") |
| 78 | + @MapKeyTemporal(TemporalType.DATE) |
| 79 | + private Map<T, Person> teachersByDate; |
| 80 | + } |
| 81 | + |
| 82 | + @Entity |
| 83 | + public static class Person { |
| 84 | + @Id |
| 85 | + private int id; |
| 86 | + } |
| 87 | +} |
0 commit comments