Example usage for org.hibernate.persister.collection AbstractCollectionPersister getKeyColumnNames

List of usage examples for org.hibernate.persister.collection AbstractCollectionPersister getKeyColumnNames

Introduction

In this page you can find the example usage for org.hibernate.persister.collection AbstractCollectionPersister getKeyColumnNames.

Prototype

@Override
    public String[] getKeyColumnNames() 

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   w w w .j  a  va2 s.c o  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 ww  w  .  j  a va  2 s .  c o 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:net.digitalprimates.persistence.translators.hibernate.HibernateSerializer.java

License:Open Source License

/**
 * Query the database and get a result set of IDS that belong to a specific
 * collection//from   w  w  w  .j  av  a  2 s . c  o  m
 * 
 * @return
 */
private List getPkIds(SessionImplementor session, CollectionPersister persister,
        PersistentCollection collection) throws ClassNotFoundException {
    AbstractCollectionPersister absPersister = (AbstractCollectionPersister) persister;
    String[] keyNames;

    if (absPersister.isOneToMany() || absPersister.isManyToMany()) {
        keyNames = absPersister.getElementColumnNames();
    } else {
        keyNames = absPersister.getKeyColumnNames();
    }
    //String[] columnNames = absPersister.getElementColumnNames();

    SimpleSelect pkSelect = new SimpleSelect(((SessionImpl) session).getFactory().getDialect());
    pkSelect.setTableName(absPersister.getTableName());
    pkSelect.addColumns(keyNames);
    pkSelect.addCondition(absPersister.getKeyColumnNames(), "=?");

    String sql = pkSelect.toStatementString();
    List results = new ArrayList();

    try {
        // int size = absPersister.getSize(collection.getKey(), eventSession);
        Query q2 = ((SessionImpl) session).createSQLQuery(sql).setParameter(0, collection.getKey())
                .setResultTransformer(new PassThroughResultTransformer());

        List hibernateResults = q2.list();
        //return results;

        Type t = persister.getKeyType();

        PreparedStatement stmt = ((SessionImpl) session).connection().prepareStatement(sql);
        if (t instanceof StringType) {
            stmt.setString(1, collection.getKey().toString());
        } else {
            stmt.setObject(1, new Integer(collection.getKey().toString()).intValue());
        }

        ResultSet keyResults = stmt.executeQuery();

        while (keyResults.next()) {
            results.add(keyResults.getObject(1));
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return results;
}