List of usage examples for org.hibernate SessionFactory getClassMetadata
@Deprecated ClassMetadata getClassMetadata(String entityName);
From source file:org.beanfuse.entity.context.HibernateEntityContext.java
License:Open Source License
/** * ???.<br>/* w w w. j a v a2 s .co m*/ * ?entityTypes. * * @param entityName * @return */ private EntityType buildEntityType(SessionFactory sessionFactory, String entityName) { EntityType entityType = (EntityType) entityTypes.get(entityName); if (null == entityType) { ClassMetadata cm = sessionFactory.getClassMetadata(entityName); if (null == cm) { logger.error("Cannot find ClassMetadata for {}", entityName); return null; } entityType = new EntityType(); entityType.setEntityName(cm.getEntityName()); entityType.setIdPropertyName(cm.getIdentifierPropertyName()); entityType.setEntityClass(cm.getMappedClass(EntityMode.POJO)); entityTypes.put(cm.getEntityName(), entityType); Map propertyTypes = entityType.getPropertyTypes(); String[] ps = cm.getPropertyNames(); for (int i = 0; i < ps.length; i++) { org.hibernate.type.Type type = cm.getPropertyType(ps[i]); if (type.isEntityType()) { propertyTypes.put(ps[i], buildEntityType(sessionFactory, type.getName())); } else if (type.isComponentType()) { propertyTypes.put(ps[i], buildComponentType(sessionFactory, entityName, ps[i])); } else if (type.isCollectionType()) { propertyTypes.put(ps[i], buildCollectionType(sessionFactory, defaultCollectionClass(type), entityName + "." + ps[i])); } } } return entityType; }
From source file:org.beanfuse.entity.context.HibernateEntityContext.java
License:Open Source License
private ComponentType buildComponentType(SessionFactory sessionFactory, String entityName, String propertyName) {/*from w ww .j av a 2s . c o m*/ EntityType entityType = (EntityType) entityTypes.get(entityName); if (null != entityType) { Type propertyType = (Type) entityType.getPropertyTypes().get(propertyName); if (null != propertyType) { return (ComponentType) propertyType; } } ClassMetadata cm = sessionFactory.getClassMetadata(entityName); org.hibernate.type.ComponentType hcType = (org.hibernate.type.ComponentType) cm .getPropertyType(propertyName); String[] propertyNames = hcType.getPropertyNames(); ComponentType cType = new ComponentType(hcType.getReturnedClass()); Map propertyTypes = cType.getPropertyTypes(); for (int j = 0; j < propertyNames.length; j++) { org.hibernate.type.Type type = cm.getPropertyType(propertyName + "." + propertyNames[j]); if (type.isEntityType()) { propertyTypes.put(propertyNames[j], buildEntityType(sessionFactory, type.getName())); } else if (type.isComponentType()) { propertyTypes.put(propertyNames[j], buildComponentType(sessionFactory, entityName, propertyName + "." + propertyNames[j])); } else if (type.isCollectionType()) { propertyTypes.put(propertyNames[j], buildCollectionType(sessionFactory, defaultCollectionClass(type), entityName + "." + propertyName + "." + propertyNames[j])); } } return cType; }
From source file:org.beangle.commons.orm.hibernate.internal.HibernateEntityContext.java
License:Open Source License
/** * ???.<br>/* w w w . j a v a2 s. c o m*/ * ?entityTypes. * * @param entityName * @return */ private EntityType buildEntityType(SessionFactory sessionFactory, String entityName) { EntityType entityType = (EntityType) entityTypes.get(entityName); if (null == entityType) { ClassMetadata cm = sessionFactory.getClassMetadata(entityName); if (null == cm) { logger.error("Cannot find ClassMetadata for {}", entityName); return null; } entityType = new EntityType(cm.getEntityName(), cm.getMappedClass(EntityMode.POJO), cm.getIdentifierPropertyName()); entityTypes.put(cm.getEntityName(), entityType); Map<String, Type> propertyTypes = entityType.getPropertyTypes(); String[] ps = cm.getPropertyNames(); for (int i = 0; i < ps.length; i++) { org.hibernate.type.Type type = cm.getPropertyType(ps[i]); if (type.isEntityType()) { propertyTypes.put(ps[i], buildEntityType(sessionFactory, type.getName())); } else if (type.isComponentType()) { propertyTypes.put(ps[i], buildComponentType(sessionFactory, entityName, ps[i])); } else if (type.isCollectionType()) { propertyTypes.put(ps[i], buildCollectionType(sessionFactory, defaultCollectionClass(type), entityName + "." + ps[i])); } } } return entityType; }
From source file:org.beangle.commons.orm.hibernate.internal.HibernateEntityContext.java
License:Open Source License
private ComponentType buildComponentType(SessionFactory sessionFactory, String entityName, String propertyName) {//from ww w.j a v a 2 s. c o m EntityType entityType = (EntityType) entityTypes.get(entityName); if (null != entityType) { Type propertyType = (Type) entityType.getPropertyTypes().get(propertyName); if (null != propertyType) { return (ComponentType) propertyType; } } ClassMetadata cm = sessionFactory.getClassMetadata(entityName); org.hibernate.type.ComponentType hcType = (org.hibernate.type.ComponentType) cm .getPropertyType(propertyName); String[] propertyNames = hcType.getPropertyNames(); ComponentType cType = new ComponentType(hcType.getReturnedClass()); Map<String, Type> propertyTypes = cType.getPropertyTypes(); for (int j = 0; j < propertyNames.length; j++) { org.hibernate.type.Type type = cm.getPropertyType(propertyName + "." + propertyNames[j]); if (type.isEntityType()) { propertyTypes.put(propertyNames[j], buildEntityType(sessionFactory, type.getName())); } else if (type.isComponentType()) { propertyTypes.put(propertyNames[j], buildComponentType(sessionFactory, entityName, propertyName + "." + propertyNames[j])); } else if (type.isCollectionType()) { propertyTypes.put(propertyNames[j], buildCollectionType(sessionFactory, defaultCollectionClass(type), entityName + "." + propertyName + "." + propertyNames[j])); } } return cType; }
From source file:org.beangle.model.persist.hibernate.HibernateEntityContext.java
License:Open Source License
/** * ???.<br>//from w w w. jav a 2 s . co m * ?entityTypes. * * @param entityName * @return */ private EntityType buildEntityType(SessionFactory sessionFactory, String entityName) { EntityType entityType = (EntityType) entityTypes.get(entityName); if (null == entityType) { ClassMetadata cm = sessionFactory.getClassMetadata(entityName); if (null == cm) { logger.error("Cannot find ClassMetadata for {}", entityName); return null; } entityType = new EntityType(); entityType.setEntityName(cm.getEntityName()); entityType.setIdPropertyName(cm.getIdentifierPropertyName()); entityType.setEntityClass(cm.getMappedClass(EntityMode.POJO)); entityTypes.put(cm.getEntityName(), entityType); Map<String, Type> propertyTypes = entityType.getPropertyTypes(); String[] ps = cm.getPropertyNames(); for (int i = 0; i < ps.length; i++) { org.hibernate.type.Type type = cm.getPropertyType(ps[i]); if (type.isEntityType()) { propertyTypes.put(ps[i], buildEntityType(sessionFactory, type.getName())); } else if (type.isComponentType()) { propertyTypes.put(ps[i], buildComponentType(sessionFactory, entityName, ps[i])); } else if (type.isCollectionType()) { propertyTypes.put(ps[i], buildCollectionType(sessionFactory, defaultCollectionClass(type), entityName + "." + ps[i])); } } } return entityType; }
From source file:org.beangle.orm.hibernate.internal.HibernateEntityContext.java
License:Open Source License
/** * ???.<br>/*www. ja v a 2 s . c o m*/ * ?entityTypes. * * @param entityName */ private EntityType buildEntityType(SessionFactory sessionFactory, String entityName) { EntityType entityType = (EntityType) entityTypes.get(entityName); if (null == entityType) { ClassMetadata cm = sessionFactory.getClassMetadata(entityName); if (null == cm) { logger.error("Cannot find ClassMetadata for {}", entityName); return null; } entityType = new EntityType(cm.getEntityName(), cm.getMappedClass(), cm.getIdentifierPropertyName(), new IdentifierType(cm.getIdentifierType().getReturnedClass())); entityTypes.put(cm.getEntityName(), entityType); Map<String, Type> propertyTypes = entityType.getPropertyTypes(); String[] ps = cm.getPropertyNames(); for (int i = 0; i < ps.length; i++) { org.hibernate.type.Type type = cm.getPropertyType(ps[i]); if (type.isEntityType()) { propertyTypes.put(ps[i], buildEntityType(sessionFactory, type.getName())); } else if (type.isComponentType()) { propertyTypes.put(ps[i], buildComponentType(sessionFactory, entityName, ps[i])); } else if (type.isCollectionType()) { propertyTypes.put(ps[i], buildCollectionType(sessionFactory, defaultCollectionClass(type), entityName + "." + ps[i])); } } } return entityType; }
From source file:org.broadleafcommerce.common.util.dao.DynamicDaoHelperImpl.java
License:Apache License
@Override public Map<String, Object> getIdMetadata(Class<?> entityClass, HibernateEntityManager entityManager) { entityClass = getNonProxyImplementationClassIfNecessary(entityClass); Map<String, Object> response = new HashMap<String, Object>(); SessionFactory sessionFactory = entityManager.getSession().getSessionFactory(); ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass); if (metadata == null) { return null; }/*w w w.j a v a2s . c o m*/ String idProperty = metadata.getIdentifierPropertyName(); response.put("name", idProperty); Type idType = metadata.getIdentifierType(); response.put("type", idType); return response; }
From source file:org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl.java
License:Apache License
public Map<String, Class<?>> getIdMetadata(Class<?> entityClass) { Map response = new HashMap(); SessionFactory sessionFactory = getSessionFactory(); ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass); String idProperty = metadata.getIdentifierPropertyName(); response.put("name", idProperty); Type idType = metadata.getIdentifierType(); response.put("type", idType); return response; }
From source file:org.broadleafcommerce.openadmin.server.service.persistence.entitymanager.HibernateCleaner.java
License:Apache License
protected void performConvert(Object originalBean, Object targetBean, Field[] fields, Method method, HibernateEntityManager em, PlatformTransactionManager txManager) throws Throwable { SessionFactory sessionFactory = em.getSession().getSessionFactory(); ClassMetadata metadata = sessionFactory.getClassMetadata(originalBean.getClass()); String idProperty = metadata.getIdentifierPropertyName(); if (!typePool.containsKey(originalBean.getClass().getName())) { List<String> propertyNames = new ArrayList<String>(); for (String propertyName : metadata.getPropertyNames()) { propertyNames.add(propertyName); }/*from w w w . j a v a2s. c o m*/ propertyNames.add(idProperty); List<Type> propertyTypes = new ArrayList<Type>(); Type idType = metadata.getIdentifierType(); for (Type propertyType : metadata.getPropertyTypes()) { propertyTypes.add(propertyType); } propertyTypes.add(idType); Map<String, Type> types = new HashMap<String, Type>(); int j = 0; for (String propertyName : propertyNames) { types.put(propertyName, propertyTypes.get(j)); j++; } typePool.put(originalBean.getClass().getName(), types); } Map<String, Type> types = (Map<String, Type>) typePool.get(originalBean.getClass().getName()); Field idField = null; for (Field field : fields) { if (types.containsKey(field.getName())) { field.setAccessible(true); Type fieldType = types.get(field.getName()); if (fieldType.isCollectionType() || fieldType.isAnyType()) { //field.set(targetBean, null); //do nothing } else if (fieldType.isEntityType()) { Object newOriginalBean = field.get(originalBean); if (newOriginalBean == null) { field.set(targetBean, null); } else { Object newTargetBean = newOriginalBean.getClass().newInstance(); field.set(targetBean, newTargetBean); Field[] newFields = getFields(newOriginalBean.getClass()); performConvert(newOriginalBean, newTargetBean, newFields, method, em, txManager); } } else { field.set(targetBean, field.get(originalBean)); } if (field.getName().equals(idProperty)) { idField = field; } } } if (txManager != null) { Object temp = null; if (idField == null) { throw new Exception( "Unable to find an identity field for the entity: " + originalBean.getClass().getName()); } final Serializable primaryKey = (Serializable) idField.get(originalBean); if (primaryKey != null) { temp = em.find(originalBean.getClass(), primaryKey); } DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); TransactionStatus status = txManager.getTransaction(def); try { if (primaryKey != null) { if (temp != null && method.getName().equals("merge")) { targetBean = em.merge(targetBean); } else { SessionImplementor session = (SessionImplementor) em.getDelegate(); EntityPersister persister = session.getEntityPersister(targetBean.getClass().getName(), targetBean); IdentifierProperty ip = persister.getEntityMetamodel().getIdentifierProperty(); synchronized (ip) { IdentifierValue backupUnsavedValue = setUnsavedValue(ip, IdentifierValue.ANY); em.persist(targetBean); setUnsavedValue(ip, backupUnsavedValue); } } } else { targetBean = method.invoke(em, targetBean); } } catch (Throwable ex) { txManager.rollback(status); throw ex; } txManager.commit(status); } }
From source file:org.castafiore.designer.designable.table.EXTableConfigForm.java
License:Open Source License
private void initGrid(String clzz) { EXGrid grid = (EXGrid) getChild("grid"); if (grid != null) grid.remove();//from ww w.j a va 2s . c o m grid = new EXGrid("grid", 5, 1); grid.setAttribute("cellpadding", "0"); grid.setAttribute("cellspacing", "0"); grid.setStyle("border-collapse", "collapse"); grid.setStyle("font-size", "10px"); grid.getCell(0, 0).addChild(new EXCheckBox("checkall")); grid.getCell(0, 0).setAttribute("width", "1"); grid.getChildByIndex(0).addClass("ui-widget-header"); grid.getCell(1, 0).setText("Property"); grid.getCell(2, 0).setText("Label"); grid.getCell(3, 0).setText("Width"); grid.getCell(3, 0).setAttribute("align", "center"); grid.getCell(4, 0).setText("Edit"); grid.getCell(4, 0).setAttribute("width", "1"); grid.setAttribute("width", "100%"); addChild(grid); SessionFactory fact = BaseSpringUtil.getBeanOfType(SessionFactory.class); ClassMetadata meta = fact.getClassMetadata(clzz); String[] properties = meta.getPropertyNames(); int counter = 0; for (String property : properties) { Type type = meta.getPropertyType(property); if (!type.isAssociationType() && !type.isCollectionType() && !type.isComponentType() && !type.isEntityType()) { if (!ArrayUtils.contains(HIDDEN_FIELDS, property)) { EXRow row = grid.addRow(); if ((counter % 2) == 0) { row.addClass("ui-state-highlight"); } row.addInCell(0, new EXCheckBox("")); row.getChildByIndex(1).setText(property); row.addInCell(2, new EXInput("")); row.addInCell(3, new EXInput("")); row.addInCell(4, new EXCheckBox("")); row.getChildByIndex(2).setAttribute("width", "90"); row.getChildByIndex(2).getChildByIndex(0).setStyle("width", "88px"); row.getChildByIndex(2).getChildByIndex(0).setStyle("border", "solid 1px silver"); row.getChildByIndex(2).getChildByIndex(0).setStyle("height", "12px"); row.getChildByIndex(3).setAttribute("width", "40"); row.getChildByIndex(3).setAttribute("align", "center"); row.getChildByIndex(3).getChildByIndex(0).setStyle("width", "38px"); row.getChildByIndex(3).getChildByIndex(0).setStyle("border", "solid 1px silver"); row.getChildByIndex(3).getChildByIndex(0).setStyle("height", "12px"); counter++; } } } }