/** * Creates a new {@link JpaMetamodelEntityInformation} for the given domain class and {@link Metamodel}. * * @param domainClass must not be {@literal null}. * @param metamodel must not be {@literal null}. */ public JpaMetamodelEntityInformation(Class<T> domainClass, Metamodel metamodel) { super(domainClass); Assert.notNull(metamodel, "Metamodel must not be null!"); this.metamodel = metamodel; ManagedType<T> type = metamodel.managedType(domainClass); if (type == null) { throw new IllegalArgumentException("The given domain class can not be found in the given Metamodel!"); } this.entityName = type instanceof EntityType ? ((EntityType<?>) type).getName() : null; if (!(type instanceof IdentifiableType)) { throw new IllegalArgumentException("The given domain class does not contain an id attribute!"); } IdentifiableType<T> identifiableType = (IdentifiableType<T>) type; this.idMetadata = new IdMetadata<T>(identifiableType); this.versionAttribute = findVersionAttribute(identifiableType, metamodel); }
@Nullable private Class<?> tryExtractIdTypeWithFallbackToIdTypeLookup() { try { Type<?> idType2 = type.getIdType(); return idType2 == null ? fallbackIdTypeLookup(type) : idType2.getJavaType(); } catch (IllegalStateException e) { // see https://hibernate.onjira.com/browse/HHH-6951 return fallbackIdTypeLookup(type); } }
public Class<?> getType() { if (idType != null) { return idType; } // lazy initialization of idType field with tolerable benign data-race this.idType = tryExtractIdTypeWithFallbackToIdTypeLookup(); if (this.idType == null) { throw new IllegalStateException("Cannot resolve Id type from " + type); } return this.idType; }
@Nullable @SuppressWarnings("unchecked") public ID getId(T entity) { BeanWrapper entityWrapper = new DirectFieldAccessFallbackBeanWrapper(entity); if (idMetadata.hasSimpleId()) { return (ID) entityWrapper.getPropertyValue(idMetadata.getSimpleIdAttribute().getName()); } BeanWrapper idWrapper = new IdentifierDerivingDirectFieldAccessFallbackBeanWrapper(idMetadata.getType(), metamodel); boolean partialIdValueFound = false; for (SingularAttribute<? super T, ?> attribute : idMetadata) { Object propertyValue = entityWrapper.getPropertyValue(attribute.getName()); if (propertyValue != null) { partialIdValueFound = true; } idWrapper.setPropertyValue(attribute.getName(), propertyValue); } return partialIdValueFound ? (ID) idWrapper.getWrappedInstance() : null; }
/** * Creates a new {@link JpaMetamodelEntityInformation} for the given domain class and {@link Metamodel}. * * @param domainClass must not be {@literal null}. * @param metamodel must not be {@literal null}. */ public JpaMetamodelEntityInformation(Class<T> domainClass, Metamodel metamodel) { super(domainClass); Assert.notNull(metamodel, "Metamodel must not be null!"); this.metamodel = metamodel; ManagedType<T> type = metamodel.managedType(domainClass); if (type == null) { throw new IllegalArgumentException("The given domain class can not be found in the given Metamodel!"); } this.entityName = type instanceof EntityType ? ((EntityType<?>) type).getName() : null; if (!(type instanceof IdentifiableType)) { throw new IllegalArgumentException("The given domain class does not contain an id attribute!"); } IdentifiableType<T> identifiableType = (IdentifiableType<T>) type; this.idMetadata = new IdMetadata<T>(identifiableType); this.versionAttribute = findVersionAttribute(identifiableType, metamodel); }
public boolean hasCompositeId() { return !idMetadata.hasSimpleId(); }
public SingularAttribute<? super T, ?> getIdAttribute() { return idMetadata.getSimpleIdAttribute(); }
@SuppressWarnings("unchecked") public Class<ID> getIdType() { return (Class<ID>) idMetadata.getType(); }
@Nullable private Class<?> tryExtractIdTypeWithFallbackToIdTypeLookup() { try { Type<?> idType2 = type.getIdType(); return idType2 == null ? fallbackIdTypeLookup(type) : idType2.getJavaType(); } catch (IllegalStateException e) { // see https://hibernate.onjira.com/browse/HHH-6951 return fallbackIdTypeLookup(type); } }
public Class<?> getType() { if (idType != null) { return idType; } // lazy initialization of idType field with tolerable benign data-race this.idType = tryExtractIdTypeWithFallbackToIdTypeLookup(); if (this.idType == null) { throw new IllegalStateException("Cannot resolve Id type from " + type); } return this.idType; }
@Override @Nullable @SuppressWarnings("unchecked") public ID getId(T entity) { BeanWrapper entityWrapper = new DirectFieldAccessFallbackBeanWrapper(entity); if (idMetadata.hasSimpleId()) { return (ID) entityWrapper.getPropertyValue(idMetadata.getSimpleIdAttribute().getName()); } BeanWrapper idWrapper = new IdentifierDerivingDirectFieldAccessFallbackBeanWrapper(idMetadata.getType(), metamodel); boolean partialIdValueFound = false; for (SingularAttribute<? super T, ?> attribute : idMetadata) { Object propertyValue = entityWrapper.getPropertyValue(attribute.getName()); if (propertyValue != null) { partialIdValueFound = true; } idWrapper.setPropertyValue(attribute.getName(), propertyValue); } return partialIdValueFound ? (ID) idWrapper.getWrappedInstance() : null; }
@Override @SuppressWarnings("unchecked") public Class<ID> getIdType() { return (Class<ID>) idMetadata.getType(); }
@Override public SingularAttribute<? super T, ?> getIdAttribute() { return idMetadata.getSimpleIdAttribute(); }
@Override public boolean hasCompositeId() { return !idMetadata.hasSimpleId(); }