Skip to content

Commit

Permalink
The type of a Map field where the key is of type Object should result…
Browse files Browse the repository at this point in the history
… as resolved
  • Loading branch information
dreab8 committed Apr 3, 2024
1 parent b082475 commit 44df19d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
package org.hibernate.models.internal;

import java.util.List;
import java.util.Objects;

import org.hibernate.models.spi.ArrayTypeDetails;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassTypeDetails;
import org.hibernate.models.spi.ParameterizedTypeDetails;
import org.hibernate.models.spi.PrimitiveTypeDetails;
Expand Down Expand Up @@ -63,7 +61,7 @@ public Boolean caseParameterizedType(
SourceModelBuildingContext buildingContext) {
final List<TypeDetails> typeArgs = parameterizedType.getArguments();
for ( TypeDetails arg : typeArgs ) {
if ( !isBound( arg, buildingContext ) ) {
if ( !arg.isResolved() ) {
return false;
}
}
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/org/hibernate/models/annotations/MapKeyTest.java
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;
}
}

0 comments on commit 44df19d

Please sign in to comment.