Example usage for org.hibernate SessionFactory getClassMetadata

List of usage examples for org.hibernate SessionFactory getClassMetadata

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getClassMetadata.

Prototype

@Deprecated
ClassMetadata getClassMetadata(String entityName);

Source Link

Document

Retrieve the ClassMetadata associated with the given entity class.

Usage

From source file:org.shept.persistence.provider.hibernate.HibernateUtils_old.java

License:Apache License

private static ClassMetadata getClassMetadata(HibernateDaoSupport dao, Object model) {
    SessionFactory hsf = dao.getHibernateTemplate().getSessionFactory();
    if (model == null)
        return null;
    return hsf.getClassMetadata(model.getClass());
}

From source file:org.unitime.commons.hibernate.util.HibernateUtil.java

License:Open Source License

public static void clearCache(Class persistentClass, boolean evictQueries) {
    _RootDAO dao = new _RootDAO();
    org.hibernate.Session hibSession = dao.getSession();
    SessionFactory hibSessionFactory = hibSession.getSessionFactory();
    if (persistentClass == null) {
        for (Iterator i = hibSessionFactory.getAllClassMetadata().entrySet().iterator(); i.hasNext();) {
            Map.Entry entry = (Map.Entry) i.next();
            String className = (String) entry.getKey();
            ClassMetadata classMetadata = (ClassMetadata) entry.getValue();
            try {
                hibSessionFactory.getCache().evictEntityRegion(Class.forName(className));
                for (int j = 0; j < classMetadata.getPropertyNames().length; j++) {
                    if (classMetadata.getPropertyTypes()[j].isCollectionType()) {
                        try {
                            hibSessionFactory.getCache().evictCollectionRegion(
                                    className + "." + classMetadata.getPropertyNames()[j]);
                        } catch (MappingException e) {
                        }//w w  w .  java 2  s.com
                    }
                }
            } catch (ClassNotFoundException e) {
            }
        }
        hibSessionFactory.getCache().evictEntityRegions();
        hibSessionFactory.getCache().evictCollectionRegions();
    } else {
        ClassMetadata classMetadata = hibSessionFactory.getClassMetadata(persistentClass);
        hibSessionFactory.getCache().evictEntityRegion(persistentClass);
        if (classMetadata != null) {
            for (int j = 0; j < classMetadata.getPropertyNames().length; j++) {
                if (classMetadata.getPropertyTypes()[j].isCollectionType()) {
                    try {
                        hibSessionFactory.getCache().evictCollectionRegion(persistentClass.getClass().getName()
                                + "." + classMetadata.getPropertyNames()[j]);
                    } catch (MappingException e) {
                    }
                }
            }
        }
    }
    if (evictQueries) {
        hibSessionFactory.getCache().evictQueryRegions();
        hibSessionFactory.getCache().evictDefaultQueryRegion();
    }
}

From source file:org.unitime.timetable.action.HibernateQueryTestAction.java

License:Open Source License

public void printHeader(StringBuffer s, Object o) {
    s.append("<table width='100%' border='0' cellspacing='0' cellpadding='3'>");
    s.append("<tr align='left'>");
    SessionFactory hibSessionFactory = new _RootDAO().getSession().getSessionFactory();
    int idx = 1;//from   w  w w  .j ava2s .  co m
    if (o == null) {
        header(s, idx++, null);
    } else if (o instanceof Object[]) {
        Object[] x = (Object[]) o;
        for (int i = 0; i < x.length; i++) {
            if (x[i] == null) {
                header(s, idx++, null);
            } else {
                ClassMetadata meta = hibSessionFactory.getClassMetadata(x[i].getClass());
                if (meta == null) {
                    header(s, idx++, null);
                } else {
                    header(s, idx++, meta.getIdentifierPropertyName());
                    for (int j = 0; j < meta.getPropertyNames().length; j++) {
                        if (!skip(meta.getPropertyTypes()[j], meta.getPropertyLaziness()[j]))
                            header(s, idx++, meta.getPropertyNames()[j]);
                    }
                }
            }
        }
    } else {
        ClassMetadata meta = hibSessionFactory.getClassMetadata(o.getClass());
        if (meta == null) {
            header(s, idx++, null);
        } else {
            header(s, idx++, meta.getIdentifierPropertyName());
            for (int i = 0; i < meta.getPropertyNames().length; i++) {
                if (!skip(meta.getPropertyTypes()[i], meta.getPropertyLaziness()[i]))
                    header(s, idx++, meta.getPropertyNames()[i]);
            }
        }
    }
    s.append("</tr>");
}

From source file:org.unitime.timetable.action.HibernateQueryTestAction.java

License:Open Source License

public void printLine(StringBuffer s, Object o, SessionImplementor session) {
    s.append(/*from   w  ww  . j  a  va 2 s .  c  o  m*/
            "<tr align='left' onmouseover=\"this.style.backgroundColor='rgb(223,231,242)';\" onmouseout=\"this.style.backgroundColor='transparent';\" >");
    SessionFactory hibSessionFactory = new _RootDAO().getSession().getSessionFactory();
    if (o == null) {
        line(s, null);
    } else if (o instanceof Object[]) {
        Object[] x = (Object[]) o;
        for (int i = 0; i < x.length; i++) {
            if (x[i] == null) {
                line(s, null);
            } else {
                ClassMetadata meta = hibSessionFactory.getClassMetadata(x[i].getClass());
                if (meta == null) {
                    line(s, x[i]);
                } else {
                    line(s, meta.getIdentifier(x[i], session));
                    for (int j = 0; j < meta.getPropertyNames().length; j++)
                        if (!skip(meta.getPropertyTypes()[j], meta.getPropertyLaziness()[j]))
                            line(s, meta.getPropertyValue(x[i], meta.getPropertyNames()[j]));
                }
            }
        }
    } else {
        ClassMetadata meta = hibSessionFactory.getClassMetadata(o.getClass());
        if (meta == null) {
            line(s, o);
        } else {
            line(s, meta.getIdentifier(o, session));
            for (int i = 0; i < meta.getPropertyNames().length; i++)
                if (!skip(meta.getPropertyTypes()[i], meta.getPropertyLaziness()[i]))
                    line(s, meta.getPropertyValue(o, meta.getPropertyNames()[i]));
        }
    }
    s.append("</tr>");
}

From source file:org.web4thejob.orm.MetaUtil.java

License:Open Source License

public static int getHibernatePropertyIndex(String entityType, String propertyName) {

    final SessionFactory sessionFactory = ContextUtil.getBean(SessionFactory.class);
    final ClassMetadata classMetadata = sessionFactory.getClassMetadata(entityType);
    for (int i = 0; i < classMetadata.getPropertyNames().length; i++) {
        if (classMetadata.getPropertyNames()[i].equals(propertyName)) {
            return i;
        }//from   w  w w .  ja  v a  2  s . c o m
    }

    return -1;
}

From source file:woko.hibernate.HibernatePrimaryKeyConverter.java

License:Apache License

/**
 * Extract and return primary key value for persistent entity, using mapping metadata.
 * @param sessionFactory the Hibernate session factory
 * @param entity the entity to get the primary key for
 * @param entityClass the entity class/*ww w  .  j av  a2s  . com*/
 * @return the primary key value for passed entity
 */
public Serializable getPrimaryKeyValue(SessionFactory sessionFactory, Object entity, Class<?> entityClass) {
    ClassMetadata cm = sessionFactory.getClassMetadata(entityClass);
    if (cm == null) {
        return null;
    }
    String idPropName = cm.getIdentifierPropertyName();
    if (idPropName == null) {
        return null;
    }
    return (Serializable) Util.getPropertyValue(entity, idPropName);
}