public Type getReferencedPropertyType(String className, String propertyName) throws MappingException { return getMetamodel().entityPersister( className ).getPropertyType( propertyName ); }
/** * Get the property type of a given property in the specified entity. * * @param session the session * @param entityName the entity name * @param propertyName the property name * @return the property type of the property or {@code null} if the property name isn't found. */ private Type getPropertyType(SessionImplementor session, String entityName, String propertyName) { // rather than rely on QueryException from calling getPropertyType(), this allows a non-failure way // to determine whether to return null or lookup the value safely. final EntityPersister persister = session.getSessionFactory().getMetamodel().entityPersister( entityName ); for ( String name : persister.getPropertyNames() ) { if ( name.equals( propertyName ) ) { return persister.getPropertyType( propertyName ); } } return null; } }
public CachedNaturalId(EntityPersister persister, Object[] values) { this.persister = persister; this.values = values; final int prime = 31; int hashCodeCalculation = 1; hashCodeCalculation = prime * hashCodeCalculation + persister.hashCode(); final int[] naturalIdPropertyIndexes = persister.getNaturalIdentifierProperties(); naturalIdTypes = new Type[ naturalIdPropertyIndexes.length ]; int i = 0; for ( int naturalIdPropertyIndex : naturalIdPropertyIndexes ) { final Type type = persister.getPropertyType( persister.getPropertyNames()[ naturalIdPropertyIndex ] ); naturalIdTypes[i] = type; final int elementHashCode = values[i] == null ? 0 :type.getHashCode( values[i], persister.getFactory() ); hashCodeCalculation = prime * hashCodeCalculation + elementHashCode; i++; } this.hashCode = hashCodeCalculation; }
private Constructor getConstructor() { Type studentNametype = sessionFactory() .getEntityPersister( Student.class.getName() ) .getPropertyType( "name" ); return ReflectHelper.getConstructor( Student.class, new Type[] {StandardBasicTypes.LONG, studentNametype} ); } };
private int getSubclassPropertyTableNumber(String propertyName, String entityName) { // When there are duplicated property names in the subclasses // then propertyMapping.toType( propertyName ) may return an // incorrect Type. To ensure correct results, lookup the property type // using the concrete EntityPersister with the specified entityName // (since the concrete EntityPersister cannot have duplicated property names). final EntityPersister concreteEntityPersister; if ( getEntityName().equals( entityName ) ) { concreteEntityPersister = this; } else { concreteEntityPersister = getFactory().getMetamodel().entityPersister( entityName ); } Type type = concreteEntityPersister.getPropertyType( propertyName ); if ( type.isAssociationType() && ( (AssociationType) type ).useLHSPrimaryKey() ) { return 0; } final Integer tabnum = propertyTableNumbersByNameAndSubclass.get( entityName + '.' + propertyName ); return tabnum == null ? 0 : tabnum; }
@Test public void checkConverterMutabilityPlans() { final EntityPersister persister = sessionFactory().getEntityPersister( SomeEntity.class.getName() ); assertFalse( persister.getPropertyType( "number" ).isMutable() ); assertTrue( persister.getPropertyType( "name" ).isMutable() ); }
private boolean isNonIdentifierWhereConditionsRequired(String entityName, String propertyName, SessionImplementor session) { final Type propertyType = session.getSessionFactory().getMetamodel().entityPersister( entityName ).getPropertyType( propertyName ); if ( propertyType.isCollectionType() ) { final CollectionType collectionType = (CollectionType) propertyType; final Type collectionElementType = collectionType.getElementType( session.getSessionFactory() ); if ( collectionElementType instanceof ComponentType ) { // required for Embeddables return true; } else if ( collectionElementType instanceof MaterializedClobType || collectionElementType instanceof MaterializedNClobType ) { // for Map<> using @Lob annotations return collectionType instanceof MapType; } } return false; } }
protected void prepareTest() throws Exception { super.prepareTest(); isContractPartiesInverse = sessionFactory().getCollectionPersister( Contract.class.getName() + ".parties" ).isInverse(); try { sessionFactory().getEntityPersister( Party.class.getName() ).getPropertyType( "contract" ); isContractPartiesBidirectional = true; } catch ( QueryException ex) { isContractPartiesBidirectional = false; } try { sessionFactory().getEntityPersister( ContractVariation.class.getName() ).getPropertyType( "contract" ); isContractVariationsBidirectional = true; } catch ( QueryException ex) { isContractVariationsBidirectional = false; } isContractVersioned = sessionFactory().getEntityPersister( Contract.class.getName() ).isVersioned(); }
final Type associationType = fetch.getAssociation().getOwner().getPropertyType( fetch.getAssociation().getAssociationPath() ); if ( associationType.isCollectionType() ) { LOG.tracev( "Handling request to add collection fetch [{0}]", fetchAssociactionRole );
final Type propertyType = persister.getPropertyType( propertyName ); if ( propertyType.isEntityType() ) { foreignValueSourceType = (EntityType) persister.getPropertyType( PropertyPath.IDENTIFIER_MAPPER_PROPERTY + "." + propertyName );
if ( persister.getPropertyType( propertyName ) != null ) { return (PropertyMapping) persister;
@Test @SuppressWarnings("unchecked") public void testPropertyAccessIgnoresStaticFields() { // verify that the entity persister is configured with property intValue as an Integer rather than // using the static field reference and determining the type to be String. assertTrue( sessionFactory() .getMetamodel() .entityPersister( FooEntity.class ) .getPropertyType( "intValue" ) .getReturnedClass() .isAssignableFrom( Integer.class ) ); } }
protected final Object getIdentifier(Object value, SharedSessionContractImplementor session) throws HibernateException { if ( isReferenceToPrimaryKey() || uniqueKeyPropertyName == null ) { return ForeignKeys.getEntityIdentifierIfNotUnsaved( getAssociatedEntityName(), value, session ); //tolerates nulls } else if ( value == null ) { return null; } else { EntityPersister entityPersister = getAssociatedEntityPersister( session.getFactory() ); Object propertyValue = entityPersister.getPropertyValue( value, uniqueKeyPropertyName ); // We now have the value of the property-ref we reference. However, // we need to dig a little deeper, as that property might also be // an entity type, in which case we need to resolve its identitifier Type type = entityPersister.getPropertyType( uniqueKeyPropertyName ); if ( type.isEntityType() ) { propertyValue = ( (EntityType) type ).getIdentifier( propertyValue, session ); } return propertyValue; } }
@Test public void testSimpleConvertUsage() throws MalformedURLException { final EntityPersister ep = sessionFactory().getEntityPersister( Entity1.class.getName() ); final Type theDatePropertyType = ep.getPropertyType( "theDate" ); final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class,
Type associationType = owner.getPropertyType( mappingFetch.getAssociation() ); if ( associationType == null || !associationType.isAssociationType() ) { throw new HibernateException( "Fetch profile [" + fetchProfile.getName() + "] specified an invalid association" );
@Test public void testJPQLUpperAttributeValueBindParameterType() throws Exception { doInJPA( this::entityManagerFactory, entityManager -> { //tag::basic-attribute-converter-query-parameter-converter-object-example[] SessionFactory sessionFactory = entityManager.getEntityManagerFactory() .unwrap( SessionFactory.class ); MetamodelImplementor metamodelImplementor = (MetamodelImplementor) sessionFactory.getMetamodel(); Type captionType = metamodelImplementor .entityPersister( Photo.class.getName() ) .getPropertyType( "caption" ); Photo photo = (Photo) entityManager.createQuery( "select p " + "from Photo p " + "where upper(caption) = upper(:caption) ", Photo.class ) .unwrap( Query.class ) .setParameter( "caption", new Caption("Nicolae Grigorescu"), captionType) .getSingleResult(); //end::basic-attribute-converter-query-parameter-converter-object-example[] assertEquals( "Dorobantul", photo.getName() ); } ); }
@Test public void testHHH10128() { final Metadata metadata = new MetadataSources( ssr ) .addAnnotatedClass( Entity.class ) .addAnnotatedClass( DescriptionEntity.class ) .addAnnotatedClass( AddressLevel.class ) .buildMetadata(); final PersistentClass addressLevelBinding = metadata.getEntityBinding( AddressLevel.class.getName() ); final Property natureProperty = addressLevelBinding.getProperty( "nature" ); CustomType customType = assertTyping( CustomType.class, natureProperty.getType() ); EnumType enumType = assertTyping( EnumType.class, customType.getUserType() ); assertEquals( Types.VARCHAR, enumType.sqlTypes()[0] ); SessionFactoryImplementor sf = (SessionFactoryImplementor) metadata.buildSessionFactory(); try { EntityPersister p = sf.getEntityPersister( AddressLevel.class.getName() ); CustomType runtimeType = assertTyping( CustomType.class, p.getPropertyType( "nature" ) ); EnumType runtimeEnumType = assertTyping( EnumType.class, runtimeType.getUserType() ); assertEquals( Types.VARCHAR, runtimeEnumType.sqlTypes()[0] ); } finally { sf.close(); } }
final AttributeConverterTypeAdapter geometryAttributeType = assertTyping( AttributeConverterTypeAdapter.class, entityPersister.getPropertyType( "geometry" ) );
private void assertEnumProperty(Class<?> entityClass, Class<?> typeClass, String propertyName, EnumType expectedType) { doInJPA( this::entityManagerFactory, entityManager -> { final SessionFactoryImplementor sessionFactory = entityManager.unwrap( SessionImplementor.class ).getSessionFactory(); final EntityPersister entityPersister = sessionFactory.getMetamodel().entityPersister( entityClass ); final EnversService enversService = sessionFactory.getServiceRegistry().getService( EnversService.class ); final String entityName = entityPersister.getEntityName(); final String auditEntityName = enversService.getAuditEntitiesConfiguration().getAuditEntityName( entityName ); final EntityPersister auditedEntityPersister = sessionFactory.getMetamodel().entityPersister( auditEntityName ); final org.hibernate.type.Type propertyType = auditedEntityPersister.getPropertyType( propertyName ); assertTyping( CustomType.class, propertyType ); final UserType userType = ( (CustomType) propertyType ).getUserType(); assertTyping( typeClass, userType ); assertTyping( org.hibernate.type.EnumType.class, userType ); switch ( expectedType ) { case STRING: assertTrue( !( (org.hibernate.type.EnumType) userType ).isOrdinal() ); break; default: assertTrue( ( (org.hibernate.type.EnumType) userType ).isOrdinal() ); break; } } ); } }
public Type getReferencedPropertyType(String className, String propertyName) throws MappingException { return getEntityPersister( className ).getPropertyType( propertyName ); }