-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from dreab8/mapkey_object_as_key
The type of a Map field where the key is of type Object should result as resolved
- Loading branch information
Showing
2 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/test/java/org/hibernate/models/annotations/MapKeyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.hibernate.models.annotations; | ||
|
||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
import org.hibernate.models.SourceModelTestHelper; | ||
import org.hibernate.models.internal.SourceModelBuildingContextImpl; | ||
import org.hibernate.models.spi.ClassDetails; | ||
import org.hibernate.models.spi.FieldDetails; | ||
import org.hibernate.models.spi.TypeDetails; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.jboss.jandex.Index; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.MapKeyClass; | ||
import jakarta.persistence.MapKeyColumn; | ||
import jakarta.persistence.MapKeyTemporal; | ||
import jakarta.persistence.OneToMany; | ||
import jakarta.persistence.TemporalType; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class MapKeyTest { | ||
|
||
@Test | ||
void testWithJandex() { | ||
final Index index = SourceModelTestHelper.buildJandexIndex( | ||
School.class, | ||
Person.class | ||
); | ||
testFieldsAreResolved( index ); | ||
} | ||
|
||
@Test | ||
void testWithoutJandex() { | ||
testFieldsAreResolved( null ); | ||
} | ||
|
||
void testFieldsAreResolved(Index index) { | ||
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext( | ||
index, | ||
School.class, | ||
Person.class | ||
); | ||
|
||
final ClassDetails schoolClassDetails = buildingContext.getClassDetailsRegistry().getClassDetails( School.class.getName() ); | ||
|
||
final FieldDetails idField = schoolClassDetails.findFieldByName( "id" ); | ||
final TypeDetails idFieldType = idField.getType(); | ||
assertThat( idFieldType.isResolved() ).isTrue(); | ||
|
||
FieldDetails mapKeyField = schoolClassDetails.findFieldByName( "studentsByDate" ); | ||
TypeDetails typeDetails = mapKeyField.resolveRelativeType( schoolClassDetails ); | ||
assertThat( typeDetails.isResolved() ).isTrue(); | ||
|
||
FieldDetails mapKeyField2 = schoolClassDetails.findFieldByName( "teachersByDate" ); | ||
TypeDetails typeDetails2 = mapKeyField2.resolveRelativeType( schoolClassDetails ); | ||
assertThat( typeDetails2.isResolved() ).isFalse(); | ||
} | ||
|
||
@Entity | ||
public static class School<T> { | ||
@Id | ||
private int id; | ||
|
||
@OneToMany(mappedBy = "school") | ||
@MapKeyClass(Date.class) | ||
@MapKeyColumn(name = "THE_DATE") | ||
@MapKeyTemporal(TemporalType.DATE) | ||
private Map<Object, Person> studentsByDate; | ||
|
||
@OneToMany(mappedBy = "school") | ||
@MapKeyClass(Date.class) | ||
@MapKeyColumn(name = "THE_DATE") | ||
@MapKeyTemporal(TemporalType.DATE) | ||
private Map<T, Person> teachersByDate; | ||
} | ||
|
||
@Entity | ||
public static class Person { | ||
@Id | ||
private int id; | ||
} | ||
} |