Fix Attribute#getJavaType() returs also primitive types

This commit is contained in:
Andrea Boriero 2022-02-01 16:21:46 +01:00 committed by Andrea Boriero
parent a25758f519
commit 768e6873fd
11 changed files with 105 additions and 19 deletions

View File

@ -44,6 +44,7 @@ import org.hibernate.metamodel.model.domain.internal.EmbeddableTypeImpl;
import org.hibernate.metamodel.model.domain.internal.EntityTypeImpl;
import org.hibernate.metamodel.model.domain.internal.MappedSuperclassTypeImpl;
import org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl;
import org.hibernate.metamodel.model.domain.internal.PrimitiveBasicTypeImpl;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.spi.EntityJavaType;
@ -702,9 +703,11 @@ public class MetadataContext {
return (BasicDomainType<J>) basicDomainTypeMap.computeIfAbsent(
javaType,
jt -> {
final JavaTypeRegistry registry =
getTypeConfiguration()
.getJavaTypeRegistry();
final JavaTypeRegistry registry = getTypeConfiguration().getJavaTypeRegistry();
if ( javaType.isPrimitive() ) {
return new PrimitiveBasicTypeImpl<>( registry.resolveDescriptor( javaType ), javaType );
}
return new BasicTypeImpl<>( registry.resolveDescriptor( javaType ) );
}
);

View File

@ -17,7 +17,6 @@ import org.hibernate.metamodel.internal.MetadataContext;
import org.hibernate.metamodel.model.domain.DomainType;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
import org.hibernate.metamodel.model.domain.SimpleDomainType;
import org.hibernate.type.descriptor.java.JavaType;
/**
@ -61,6 +60,9 @@ public abstract class AbstractAttribute<D,J,B> implements PersistentAttribute<D,
@Override
public Class<J> getJavaType() {
if ( valueType instanceof BasicTypeImpl ) {
return ( (BasicTypeImpl) valueType ).getJavaType();
}
return attributeJtd.getJavaTypeClass();
}

View File

@ -35,6 +35,7 @@ public class BasicTypeImpl<J> implements BasicDomainType<J>, Serializable {
return javaType;
}
@Override
public Class<J> getJavaType() {
return this.getExpressibleJavaType().getJavaTypeClass();
}

View File

@ -0,0 +1,24 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.metamodel.model.domain.internal;
import org.hibernate.type.descriptor.java.JavaType;
public class PrimitiveBasicTypeImpl<J> extends BasicTypeImpl<J> {
private final Class<J> javaTypeClass;
public PrimitiveBasicTypeImpl(JavaType<J> javaType, Class<J> javaTypeClass) {
super( javaType );
assert javaTypeClass.isPrimitive();
this.javaTypeClass = javaTypeClass;
}
@Override
public Class<J> getJavaType() {
return javaTypeClass;
}
}

View File

@ -108,7 +108,7 @@ public class SingularAttributeImpl<D,J>
@Override
public Class<J> getBindableJavaType() {
return getExpressibleJavaType().getJavaTypeClass();
return getJavaType();
}
@Override

View File

@ -22,10 +22,10 @@ import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
*
* @author Steve Ebersole
*/
public class FloatTypeDescriptor extends AbstractClassJavaType<Float> implements PrimitiveJavaType<Float> {
public static final FloatTypeDescriptor INSTANCE = new FloatTypeDescriptor();
public class FloatJavaType extends AbstractClassJavaType<Float> implements PrimitiveJavaType<Float> {
public static final FloatJavaType INSTANCE = new FloatJavaType();
public FloatTypeDescriptor() {
public FloatJavaType() {
super( Float.class );
}

View File

@ -45,7 +45,7 @@ import org.hibernate.type.descriptor.java.CurrencyJavaType;
import org.hibernate.type.descriptor.java.DateJavaType;
import org.hibernate.type.descriptor.java.DoubleJavaType;
import org.hibernate.type.descriptor.java.DurationJavaType;
import org.hibernate.type.descriptor.java.FloatTypeDescriptor;
import org.hibernate.type.descriptor.java.FloatJavaType;
import org.hibernate.type.descriptor.java.InetAddressJavaType;
import org.hibernate.type.descriptor.java.InstantJavaType;
import org.hibernate.type.descriptor.java.IntegerJavaType;
@ -104,7 +104,7 @@ public class JavaTypeBaseline {
primePrimitive( target, ShortJavaType.INSTANCE );
primePrimitive( target, IntegerJavaType.INSTANCE );
primePrimitive( target, LongJavaType.INSTANCE );
primePrimitive( target, FloatTypeDescriptor.INSTANCE );
primePrimitive( target, FloatJavaType.INSTANCE );
primePrimitive( target, DoubleJavaType.INSTANCE );
target.addBaselineDescriptor( ObjectJavaType.INSTANCE );

View File

@ -0,0 +1,55 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.jpa.compliance;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.ManagedType;
import jakarta.persistence.metamodel.Metamodel;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@Jpa(
annotatedClasses = GetJavaTypeTest.Person.class
)
public class GetJavaTypeTest {
@Test
public void getJavaType(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
Metamodel metaModel = entityManager.getMetamodel();
if ( metaModel != null ) {
ManagedType<Person> mTypeOrder = metaModel.managedType( Person.class );
assertNotNull( mTypeOrder );
Attribute<Person, ?> attrib = mTypeOrder.getDeclaredAttribute( "age" );
assertNotNull( attrib );
Class pAttribJavaType = attrib.getJavaType();
assertEquals( "int", pAttribJavaType.getName() );
}
}
);
}
@Entity(name = "Person")
@Table(name = "PERSON_TABLE")
public static class Person {
@Id
private int id;
private String name;
private int age;
}
}

View File

@ -26,6 +26,7 @@ import jakarta.persistence.metamodel.Type;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.orm.test.legacy.I;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
@ -226,8 +227,8 @@ public class MetadataTest {
"temperature",
Integer.class
);
assertEquals( Integer.class, singularAttribute.getBindableJavaType() );
assertEquals( Integer.class, singularAttribute.getType().getJavaType() );
assertEquals( int.class, singularAttribute.getBindableJavaType() );
assertEquals( int.class, singularAttribute.getType().getJavaType() );
assertEquals(Bindable.BindableType.SINGULAR_ATTRIBUTE, singularAttribute.getBindableType());
assertFalse(singularAttribute.isId());
assertFalse(singularAttribute.isOptional());
@ -238,7 +239,7 @@ public class MetadataTest {
assertEquals("temperature", attribute.getName());
assertEquals(Fridge.class, attribute.getDeclaringType().getJavaType());
assertEquals(Attribute.PersistentAttributeType.BASIC, attribute.getPersistentAttributeType());
assertEquals( Integer.class, attribute.getJavaType() );
assertEquals( int.class, attribute.getJavaType() );
assertFalse(attribute.isAssociation());
assertFalse(attribute.isCollection());

View File

@ -62,7 +62,7 @@ public class StaticMetadataTest {
assertTrue( Animal_.id.isId() );
assertEquals( Long.class, Animal_.id.getJavaType() );
assertNotNull( Animal_.legNbr );
assertEquals( Integer.class, Animal_.legNbr.getJavaType() );
assertEquals( int.class, Animal_.legNbr.getJavaType() );
// Cat (hierarchy)
assertNotNull( Cat_.id );
@ -81,9 +81,9 @@ public class StaticMetadataTest {
assertNotNull( Fridge_.temperature );
assertEquals( "temperature", Fridge_.temperature.getName() );
assertEquals( Fridge.class, Fridge_.temperature.getDeclaringType().getJavaType() );
assertEquals( Integer.class, Fridge_.temperature.getJavaType() );
assertEquals( Integer.class, Fridge_.temperature.getBindableJavaType() );
assertEquals( Integer.class, Fridge_.temperature.getType().getJavaType() );
assertEquals( int.class, Fridge_.temperature.getJavaType() );
assertEquals( int.class, Fridge_.temperature.getBindableJavaType() );
assertEquals( int.class, Fridge_.temperature.getType().getJavaType() );
assertEquals( Bindable.BindableType.SINGULAR_ATTRIBUTE, Fridge_.temperature.getBindableType() );
assertEquals( Type.PersistenceType.BASIC, Fridge_.temperature.getType().getPersistenceType() );
assertEquals( Attribute.PersistentAttributeType.BASIC, Fridge_.temperature.getPersistentAttributeType() );

View File

@ -40,7 +40,7 @@ import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.descriptor.java.BigDecimalJavaType;
import org.hibernate.type.descriptor.java.FloatTypeDescriptor;
import org.hibernate.type.descriptor.java.FloatJavaType;
import org.hibernate.type.descriptor.java.PrimitiveByteArrayJavaType;
import org.hibernate.type.descriptor.java.StringJavaType;
import org.hibernate.type.descriptor.jdbc.BinaryJdbcType;
@ -576,7 +576,7 @@ public class NativeQueryResultTypeAutoDiscoveryTest {
public static final String NAME = "float_as_real";
public FloatAsRealType() {
super( RealJdbcType.INSTANCE, FloatTypeDescriptor.INSTANCE );
super( RealJdbcType.INSTANCE, FloatJavaType.INSTANCE );
}
@Override