Example usage for org.hibernate.persister.entity SingleTableEntityPersister getPropertyColumnNames

List of usage examples for org.hibernate.persister.entity SingleTableEntityPersister getPropertyColumnNames

Introduction

In this page you can find the example usage for org.hibernate.persister.entity SingleTableEntityPersister getPropertyColumnNames.

Prototype

@Override
public String[] getPropertyColumnNames(String propertyName) 

Source Link

Document

Warning: When there are duplicated property names in the subclasses then this method may return the wrong results.

Usage

From source file:com.github.kuros.random.jpa.metamodel.AttributeProvider.java

License:Open Source License

private void init() {
    entityTableMappingByClass = new HashMap<Class<?>, EntityTableMapping>();
    entityTableMappingByTableName = new HashMap<String, List<EntityTableMapping>>();
    final HibernateEntityManagerFactory entityManagerFactory = (HibernateEntityManagerFactory) entityManager
            .getEntityManagerFactory();// w w w.  ja  v a  2  s  .  c om
    final Map<String, ClassMetadata> allClassMetadata = entityManagerFactory.getSessionFactory()
            .getAllClassMetadata();

    final Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities();
    for (EntityType<?> entity : entities) {
        final ClassMetadata classMetadata = allClassMetadata.get(entity.getJavaType().getName());
        if (classMetadata instanceof SingleTableEntityPersister) {
            final EntityTableMapping entityTableMapping = new EntityTableMapping(entity.getJavaType());
            final SingleTableEntityPersister singleTableEntityPersister = (SingleTableEntityPersister) classMetadata;

            entityTableMapping.addColumnIds(singleTableEntityPersister.getKeyColumnNames());
            entityTableMapping.setTableName(getTableName(singleTableEntityPersister));
            entityTableMapping
                    .setIdentifierGenerator(singleTableEntityPersister.getIdentifierGenerator().getClass());

            final List<String> attributeNames = getSupportedAttributeNames(singleTableEntityPersister);

            for (Attribute attribute : entity.getAttributes()) {
                final String name = AttributeHelper.getName(attribute);
                final String[] propertyColumnNames = singleTableEntityPersister.getPropertyColumnNames(name);
                final String columnName = propertyColumnNames.length > 0 ? propertyColumnNames[0] : null;
                if (columnName != null && attributeNames.contains(name)) {
                    entityTableMapping.addAttributeColumnMapping(name, columnName);
                }
            }

            for (String id : entityTableMapping.getColumnIds()) {
                entityTableMapping.addAttributeIds(entityTableMapping.getAttributeName(id));
            }

            putEntityTableMapping(singleTableEntityPersister, entityTableMapping);
            entityTableMappingByClass.put(entity.getJavaType(), entityTableMapping);
        }

    }
}

From source file:org.xerela.provider.devices.internal.TagResolutionScheme.java

License:Mozilla Public License

@SuppressWarnings("nls")
private PageData searchByNoTag(PageData pageData, String sortColumn, boolean descending) {
    Session session = DeviceProviderActivator.getSessionFactory().getCurrentSession();

    StringBuilder sortClause = new StringBuilder();
    StringBuilder fromClause = new StringBuilder();
    fromClause.append("FROM device d LEFT OUTER JOIN device_tag dt ON d.device_id=dt.device_id ")
            .append("WHERE dt.device_id IS NULL");

    if (sortColumn != null) {
        ClassMetadata classMetadata = DeviceProviderActivator.getSessionFactory()
                .getClassMetadata(ZDeviceLite.class);
        SingleTableEntityPersister step = (SingleTableEntityPersister) classMetadata;
        String[] propertyColumnNames = step.getPropertyColumnNames(sortColumn);
        if (propertyColumnNames != null && propertyColumnNames.length > 0) {
            sortClause.append(" ORDER BY ").append(propertyColumnNames[0])
                    .append(descending ? " DESC" : " ASC");
        }/*from   w  ww.j a  v a2  s  . c o m*/
    }

    Query query = session.createSQLQuery("SELECT d.device_id " + fromClause + sortClause)
            .setFirstResult(pageData.getOffset()).setMaxResults(pageData.getPageSize());

    List<?> deviceIds = query.list();
    if (deviceIds == null || deviceIds.isEmpty()) {
        pageData.setDevices(new ZDeviceLite[0]);
        pageData.setTotal(0);

        return pageData;
    }

    if (pageData.getOffset() == 0) {
        // Set the total result size into the page data.
        query = session.createSQLQuery("SELECT count(d.device_id) " + fromClause);
        pageData.setTotal(getCount(query));
    }

    // Load the device objects.
    Criteria criteria = session.createCriteria(ZDeviceLite.class)
            .add(Restrictions.in(ATTR_DEVICE_ID, deviceIds));

    if (sortColumn != null) {
        criteria.addOrder((descending ? Order.desc(sortColumn.trim()) : Order.asc(sortColumn.trim())));
    }

    List<?> devices = criteria.list();

    pageData.setDevices(devices.toArray(new ZDeviceLite[0]));
    return pageData;
}

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

License:Open Source License

/**
 * Try to locate the property in the child that refers to the parent in a horrible way.
 * @param cm//from  w  w  w.j  a v a2  s. c  om
 * @param keyCols
 * @return
 */
private String findCruddyChildProperty(ClassMetadata cm, String[] keyCols) {
    SingleTableEntityPersister fuckup = (SingleTableEntityPersister) cm;
    for (int i = fuckup.getPropertyNames().length; --i >= 0;) {
        String[] cols = fuckup.getPropertyColumnNames(i);
        if (Arrays.equals(keyCols, cols)) {
            return cm.getPropertyNames()[i];
        }
    }

    /*
     * The identifier property is fully separate from all other properties because that
     * makes it hard to use, of course. So explicitly check for a full identifying relation
     * initially.
     */
    String idname = fuckup.getIdentifierPropertyName();
    String[] cols = fuckup.getIdentifierColumnNames();
    if (Arrays.equals(keyCols, cols)) {
        return idname;
    }

    /*
     * The ID property can be compound, in that case we need to handle it's
     * component properties separately. This code is wrong because it only
     * handles one level of indirection - but that is enough for me now, this
     * is horrible. The proper way of implementing is to recursively determine
     * the smallest property accessing the columns specified in this call, and
     * to determine it's full path.
     */
    Type idtype = fuckup.getIdentifierType();
    if (idtype instanceof ComponentType) {
        ComponentType ct = (ComponentType) idtype;

        //         for(int scp = 0; scp < fuckup.countSubclassProperties(); scp++) { There's no end to the incredible mess.
        //            String scpn = fuckup.getSubclassPropertyName(scp);
        //            cols = fuckup.getSubclassPropertyColumnNames(scpn);
        //            System.out.println("prop: " + scpn + ", cols=" + Arrays.toString(cols));
        //         }
        //

        String[] xx = fuckup.getSubclassPropertyColumnNames(idname);
        String[] cpnar = ct.getPropertyNames();
        for (int i = 0; i < cpnar.length; i++) {
            String pname = cpnar[i];
            cols = fuckup.getSubclassPropertyColumnNames(idname + "." + pname);
            if (Arrays.equals(keyCols, cols)) {
                return idname + "." + pname;
            }
        }
    }

    //-- All has failed- mapping unknown.
    return null;
}