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

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

Introduction

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

Prototype

public String[] getPropertyNames() 

Source Link

Usage

From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java

License:Open Source License

/**
 * Get the inverse of a one-to-many role
 * @param entityName The entity owning the role
 * @param rName The role name//from  ww  w. j  a  v a2s  . co m
 * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role
 */
private String[] getInverseOfCollectionRole(String entityName, String rName) {
    String[] result = new String[2];

    SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory();
    AbstractCollectionPersister parentMeta = (AbstractCollectionPersister) sessFact
            .getCollectionMetadata(entityName + "." + rName);
    if (parentMeta == null) { // Could be inherited -- search through superclasses
        while (parentMeta == null) {
            Class<?> cls = null;
            if (getMetaEntity().getEntityType() == MetaEntity.EntityType.POJO)
                cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.POJO);
            else
                cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.MAP);
            Class<?> superCls = cls.getSuperclass();
            if (superCls.getName().equals("java.lang.Object"))
                throw new RuntimeException(
                        "Unable to retrieve Hibernate information for collection " + entityName + "." + rName);
            ClassMetadata clsMeta = sessFact.getClassMetadata(superCls);
            if (clsMeta == null)
                throw new RuntimeException("Unable to retrieve Hibernate information for collection "
                        + entityName + "." + rName + ", even from superclass(es)");
            entityName = clsMeta.getEntityName();
            parentMeta = (AbstractCollectionPersister) sessFact.getCollectionMetadata(entityName + "." + rName);
        }
    }
    String[] colNames = parentMeta.getKeyColumnNames();
    String childName = parentMeta.getElementType().getName();

    AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(childName);
    String[] propNames = childMeta.getPropertyNames();
    for (int i = 0; i < propNames.length; i++) {
        Type type = childMeta.getPropertyType(propNames[i]);
        if (!type.isEntityType())
            continue;
        EntityType entType = (EntityType) type;
        if (!entType.getAssociatedEntityName().equals(entityName))
            continue;
        String[] cnames = childMeta.getPropertyColumnNames(i);
        if (cnames.length != colNames.length)
            continue;
        boolean columnMatch = true;
        for (int j = 0; j < cnames.length; j++) {
            if (!cnames[j].equals(colNames[j])) {
                columnMatch = false;
                break;
            }
        }
        if (columnMatch) {
            result[0] = childName;
            result[1] = propNames[i];
            return result;
        }
    }

    return null;
}

From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java

License:Open Source License

/**
 * Get the inverse of a many-to-one role
 * @param entityName The entity owning the role
 * @param rName The role name//from   w  w  w  .  j  av  a2  s  .  co m
 * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role
 */
private String[] getInverseOfSingleRole(String entityName, String rName) {
    String[] result = new String[2];

    SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory();
    AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(entityName);
    int propIdx = childMeta.getPropertyIndex(rName);
    String[] cnames = childMeta.getPropertyColumnNames(propIdx);
    Type parentType = childMeta.getPropertyType(rName);
    if (parentType instanceof OneToOneType)
        return getInverseOfOneToOneRole(entityName, rName);
    if (!(parentType instanceof ManyToOneType))
        throw new RuntimeException("Inverse of single-valued role " + entityName + "." + rName
                + " is neither single-valued not multi-valued");
    ManyToOneType manyType = (ManyToOneType) parentType;
    String parentEntityName = manyType.getAssociatedEntityName();

    AbstractEntityPersister parentMeta = (AbstractEntityPersister) sessFact.getClassMetadata(parentEntityName);
    String[] propNames = parentMeta.getPropertyNames();
    for (int i = 0; i < propNames.length; i++) {
        Type type = parentMeta.getPropertyType(propNames[i]);
        if (!type.isCollectionType())
            continue;
        CollectionType collType = (CollectionType) type;
        if (!collType.getAssociatedEntityName((SessionFactoryImplementor) sessFact).equals(entityName))
            continue;

        AbstractCollectionPersister persister = (AbstractCollectionPersister) sessFact
                .getCollectionMetadata(parentEntityName + "." + propNames[i]);
        String[] colNames = persister.getKeyColumnNames();
        if (cnames.length != colNames.length)
            continue;
        boolean columnMatch = true;
        for (int j = 0; j < cnames.length; j++) {
            if (!cnames[j].equals(colNames[j])) {
                columnMatch = false;
                break;
            }
        }
        if (columnMatch) {
            result[0] = parentEntityName;
            result[1] = propNames[i];
            return result;
        }
    }

    return null;
}

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

License:Open Source License

public EntityPropertyMapper buildPropertyMapper(Class entityClass) {
    AbstractEntityPersister entityPersister = (AbstractEntityPersister) sessionFactory
            .getClassMetadata(entityClass);
    if (entityPersister == null)
        throw new IllegalArgumentException("Class " + entityClass + " is not an Hibernate entity class");
    EntityPropertyMapper mapper = new EntityPropertyMapper(entityClass, entityPersister.getTableName());
    String[] propertyNames = entityPersister.getPropertyNames();
    for (String propertyName : propertyNames) {
        String[] columnNames = entityPersister.getPropertyColumnNames(propertyName);
        Type propertyType = entityPersister.getPropertyType(propertyName);
        mapper.put(propertyName, propertyType, columnNames);
    }/*from   w  w w  .  j  ava2  s  .co m*/
    String identifierPropertyName = entityPersister.getIdentifierPropertyName();
    String[] identifierColumnNames = entityPersister.getIdentifierColumnNames();
    Type identifierPropertyType = entityPersister.getIdentifierType();
    mapper.putId(identifierPropertyName, identifierPropertyType, identifierColumnNames);

    return mapper;
}

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

License:Apache License

private boolean isColumnShared(AbstractEntityPersister subclassPersister, String[] columnNames) {
    List<String> propertiesToCheck = new ArrayList<>(Arrays.asList(subclassPersister.getPropertyNames()));
    while (!propertiesToCheck.isEmpty()) {
        String propertyName = propertiesToCheck.remove(propertiesToCheck.size() - 1);
        Type propertyType = subclassPersister.getPropertyType(propertyName);
        if (propertyType instanceof ComponentType) {
            ComponentType componentType = (ComponentType) propertyType;
            for (String subPropertyName : componentType.getPropertyNames()) {
                propertiesToCheck.add(propertyName + "." + subPropertyName);
            }/*from w  ww  .  j  a va  2s.  c  o  m*/
        } else {
            String[] subclassColumnNames = subclassPersister.getSubclassPropertyColumnNames(propertyName);
            if (Arrays.deepEquals(columnNames, subclassColumnNames)) {
                return true;
            }
        }
    }

    return false;
}

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

License:Apache License

private static Set<String> getColumnMatchingAttributeNames(AbstractEntityPersister ownerEntityPersister,
        List<String> idColumnNames) {
    Set<String> idAttributeNames = new LinkedHashSet<>();
    Type identifierType = ownerEntityPersister.getIdentifierType();
    if (identifierType instanceof ComponentType) {
        String[] idPropertyNames = ((ComponentType) identifierType).getPropertyNames();
        for (String propertyName : idPropertyNames) {
            String attributeName = ownerEntityPersister.getIdentifierPropertyName() + "." + propertyName;
            String[] propertyColumnNames = ownerEntityPersister.getSubclassPropertyColumnNames(attributeName);
            if (propertyColumnNames != null) {
                for (int j = 0; j < propertyColumnNames.length; j++) {
                    String propertyColumnName = propertyColumnNames[j];
                    if (idColumnNames.contains(propertyColumnName)) {
                        idAttributeNames.add(attributeName);
                        break;
                    }//from   w  w  w . java 2s  . c  o  m
                }
            }
        }
        // We assume that when a primary identifier attribute is part of the id column names, that we are done
        if (!idAttributeNames.isEmpty()) {
            return idAttributeNames;
        }
    } else {
        for (String identifierColumnName : ownerEntityPersister.getIdentifierColumnNames()) {
            if (idColumnNames.contains(identifierColumnName)) {
                idAttributeNames.add(ownerEntityPersister.getIdentifierPropertyName());
                return idAttributeNames;
            }
        }
    }
    String[] propertyNames = ownerEntityPersister.getPropertyNames();
    for (int i = 0; i < propertyNames.length; i++) {
        String propertyName = propertyNames[i];
        String[] propertyColumnNames = ownerEntityPersister.getSubclassPropertyColumnNames(propertyName);
        if (propertyColumnNames != null) {
            for (int j = 0; j < propertyColumnNames.length; j++) {
                String propertyColumnName = propertyColumnNames[j];
                if (idColumnNames.contains(propertyColumnName)) {
                    idAttributeNames.add(propertyName);
                    break;
                }
            }
        }
    }
    return idAttributeNames;
}

From source file:org.apache.ctakes.ytex.uima.mapper.DocumentMapperServiceImpl.java

License:Apache License

public void initDocKeyMapping() {
    AbstractEntityPersister cm = (AbstractEntityPersister) this.sessionFactory.getClassMetadata(Document.class);
    // figure out which columns are already mapped
    String[] propNames = cm.getPropertyNames();
    Set<String> mappedCols = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    for (String prop : propNames) {
        String cols[] = cm.getPropertyColumnNames(prop);
        mappedCols.addAll(Arrays.asList(cols));
    }//from ww  w .  j  a va  2 s . com
    // this.formattedTableName = DBUtil.formatTableName(cm.getTableName());
    this.formattedTableName = cm.getTableName();
    log.info("document table name = " + formattedTableName);
    final String query = "select * from " + formattedTableName + " where 1=2";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(query);
        ResultSetMetaData rsmd = rs.getMetaData();
        int nCols = rsmd.getColumnCount();
        for (int i = 1; i <= nCols; i++) {
            String colName = rsmd.getColumnName(i);
            if (!mappedCols.contains(colName)) {
                log.info("document candidate foreign key column: " + colName);
                docTableCols.put(colName, rsmd.getColumnType(i));
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("docTableCols: " + docTableCols);
        }
    } catch (SQLException e) {
        log.error("problem determining document table fields", e);
        throw new RuntimeException(e);
    } finally {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

From source file:org.nextframework.persistence.PersistenceUtils.java

License:Apache License

public static InverseCollectionProperties getInverseCollectionProperty(SessionFactory sessionFactory,
        Class<? extends Object> clazz, String collectionProperty) {
    SessionFactoryImplementor sessionFactoryImplementor;
    String[] keyColumnNames;//ww w.  j a  v a2s  . c  om
    Class<?> returnedClass;
    try {
        sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
        ClassMetadata classMetadata = getClassMetadata(clazz, sessionFactory);
        if (classMetadata == null) {
            throw new PersistenceException("Class " + clazz.getName() + " is not mapped. ");
        }
        CollectionType ct = (CollectionType) classMetadata.getPropertyType(collectionProperty);
        AbstractCollectionPersister collectionMetadata = (AbstractCollectionPersister) sessionFactoryImplementor
                .getCollectionMetadata(ct.getRole());
        keyColumnNames = ((AbstractCollectionPersister) collectionMetadata).getKeyColumnNames();
        returnedClass = ct.getElementType(sessionFactoryImplementor).getReturnedClass();
    } catch (ClassCastException e) {
        throw new PersistenceException(
                "Property \"" + collectionProperty + "\" of " + clazz + " is not a mapped as a collection.");
    }

    AbstractEntityPersister collectionItemMetadata = (AbstractEntityPersister) sessionFactoryImplementor
            .getClassMetadata(returnedClass);
    Type[] propertyTypes = collectionItemMetadata.getPropertyTypes();
    String[] propertyNames = collectionItemMetadata.getPropertyNames();
    for (int i = 0; i < propertyTypes.length; i++) {
        Type type = propertyTypes[i];
        String propertyName = propertyNames[i];
        String[] propertyColumnNames = collectionItemMetadata.getPropertyColumnNames(propertyName);
        InverseCollectionProperties inverseCollectionProperties = getInverseCollectionProperties(
                sessionFactoryImplementor, clazz, returnedClass, keyColumnNames, propertyColumnNames, type,
                propertyName);
        if (inverseCollectionProperties != null) {
            return inverseCollectionProperties;
        }
    }
    //check id
    Type identifierType = collectionItemMetadata.getIdentifierType();
    String identifierName = collectionItemMetadata.getIdentifierPropertyName();
    String[] identifierColumnNames = collectionItemMetadata.getIdentifierColumnNames();
    InverseCollectionProperties inverseCollectionProperties = getInverseCollectionProperties(
            sessionFactoryImplementor, clazz, returnedClass, keyColumnNames, identifierColumnNames,
            identifierType, identifierName);
    if (inverseCollectionProperties != null) {
        return inverseCollectionProperties;
    }
    throw new PersistenceException(
            "Collection " + collectionProperty + " of " + clazz + " does not have an inverse path!");
}

From source file:uk.org.openeyes.DatabaseFunctions.java

/**
 *
 * @param originalTable//w w  w.j  a  v  a 2  s.c  om
 * @param originalID
 * @return
 */
public int addVersionTableData(Object originalTable, Integer originalID) {
    // http://www.tutorialspoint.com/hibernate/hibernate_native_sql.htm
    AbstractEntityPersister aep = ((AbstractEntityPersister) session.getSessionFactory()
            .getClassMetadata(originalTable.getClass()));
    String tableName = aep.getTableName();
    String[] properties = aep.getPropertyNames();
    String[] originalColumns = new String[properties.length];

    for (int nameIndex = 0; nameIndex != properties.length; nameIndex++) {
        //System.out.println("Property name: "+properties[nameIndex]);  
        String[] columns = aep.getPropertyColumnNames(nameIndex);
        for (int columnIndex = 0; columnIndex != columns.length; columnIndex++) {
            //System.out.println("Column name: "+columns[columnIndex]);  
            originalColumns[nameIndex] = columns[columnIndex];
        }
    }

    //System.out.println("INSERT INTO "+tableName+"_version (id, "+String.join(",", originalColumns)+",`version_date`,`version_id`) SELECT id, "+String.join(",", originalColumns)+", now(), NULL FROM "+tableName+" WHERE id="+originalID.toString());
    Query query = this.session.createSQLQuery(
            "INSERT INTO " + tableName + "_version (id, " + StringUtils.concat(originalColumns, ',')
                    + ",`version_date`,`version_id`) SELECT id, " + StringUtils.concat(originalColumns, ',')
                    + ", now(), NULL FROM " + tableName + " WHERE id=" + originalID.toString());
    dicomLogger.addToRawOutput("Version audit trail has been added to: " + tableName + "_version");
    return query.executeUpdate();
}