Refine search
@Override public Field getIdField(Class<?> clazz, Session session) { clazz = getNonProxyImplementationClassIfNecessary(clazz); ClassMetadata metadata = session.getSessionFactory().getClassMetadata(clazz); Field idField = ReflectionUtils.findField(clazz, metadata.getIdentifierPropertyName()); idField.setAccessible(true); return idField; } }
@Override public Map<String, Object> getIdMetadata(Class<?> entityClass, HibernateEntityManager entityManager) { entityClass = getNonProxyImplementationClassIfNecessary(entityClass); Map<String, Object> response = new HashMap<>(); SessionFactory sessionFactory = entityManager.getSession().getSessionFactory(); ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass); if (metadata == null) { return null; } String idProperty = metadata.getIdentifierPropertyName(); response.put("name", idProperty); Type idType = metadata.getIdentifierType(); response.put("type", idType); return response; }
public MetamodelUtilFromSessionFactory(SessionFactory factory) { // Go through all the entities for (String entityClassName: factory.getAllClassMetadata().keySet()) { ClassMetadata entityData = factory.getClassMetadata(entityClassName); classToEntityName.put(entityData.getMappedClass(), entityData.getEntityName()); classNameToEntityName.put(entityClassName, entityData.getEntityName()); //System.out.println(entityClassName + " " + entityData.getMappedClass().getCanonicalName() + " " + entityData.getEntityName()); // TODO: It turns out all three values are the same, but I think it's ok for now. scanClassMetadata(entityData); } }
/** * Add parent and child entity names so that we know how to rearrange dependencies * * @param action The action being sorted * @param batchIdentifier The batch identifier of the entity affected by the action */ private void addParentChildEntityNames(AbstractEntityInsertAction action, BatchIdentifier batchIdentifier) { Object[] propertyValues = action.getState(); ClassMetadata classMetadata = action.getPersister().getClassMetadata(); if ( classMetadata != null ) { Type[] propertyTypes = classMetadata.getPropertyTypes(); Type identifierType = classMetadata.getIdentifierType(); for ( int i = 0; i < propertyValues.length; i++ ) { Object value = propertyValues[i]; Type type = propertyTypes[i]; addParentChildEntityNameByPropertyAndValue( action, batchIdentifier, type, value ); } if ( identifierType.isComponentType() ) { CompositeType compositeType = (CompositeType) identifierType; Type[] compositeIdentifierTypes = compositeType.getSubtypes(); for ( Type type : compositeIdentifierTypes ) { addParentChildEntityNameByPropertyAndValue( action, batchIdentifier, type, null ); } } } }
protected void patchSequenceGeneratorInconsistencies(EntityManager em, Session session) { SessionFactory sessionFactory = session.getSessionFactory(); for (Object item : sessionFactory.getAllClassMetadata().values()) { ClassMetadata metadata = (ClassMetadata) item; String idProperty = metadata.getIdentifierPropertyName(); Class<?> mappedClass = metadata.getMappedClass(); Field idField; try {
public boolean isOrderFrequencyInUse(OrderFrequency orderFrequency) { Map<String, ClassMetadata> metadata = sessionFactory.getAllClassMetadata(); for (ClassMetadata classMetadata : metadata.values()) { Class<?> entityClass = classMetadata.getMappedClass(); if (Order.class.equals(entityClass)) { String[] names = classMetadata.getPropertyNames(); for (String name : names) { if (classMetadata.getPropertyType(name).getReturnedClass().equals(OrderFrequency.class)) { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass); criteria.add(Restrictions.eq(name, orderFrequency)); criteria.setMaxResults(1);
public void validate(Object object, Errors errors) throws DAOException { Class entityClass = object.getClass(); ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass); if (metadata != null) { String[] propNames = metadata.getPropertyNames(); Object identifierType = metadata.getIdentifierType(); String identifierName = metadata.getIdentifierPropertyName(); if (identifierType instanceof StringType || identifierType instanceof TextType) { int maxLength = getMaximumPropertyLength(entityClass, identifierName); String identifierValue = (String) metadata.getIdentifier(object, (SessionImplementor) sessionFactory.getCurrentSession()); if (identifierValue != null) { int identifierLength = identifierValue.length(); Type propType = metadata.getPropertyType(propName); if (propType instanceof StringType || propType instanceof TextType) { String propertyValue = (String) metadata.getPropertyValue(object, propName); if (propertyValue != null) { int maxLength = getMaximumPropertyLength(entityClass, propName); FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode(); sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL); try { for (Validator validator : getValidators(object)) { sessionFactory.getCurrentSession().setFlushMode(previousFlushMode);
private Object getFeature(String featureId) throws HibernateLayerException { Session session = getSessionFactory().getCurrentSession(); return session.get(getFeatureInfo().getDataSourceName(), (Serializable) ConvertUtils.convert(featureId, getEntityMetadata().getIdentifierType().getReturnedClass())); }
/** * 通过Updater更新对象 * * @param updater * @return */ @SuppressWarnings("unchecked") public T updateByUpdater(Updater<T> updater) { ClassMetadata cm = getSessionFactory().getClassMetadata(getEntityClass()); T bean = updater.getBean(); T po = getSession().get(getEntityClass(), cm.getIdentifier(bean)); updaterCopyToPersistentObject(updater, po, cm); return po; }
public static void update(final Session session, final Object entity) { // if the given instance is in session, nothing to do if (session.contains(entity)) return; // check if there is already a different attached instance representing the same row final ClassMetadata classMetadata = session.getSessionFactory().getClassMetadata(entity.getClass()); final Serializable identifier = classMetadata.getIdentifier(entity, (SessionImplementor) session); final Object sessionEntity = session.load(entity.getClass(), identifier); // override changes, last call to update wins if (sessionEntity != null) session.evict(sessionEntity); session.update(entity); }
private Object loadIndexByContent(Content content) { ContentContainer container = content.getContainer(); if (container != null) { Object owner = container.getOwner(); if (owner != null) { String ownerClassName = Hibernate.getClass(owner).getName(); ClassMetadata meta = getIndexClassMetadata(ownerClassName); if (meta != null) { return sessionFactory.getCurrentSession().get(meta.getEntityName(), content.getId()); } } } return null; }
public static Serializable getId(SessionFactory sessionFactory, Object bean) { Class<?> clazz = Hibernate.getClass(bean); ClassMetadata metadata = sessionFactory.getClassMetadata(clazz); return metadata.getIdentifier(bean, (SessionImplementor) sessionFactory.getCurrentSession()); }
.getPropertyValue( object, propertyName, session.getEntityMode() ); .getPropertyType( propertyName ); id = session.save( type.getAssociatedEntityName(), associatedObject ); if ( session.contains(object) ) {
@Override public Object newInstance(String id) throws LayerException { try { Serializable ser = (Serializable) ConvertUtils.convert(id, getEntityMetadata().getIdentifierType() .getReturnedClass()); return getEntityMetadata().instantiate(ser, (SessionImplementor) getSessionFactory().getCurrentSession()); } catch (Exception e) { // NOSONAR throw new LayerException(e, ExceptionCode.HIBERNATE_CANNOT_CREATE_POJO, getFeatureInfo() .getDataSourceName()); } }
Session session = entityManager.unwrap(Session.class); ClassMetadata metadata = session.getSessionFactory() .getClassMetadata(value.getClass()); String idName = metadata.getIdentifierPropertyName(); Serializable id = metadata.getIdentifier(value, null); metadata.getPropertyValue(value, property))); FlushMode flushMode = session.getFlushMode(); session.setFlushMode(FlushMode.MANUAL); List<?> results = criteria.getExecutableCriteria(session).list(); Number count = (Number) results.iterator().next();
@Override public Object getId(String name) throws LayerException { return metadata.getIdentifier(object, (SessionImplementor) sessionFactory.getCurrentSession()); }
/** * @param entity an actual entity object, not a proxy! */ public String toString(Object entity, EntityMode entityMode) throws HibernateException { // todo : this call will not work for anything other than pojos! ClassMetadata cm = factory.getClassMetadata( entity.getClass() ); if ( cm==null ) return entity.getClass().getName(); Map result = new HashMap(); if ( cm.hasIdentifierProperty() ) { result.put( cm.getIdentifierPropertyName(), cm.getIdentifierType().toLoggableString( cm.getIdentifier( entity, entityMode ), factory ) ); } Type[] types = cm.getPropertyTypes(); String[] names = cm.getPropertyNames(); Object[] values = cm.getPropertyValues( entity, entityMode ); for ( int i=0; i<types.length; i++ ) { if ( !names[i].startsWith("_") ) { String strValue = values[i]==LazyPropertyInitializer.UNFETCHED_PROPERTY ? values[i].toString() : types[i].toLoggableString( values[i], factory ); result.put( names[i], strValue ); } } return cm.getEntityName() + result.toString(); }
try { foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory(). getClassMetadata(Class.forName(addMetadataFromFieldTypeRequest.getForeignField() .getForeignKeyClass())); foreignKeyClass = addMetadataFromFieldTypeRequest.getForeignField().getForeignKeyClass(); Class<?> foreignResponseType = foreignMetadata.getIdentifierType().getReturnedClass(); if (foreignResponseType.equals(String.class)) { metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), setForeignKeyProperty(foreignMetadata.getIdentifierPropertyName()); ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())) .setForeignKeyClass(foreignKeyClass); getClassMetadata(entities[entities.length - 1]); foreignKeyClass = entities[entities.length - 1].getName(); lookupDisplayProperty = ((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()). Class<?> foreignResponseType = foreignMetadata.getIdentifierType().getReturnedClass(); if (foreignResponseType.equals(String.class)) { metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), setForeignKeyProperty(foreignMetadata.getIdentifierPropertyName()); ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())). setForeignKeyClass(foreignKeyClass);
public Class<?> getPrimaryKeyClass(Class<?> entityClass) { ClassMetadata cm = sessionFactory.getClassMetadata(entityClass); if (cm == null) { // default to String return String.class; } else { return cm.getIdentifierType().getReturnedClass(); } }
public Metadata getPropertyType(String property) { Type pType = metadata.getPropertyType(property); Class<?> pCollectionType = null; if (pType.isCollectionType()) { pType = ((CollectionType)pType).getElementType((SessionFactoryImplementor) sessionFactory); pCollectionType = pType.getReturnedClass(); } if (pType.isEntityType()) { return new HibernateEntityMetadata(sessionFactory, sessionFactory.getClassMetadata(((EntityType)pType).getName()), pCollectionType); } else { return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType); } }