/** * Return the values of the mapped properties of the object */ @SuppressWarnings( {"UnusedDeclaration"}) Object[] getPropertyValuesToInsert(Object entity, Map mergeMap, SharedSessionContractImplementor session) throws HibernateException;
/** * Inject the identifier value into the given entity. * * @param entity The entity to inject with the identifier value. * @param id The value to be injected as the identifier. * @param session The session from which is requests originates * * @deprecated Use {@link #setIdentifier(Object, Serializable, SharedSessionContractImplementor)} instead */ @Deprecated default void setIdentifier(Object entity, Serializable id, SessionImplementor session) { setIdentifier( entity, id, (SharedSessionContractImplementor) session ); }
/** * Get the identifier of an instance (throw an exception if no identifier property) * * @param entity The entity for which to get the identifier * @param session The session from which the request originated * * @return The identifier * * @deprecated Use {@link #getIdentifier(Object, SharedSessionContractImplementor)} instead */ @Deprecated default Serializable getIdentifier(Object entity, SessionImplementor session) { return getIdentifier( entity, (SharedSessionContractImplementor) session ); }
private void scanClassMetadata(ClassMetadata meta) { Class<?> entityClass = meta.getMappedClass(); String[] names = meta.getPropertyNames(); Type[] types = meta.getPropertyTypes(); for (int n = 0; n < names.length; n++) { String fieldName = names[n]; Type type = types[n]; registerEntityField(entityClass, fieldName, type); } if (meta.getIdentifierPropertyName() != null) registerEntityField(entityClass, meta.getIdentifierPropertyName(), meta.getIdentifierType()); //System.out.println(names + " " + types); }
@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; }
for (Object item : sessionFactory.getAllClassMetadata().values()) { ClassMetadata metadata = (ClassMetadata) item; String idProperty = metadata.getIdentifierPropertyName(); Class<?> mappedClass = metadata.getMappedClass(); Field idField; try {
@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; } }
/** * 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 ); } } } }
@Override public List<String> getPropertyNames(Class<?> entityClass, HibernateEntityManager entityManager) { entityClass = getNonProxyImplementationClassIfNecessary(entityClass); ClassMetadata metadata = getSessionFactory(entityManager).getClassMetadata(entityClass); List<String> propertyNames = new ArrayList<>(); Collections.addAll(propertyNames, metadata.getPropertyNames()); return propertyNames; }
protected void populateCaches(String targetMode, Map<String, Object> managerMap) { final EntityManager em = getEntityManager(managerMap); final PlatformTransactionManager txManager = getTransactionManager(managerMap); final EJB3ConfigurationDao ejb3ConfigurationDao = getEJB3ConfigurationDao(managerMap); SessionFactory sessionFactory = em.unwrap(Session.class).getSessionFactory(); for (Object item : sessionFactory.getAllClassMetadata().values()) { ClassMetadata metadata = (ClassMetadata) item; Class<?> mappedClass = metadata.getMappedClass(); String managerCacheKey = buildManagerCacheKey(targetMode, mappedClass); ENTITY_MANAGER_CACHE.put(managerCacheKey, em); TRANSACTION_MANAGER_CACHE.put(managerCacheKey, txManager); String ejb3ConfigDaoCacheKey = buildEJB3ConfigDaoCacheKey(mappedClass); if (!EJB3_CONFIG_DAO_CACHE.containsKey(ejb3ConfigDaoCacheKey)) { EJB3_CONFIG_DAO_CACHE.put(ejb3ConfigDaoCacheKey, ejb3ConfigurationDao); } } }
@Override public void nullSafeSet( PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { String entityName = session.getFactory().getClassMetadata((Class) value).getEntityName(); Loadable entityPersister = (Loadable) session.getFactory().getEntityPersister(entityName); underlyingType.nullSafeSet(st, entityPersister.getDiscriminatorValue(), index, session); }
@Override public List<Type> getPropertyTypes(Class<?> entityClass, HibernateEntityManager entityManager) { entityClass = getNonProxyImplementationClassIfNecessary(entityClass); ClassMetadata metadata = getSessionFactory(entityManager).getClassMetadata(entityClass); List<Type> propertyTypes = new ArrayList<>(); Collections.addAll(propertyTypes, metadata.getPropertyTypes()); return propertyTypes; }
private boolean copyState(Object entity, Type[] types, Object[] state, SessionFactory sf) { // copy the entity state into the state array and return true if the state has changed ClassMetadata metadata = sf.getClassMetadata( entity.getClass() ); Object[] newState = metadata.getPropertyValues( entity ); int size = newState.length; boolean isDirty = false; for ( int index = 0; index < size; index++ ) { if ( ( state[index] == LazyPropertyInitializer.UNFETCHED_PROPERTY && newState[index] != LazyPropertyInitializer.UNFETCHED_PROPERTY ) || ( state[index] != newState[index] && !types[index].isEqual( state[index], newState[index] ) ) ) { isDirty = true; state[index] = newState[index]; } } return isDirty; }
/** * Create a class instance initialized with the given identifier * * @param id The identifier value to use (may be null to represent no value) * @param session The session from which the request originated. * * @return The instantiated entity. * * @deprecated (since 5.3) Use the form accepting SharedSessionContractImplementor * instead */ @Deprecated default Object instantiate(Serializable id, SessionImplementor session) { return instantiate( id, (SharedSessionContractImplementor) session ); }
@Test public void testMappingProperties() { ClassMetadata metaData = sessionFactory().getClassMetadata( Citizen.class ); assertTrue( "Class should have a natural key", metaData .hasNaturalIdentifier() ); int[] propertiesIndex = metaData.getNaturalIdentifierProperties(); assertTrue( "Wrong number of elements", propertiesIndex.length == 2 ); }
Class<?> foreignResponseType = foreignMetadata.getIdentifierType().getReturnedClass(); if (foreignResponseType.equals(String.class)) { metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), setForeignKeyProperty(foreignMetadata.getIdentifierPropertyName()); ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())) .setForeignKeyClass(foreignKeyClass); Class<?> foreignResponseType = foreignMetadata.getIdentifierType().getReturnedClass(); if (foreignResponseType.equals(String.class)) { metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), setForeignKeyProperty(foreignMetadata.getIdentifierPropertyName()); ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())). setForeignKeyClass(foreignKeyClass);
@Override public Field getIdField(Class<?> clazz, EntityManager em) { clazz = getNonProxyImplementationClassIfNecessary(clazz); ClassMetadata metadata = em.unwrap(Session.class).getSessionFactory().getClassMetadata(clazz); Field idField = ReflectionUtils.findField(clazz, metadata.getIdentifierPropertyName()); idField.setAccessible(true); return idField; }
ClassMetadata classMetadata = sessionFactory.getClassMetadata(AppTaskConfig.class); String[] propertyNames = classMetadata.getPropertyNames();
for (Object item : sessionFactory.getAllClassMetadata().values()) { ClassMetadata metadata = (ClassMetadata) item; Class<?> mappedClass = metadata.getMappedClass(); if (mappedClass != null && ceilingClass.isAssignableFrom(mappedClass)) { entities.add(mappedClass);
@Test public void testMappingProperties() { log.warn("Commented out test"); ClassMetadata metaData = sessionFactory().getClassMetadata( NaturalIdOnManyToOne.class ); assertTrue( "Class should have a natural key", metaData .hasNaturalIdentifier() ); int[] propertiesIndex = metaData.getNaturalIdentifierProperties(); assertTrue( "Wrong number of elements", propertiesIndex.length == 1 ); }