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

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

Introduction

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

Prototype

public int getPropertyIndex(String propertyName) 

Source Link

Usage

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//w w w  . ja  v  a  2  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.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.java

License:Apache License

@Override
public int getSqlSelectAttributePosition(EntityManager em, Query query, String expression) {
    if (expression.contains(".")) {
        // TODO: implement
        throw new UnsupportedOperationException("Embeddables are not yet supported!");
    }/*from   w  w w  .j a  v a2 s .  co  m*/

    SessionImplementor session = em.unwrap(SessionImplementor.class);
    HQLQueryPlan plan = getOriginalQueryPlan(session, query);
    if (plan.getTranslators().length > 1) {
        throw new IllegalArgumentException("No support for multiple translators yet!");
    }
    QueryTranslator translator = plan.getTranslators()[0];

    try {
        QueryNode queryNode = getField(translator, "sqlAst");
        SelectClause selectClause = queryNode.getSelectClause();
        Type[] queryReturnTypes = selectClause.getQueryReturnTypes();

        boolean found = false;
        // The ordinal is 1 based
        int position = 1;
        for (int i = 0; i < queryReturnTypes.length; i++) {
            Type t = queryReturnTypes[i];
            if (t instanceof ManyToOneType) {
                ManyToOneType manyToOneType = (ManyToOneType) t;
                AbstractEntityPersister persister = (AbstractEntityPersister) session.getFactory()
                        .getEntityPersister(manyToOneType.getAssociatedEntityName());

                int propertyIndex = persister.getPropertyIndex(expression);
                found = true;
                for (int j = 0; j < propertyIndex; j++) {
                    position += persister.getPropertyColumnNames(j).length;
                }
                // Increment to the actual property position
                position++;
            } else {
                position++;
            }
        }

        if (found) {
            return position;
        }

        AST selectItem = selectClause.getFirstChild();

        while (selectItem != null && (selectItem.getType() == SqlTokenTypes.DISTINCT
                || selectItem.getType() == SqlTokenTypes.ALL)) {
            selectItem = selectItem.getNextSibling();
        }

        position = 1;
        for (AST n = selectItem; n != null; n = n.getNextSibling()) {
            if (n instanceof DotNode) {
                DotNode dot = (DotNode) n;
                if (expression.equals(dot.getPropertyPath())) {
                    // Check if the property is an embeddable
                    if (dot.getText().contains(",")) {
                        throw new IllegalStateException("Can't order by the embeddable: " + expression);
                    }
                    found = true;
                    break;
                }
            }
            position++;
        }

        if (found) {
            return position;
        }

        return -1;
    } catch (Exception e1) {
        throw new RuntimeException(e1);
    }
}

From source file:to.etc.domui.hibernate.model.CriteriaCreatingVisitor.java

License:Open Source License

/**
 * Hibernate's jokish metadata does not include the PK in it's properties structures. So
 * we explicitly need to check if the name is the PK property, then return the column names
 * for that PK.//from  w w w. j  a  v a2 s . c o  m
 *
 * @param aep
 * @param compoundName
 * @return
 */
@NonNull
private String[] getPropertyColumnNamesFromLousyMetadata(AbstractEntityPersister aep, String compoundName) {
    String name = compoundName;
    int dotix = compoundName.lastIndexOf('.');
    if (dotix != -1) {
        name = compoundName.substring(dotix + 1);
    }

    //-- The PK property is not part of the "properties" in hibernate's idiot metadata. So first check if we're looking at that ID property.
    if (name.equals(aep.getIdentifierPropertyName())) {
        return aep.getIdentifierColumnNames();
    }
    int ix = aep.getPropertyIndex(name);
    if (ix < 0)
        throw new QQuerySyntaxException(
                "Cannot obtain Hibernate metadata for property=" + name + ": property index not found");
    String[] colar = aep.getPropertyColumnNames(ix);
    if (colar == null || colar.length != 1/* || colar[0] == null*/)
        throw new QQuerySyntaxException("'Like' cannot be done on multicolumn/0column property " + name);
    return colar;
}