Example usage for org.hibernate.persister.entity AbstractEntityPersister getEntityMetamodel

List of usage examples for org.hibernate.persister.entity AbstractEntityPersister getEntityMetamodel

Introduction

In this page you can find the example usage for org.hibernate.persister.entity AbstractEntityPersister getEntityMetamodel.

Prototype

public EntityMetamodel getEntityMetamodel() 

Source Link

Usage

From source file:com.aw.core.dao.meta.HbmUtil.java

License:Open Source License

public IdentifierProperty getTableColumnIdentifier(Class entityClass, String entityField) {
    AbstractEntityPersister entityPersister = (AbstractEntityPersister) sessionFactory
            .getClassMetadata(entityClass);
    if (entityField.equalsIgnoreCase(entityPersister.getIdentifierPropertyName()))
        return entityPersister.getEntityMetamodel().getIdentifierProperty();
    else/* w ww .ja  v  a2  s  .com*/
        return null;
}

From source file:com.aw.core.dao.meta.HbmUtil.java

License:Open Source License

public Boolean isUnsaved(Object entity) {
    AbstractEntityPersister entityPersister = (AbstractEntityPersister) sessionFactory
            .getClassMetadata(entity.getClass());
    String identifierPropertyName = entityPersister.getIdentifierPropertyName();
    //IdentifierProperty identifierProperty = entityPersister.getEntityMetamodel().getIdentifierProperty();
    Serializable entityIdValue = (Serializable) new BeanWrapperImpl(entity)
            .getPropertyValue(identifierPropertyName);
    IdentifierValue unSavedValue = entityPersister.getEntityMetamodel().getIdentifierProperty()
            .getUnsavedValue();//from  w w  w  .  ja  va2  s. c  o  m
    return unSavedValue.isUnsaved(entityIdValue);
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpa21Provider.java

License:Apache License

@Override
public boolean isOrphanRemoval(ManagedType<?> ownerType, String attributeName) {
    AbstractEntityPersister entityPersister = getEntityPersister(ownerType);
    if (entityPersister != null) {
        EntityMetamodel entityMetamodel = entityPersister.getEntityMetamodel();
        Integer index = entityMetamodel.getPropertyIndexOrNull(attributeName);
        if (index != null) {
            try {
                return (boolean) HAS_ORPHAN_DELETE_METHOD.invoke(entityMetamodel.getCascadeStyles()[index]);
            } catch (Exception ex) {
                throw new RuntimeException(
                        "Could not access orphan removal information. Please report your version of hibernate so we can provide support for it!",
                        ex);/*from  w  w  w.  j a v a 2s  .  co m*/
            }
        }
    }

    return false;
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpa21Provider.java

License:Apache License

@Override
public boolean isDeleteCascaded(ManagedType<?> ownerType, String attributeName) {
    AbstractEntityPersister entityPersister = getEntityPersister(ownerType);
    if (entityPersister != null) {
        EntityMetamodel entityMetamodel = entityPersister.getEntityMetamodel();
        Integer index = entityMetamodel.getPropertyIndexOrNull(attributeName);
        if (index != null) {
            try {
                return (boolean) DO_CASCADE_METHOD.invoke(entityMetamodel.getCascadeStyles()[index],
                        DELETE_CASCADE);
            } catch (Exception ex) {
                throw new RuntimeException(
                        "Could not access orphan removal information. Please report your version of hibernate so we can provide support for it!",
                        ex);/*  w w w. jav a 2  s .c  o  m*/
            }
        }
    }

    return false;
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

protected final QueryableCollection getCollectionPersister(ManagedType<?> ownerType, String attributeName) {
    AbstractEntityPersister entityPersister = getEntityPersister(ownerType);
    QueryableCollection collection;//  w w w  . j  a v a 2 s . c om
    do {
        String ownerTypeName = entityPersister.getName();
        StringBuilder sb = new StringBuilder(ownerTypeName.length() + attributeName.length() + 1);
        sb.append(ownerTypeName);
        sb.append('.');
        sb.append(attributeName);

        collection = (QueryableCollection) collectionPersisters.get(sb.toString());
        if (collection == null) {
            String superclass = entityPersister.getEntityMetamodel().getSuperclass();
            entityPersister = superclass == null ? null
                    : (AbstractEntityPersister) entityPersisters.get(superclass);
        }
    } while (collection == null && entityPersister != null);

    return collection;
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public boolean isForeignJoinColumn(EntityType<?> ownerType, String attributeName) {
    AbstractEntityPersister persister = getEntityPersister(ownerType);
    Type propertyType = persister.getPropertyType(attributeName);

    if (propertyType instanceof OneToOneType) {
        OneToOneType oneToOneType = (OneToOneType) propertyType;
        // It is foreign if there is a mapped by attribute
        // But as of Hibernate 5.4 we noticed that we have to treat nullable one-to-ones as "foreign" as well
        return (oneToOneType).getRHSUniqueKeyPropertyName() != null || isNullable(oneToOneType);
    }//from  w  w w  .j  a  va2 s .co  m

    // Every entity persister has "owned" properties on table number 0, others have higher numbers
    int tableNumber = persister.getSubclassPropertyTableNumber(attributeName);
    return tableNumber >= persister.getEntityMetamodel().getSubclassEntityNames().size();
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public Map<String, String> getWritableMappedByMappings(EntityType<?> inverseType, EntityType<?> ownerType,
        String attributeName, String inverseAttribute) {
    AbstractEntityPersister entityPersister = getEntityPersister(ownerType);
    int propertyIndex = entityPersister.getEntityMetamodel().getPropertyIndex(attributeName);
    Type propertyType = entityPersister.getPropertyTypes()[propertyIndex];
    org.hibernate.type.EntityType ownerPropertyType;
    if (propertyType instanceof CollectionType) {
        QueryableCollection persister = getCollectionPersister(ownerType, attributeName);
        AbstractEntityPersister inversePersister = getEntityPersister(inverseType);
        if (!persister.isInverse() && persister.getTableName().equals(inversePersister.getTableName())) {
            // We have a one-to-many relationship that has just join columns

            // Find properties for element columns in entityPersister
            // Map to properties for key columns of inverseType
            Set<String> elementAttributes = getColumnMatchingAttributeNames(entityPersister,
                    Arrays.asList((entityPersister.toColumns(attributeName))));
            Set<String> keyAttributes = removeIdentifierAccess(ownerType, elementAttributes, inverseType,
                    getColumnMatchingAttributeNames(inversePersister,
                            Arrays.asList(persister.getKeyColumnNames())));

            Map<String, String> mapping = new HashMap<>();
            Iterator<String> elemAttrIter = elementAttributes.iterator();
            Iterator<String> keyAttrIter = keyAttributes.iterator();

            while (elemAttrIter.hasNext()) {
                mapping.put(elemAttrIter.next(), keyAttrIter.next());
            }/*from  w  w  w  . j a  va2  s . c  o m*/

            if (mapping.isEmpty()) {
                throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#"
                        + attributeName + "' must be writable or the column must be part of the id!");
            }
            return mapping;
        } else {
            // We only support detection when the inverse collection is writable
            if (entityPersister.getEntityMetamodel().getPropertyInsertability()[propertyIndex]) {
                return null;
            }
            throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#"
                    + attributeName + "' must be writable!");
        }
    } else {
        // Either the mapped by property is writable
        if (entityPersister.getEntityMetamodel().getPropertyInsertability()[propertyIndex]) {
            return null;
        }
        // Or the columns of the mapped by property are part of the target id
        ownerPropertyType = (org.hibernate.type.EntityType) propertyType;
    }
    AbstractEntityPersister sourceType = (AbstractEntityPersister) entityPersisters
            .get(ownerPropertyType.getAssociatedEntityName());
    Type identifierType = ownerPropertyType.getIdentifierOrUniqueKeyType(entityPersister.getFactory());
    String sourcePropertyPrefix;
    String[] sourcePropertyNames;
    if (identifierType.isComponentType()) {
        ComponentType componentType = (ComponentType) identifierType;
        sourcePropertyPrefix = sourceType.getIdentifierPropertyName() + ".";
        sourcePropertyNames = componentType.getPropertyNames();
    } else {
        sourcePropertyPrefix = "";
        sourcePropertyNames = new String[] { sourceType.getIdentifierPropertyName() };
    }
    String[] targetColumnNames = entityPersister.getPropertyColumnNames(propertyIndex);
    Type targetIdType = entityPersister.getIdentifierType();
    if (targetIdType.isComponentType()) {
        ComponentType targetIdentifierType = (ComponentType) entityPersister.getIdentifierType();
        String targetPropertyPrefix = entityPersister.getIdentifierPropertyName() + ".";
        String[] identifierColumnNames = entityPersister.getIdentifierColumnNames();
        String[] targetIdentifierTypePropertyNames = targetIdentifierType.getPropertyNames();
        Map<String, String> mapping = new HashMap<>();
        for (int i = 0; i < targetColumnNames.length; i++) {
            for (int j = 0; j < identifierColumnNames.length; j++) {
                if (targetColumnNames[i].equals(identifierColumnNames[j])) {
                    mapping.put(sourcePropertyPrefix + sourcePropertyNames[i],
                            targetPropertyPrefix + targetIdentifierTypePropertyNames[j]);
                    break;
                }
            }
        }
        if (mapping.isEmpty()) {
            throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#"
                    + attributeName + "' must be writable or the column must be part of the id!");
        }
        return mapping;
    } else {
        String targetIdColumnName = entityPersister.getIdentifierColumnNames()[0];
        if (!targetIdColumnName.equals(targetColumnNames[0])) {
            throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#"
                    + attributeName + "' must be writable or the column must be part of the id!");
        }
        Map<String, String> mapping = new HashMap<>();
        mapping.put(sourcePropertyPrefix + sourcePropertyNames[0], entityPersister.getIdentifierPropertyName());
        return mapping;
    }
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public boolean isOrphanRemoval(ManagedType<?> ownerType, String attributeName) {
    AbstractEntityPersister entityPersister = getEntityPersister(ownerType);
    if (entityPersister != null) {
        EntityMetamodel entityMetamodel = entityPersister.getEntityMetamodel();
        Integer index = entityMetamodel.getPropertyIndexOrNull(attributeName);
        if (index != null) {
            return entityMetamodel.getCascadeStyles()[index].hasOrphanDelete();
        }/*from  w  w  w .j av a2  s.  c  om*/
    }

    return false;
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public boolean isDeleteCascaded(ManagedType<?> ownerType, String attributeName) {
    AbstractEntityPersister entityPersister = getEntityPersister(ownerType);
    if (entityPersister != null) {
        EntityMetamodel entityMetamodel = entityPersister.getEntityMetamodel();
        Integer index = entityMetamodel.getPropertyIndexOrNull(attributeName);
        if (index != null) {
            return entityMetamodel.getCascadeStyles()[index].doCascade(CascadingAction.DELETE);
        }//from w  w  w  .jav a  2 s  . c om
    }

    return false;
}

From source file:com.evolveum.midpoint.repo.sql.util.RUtil.java

License:Apache License

private static void fixCompositeIdentifierInMetaModel(SessionFactory sessionFactory, Class clazz) {
    ClassMetadata classMetadata = sessionFactory.getClassMetadata(clazz);
    if (classMetadata instanceof AbstractEntityPersister) {
        AbstractEntityPersister persister = (AbstractEntityPersister) classMetadata;
        EntityMetamodel model = persister.getEntityMetamodel();
        IdentifierProperty identifier = model.getIdentifierProperty();

        try {/*from www.  j  a va  2s  .  c  om*/
            Field field = IdentifierProperty.class.getDeclaredField("hasIdentifierMapper");
            field.setAccessible(true);
            field.set(identifier, true);
            field.setAccessible(false);
        } catch (Exception ex) {
            throw new SystemException(
                    "Attempt to fix entity meta model with hack failed, reason: " + ex.getMessage(), ex);
        }
    }
}