use of org.hibernate.metadata.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class SequenceGeneratorCorruptionDetection method patchSequenceGeneratorInconsistencies.
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 {
idField = mappedClass.getDeclaredField(idProperty);
} catch (NoSuchFieldException e) {
continue;
}
idField.setAccessible(true);
GenericGenerator genericAnnot = idField.getAnnotation(GenericGenerator.class);
TableGenerator tableAnnot = idField.getAnnotation(TableGenerator.class);
String segmentValue = null;
String tableName = null;
String segmentColumnName = null;
String valueColumnName = null;
if (genericAnnot != null && genericAnnot.strategy().equals(IdOverrideTableGenerator.class.getName())) {
// This is a BLC style ID generator
for (Parameter param : genericAnnot.parameters()) {
if (param.name().equals("segment_value")) {
segmentValue = param.value();
}
if (param.name().equals("table_name")) {
tableName = param.value();
}
if (param.name().equals("segment_column_name")) {
segmentColumnName = param.value();
}
if (param.name().equals("value_column_name")) {
valueColumnName = param.value();
}
}
// to redefine them. If they aren't defined in the annotation, glean them from the defaults
if (StringUtils.isBlank(tableName)) {
tableName = IdOverrideTableGenerator.DEFAULT_TABLE_NAME;
}
if (StringUtils.isBlank(segmentColumnName)) {
segmentColumnName = IdOverrideTableGenerator.DEFAULT_SEGMENT_COLUMN_NAME;
}
if (StringUtils.isBlank(valueColumnName)) {
valueColumnName = IdOverrideTableGenerator.DEFAULT_VALUE_COLUMN_NAME;
}
} else if (tableAnnot != null) {
// This is a traditional Hibernate generator
segmentValue = tableAnnot.pkColumnValue();
tableName = tableAnnot.table();
segmentColumnName = tableAnnot.pkColumnName();
valueColumnName = tableAnnot.valueColumnName();
}
if (!StringUtils.isEmpty(segmentValue) && !StringUtils.isEmpty(tableName) && !StringUtils.isEmpty(segmentColumnName) && !StringUtils.isEmpty(valueColumnName)) {
StringBuilder sb2 = new StringBuilder();
sb2.append("select ");
sb2.append(valueColumnName);
sb2.append(" from ");
if (!tableName.contains(".") && !StringUtils.isEmpty(defaultSchemaSequenceGenerator)) {
sb2.append(defaultSchemaSequenceGenerator);
sb2.append(".");
}
sb2.append(tableName);
sb2.append(" where ");
sb2.append(segmentColumnName);
sb2.append(" = '");
sb2.append(segmentValue);
sb2.append("'");
Long maxSequenceId = 0l;
boolean sequenceEntryExists = false;
List results2 = em.createNativeQuery(sb2.toString()).getResultList();
if (CollectionUtils.isNotEmpty(results2) && results2.get(0) != null) {
maxSequenceId = ((Number) results2.get(0)).longValue();
sequenceEntryExists = true;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Detecting id sequence state between " + mappedClass.getName() + " and " + segmentValue + " in " + tableName);
}
StringBuilder sb = new StringBuilder();
sb.append("select max(");
sb.append(idField.getName());
sb.append(") from ");
sb.append(mappedClass.getName());
sb.append(" entity");
List results;
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
if (context == null) {
context = new BroadleafRequestContext();
BroadleafRequestContext.setBroadleafRequestContext(context);
}
try {
context.setInternalIgnoreFilters(true);
results = em.createQuery(sb.toString()).getResultList();
} finally {
context.setInternalIgnoreFilters(false);
}
if (CollectionUtils.isNotEmpty(results) && results.get(0) != null) {
LOG.debug(String.format("Checking for sequence corruption on entity %s", segmentValue));
Long maxEntityId = BLCNumberUtils.toLong(results.get(0));
if (maxEntityId > maxSequenceId) {
LOG.error(String.format("The sequence value for %s in %s was found as %d (or an entry did not exist) but the actual max sequence in" + " %s's table was found as %d", segmentValue, tableName, maxSequenceId, mappedClass.getName(), maxEntityId));
if (automaticallyCorrectInconsistencies) {
long newMaxId = maxEntityId + 10;
if (sequenceEntryExists) {
String log = String.format("Correcting sequences for entity %s. Updating the sequence value" + " to %d", mappedClass.getName(), newMaxId);
LOG.warn(log);
StringBuilder updateQuery = new StringBuilder();
updateQuery.append("update ");
if (!tableName.contains(".") && !StringUtils.isEmpty(defaultSchemaSequenceGenerator)) {
sb2.append(defaultSchemaSequenceGenerator);
sb2.append(".");
}
updateQuery.append(tableName);
updateQuery.append(" set ");
updateQuery.append(valueColumnName);
updateQuery.append(" = ");
updateQuery.append(String.valueOf(newMaxId));
updateQuery.append(" where ");
updateQuery.append(segmentColumnName);
updateQuery.append(" = '");
updateQuery.append(segmentValue);
updateQuery.append("'");
int response = em.createNativeQuery(updateQuery.toString()).executeUpdate();
if (response <= 0) {
throw new RuntimeException("Unable to update " + tableName + " with the sequence generator id for " + segmentValue);
}
} else {
String log = String.format("Correcting sequences for entity %s. Did not find an entry in" + " %s, inserting the new sequence value as %d", mappedClass.getName(), tableName, newMaxId);
LOG.warn(log);
StringBuilder insertQuery = new StringBuilder();
insertQuery.append("insert into ");
if (!tableName.contains(".") && !StringUtils.isEmpty(defaultSchemaSequenceGenerator)) {
sb2.append(defaultSchemaSequenceGenerator);
sb2.append(".");
}
insertQuery.append(tableName);
insertQuery.append(" (" + segmentColumnName + "," + valueColumnName + ")");
insertQuery.append("values ('" + segmentValue + "','" + String.valueOf(newMaxId) + "')");
int response = em.createNativeQuery(insertQuery.toString()).executeUpdate();
if (response <= 0) {
throw new RuntimeException("Unable to update " + tableName + " with the sequence generator id for " + segmentValue);
}
}
} else {
String reason = "A data inconsistency has been detected between the " + tableName + " table and one or more entity tables for which it manages current max primary key values.\n" + "The inconsistency was detected between the managed class (" + mappedClass.getName() + ") and the identifier (" + segmentValue + ") in " + tableName + ". Broadleaf\n" + "has stopped startup of the application in order to allow you to resolve the issue and avoid possible data corruption. If you wish to disable this detection, you may\n" + "set the 'detect.sequence.generator.inconsistencies' property to false in your application's common.properties or common-shared.properties. If you would like for this component\n" + "to autocorrect these problems by setting the sequence generator value to a value greater than the max entity id, then set the 'auto.correct.sequence.generator.inconsistencies'\n" + "property to true in your application's common.properties or common-shared.properties. If you would like to provide a default schema to be used to qualify table names used in the\n" + "queries for this detection, set the 'default.schema.sequence.generator' property in your application's common.properties or common-shared.properties. Also, if you are upgrading\n" + "from 1.6 or below, please refer to http://docs.broadleafcommerce.org/current/1.6-to-2.0-Migration.html for important information regarding migrating your SEQUENCE_GENERATOR table.";
LOG.error("Broadleaf Commerce failed to start", new RuntimeException(reason));
System.exit(1);
}
}
}
}
}
}
use of org.hibernate.metadata.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class PersistenceServiceImpl method populateCaches.
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);
}
}
}
use of org.hibernate.metadata.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class DefaultFieldMetadataProvider method addMetadataFromFieldType.
@Override
public MetadataProviderResponse addMetadataFromFieldType(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
if (addMetadataFromFieldTypeRequest.getPresentationAttribute() != null) {
if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.UNKNOWN && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.BOOLEAN && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.INTEGER && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.DATE && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.STRING && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.MONEY && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.DECIMAL && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.FOREIGN_KEY && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.ADDITIONAL_FOREIGN_KEY) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), addMetadataFromFieldTypeRequest.getExplicitType(), addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.BOOLEAN || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Boolean.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Character.class)) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.BOOLEAN, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.INTEGER || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Byte.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Short.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Integer.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Long.class)) {
if (addMetadataFromFieldTypeRequest.getRequestedPropertyName().equals(addMetadataFromFieldTypeRequest.getIdProperty())) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.ID, SupportedFieldType.INTEGER, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.INTEGER, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
}
} else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.DATE || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Calendar.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Date.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Timestamp.class)) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.DATE, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.STRING || addMetadataFromFieldTypeRequest.getReturnedClass().equals(String.class)) {
if (addMetadataFromFieldTypeRequest.getRequestedPropertyName().equals(addMetadataFromFieldTypeRequest.getIdProperty())) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.ID, SupportedFieldType.STRING, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.STRING, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
}
} else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.MONEY || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Money.class)) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.MONEY, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.DECIMAL || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Double.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(BigDecimal.class)) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.DECIMAL, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.FOREIGN_KEY || addMetadataFromFieldTypeRequest.getForeignField() != null && addMetadataFromFieldTypeRequest.isPropertyForeignKey()) {
ClassMetadata foreignMetadata;
String foreignKeyClass;
String lookupDisplayProperty;
if (addMetadataFromFieldTypeRequest.getForeignField() == null) {
Class<?>[] entities = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getAllPolymorphicEntitiesFromCeiling(addMetadataFromFieldTypeRequest.getType().getReturnedClass());
foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory().getClassMetadata(entities[entities.length - 1]);
foreignKeyClass = entities[entities.length - 1].getName();
lookupDisplayProperty = ((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()).getLookupDisplayProperty();
if (StringUtils.isEmpty(lookupDisplayProperty) && AdminMainEntity.class.isAssignableFrom(entities[entities.length - 1])) {
lookupDisplayProperty = AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY;
}
if (StringUtils.isEmpty(lookupDisplayProperty)) {
lookupDisplayProperty = "name";
}
} else {
try {
foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory().getClassMetadata(Class.forName(addMetadataFromFieldTypeRequest.getForeignField().getForeignKeyClass()));
foreignKeyClass = addMetadataFromFieldTypeRequest.getForeignField().getForeignKeyClass();
lookupDisplayProperty = addMetadataFromFieldTypeRequest.getForeignField().getDisplayValueProperty();
if (StringUtils.isEmpty(lookupDisplayProperty) && AdminMainEntity.class.isAssignableFrom(Class.forName(foreignKeyClass))) {
lookupDisplayProperty = AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY;
}
if (StringUtils.isEmpty(lookupDisplayProperty)) {
lookupDisplayProperty = "name";
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
Class<?> foreignResponseType = foreignMetadata.getIdentifierType().getReturnedClass();
if (foreignResponseType.equals(String.class)) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.FOREIGN_KEY, SupportedFieldType.STRING, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.FOREIGN_KEY, SupportedFieldType.INTEGER, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
}
((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyProperty(foreignMetadata.getIdentifierPropertyName());
((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyClass(foreignKeyClass);
((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyDisplayValueProperty(lookupDisplayProperty);
} else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.ADDITIONAL_FOREIGN_KEY || addMetadataFromFieldTypeRequest.getAdditionalForeignFields() != null && addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition() >= 0) {
if (!addMetadataFromFieldTypeRequest.getType().isEntityType()) {
throw new IllegalArgumentException("Only ManyToOne and OneToOne fields can be marked as a " + "SupportedFieldType of ADDITIONAL_FOREIGN_KEY");
}
ClassMetadata foreignMetadata;
String foreignKeyClass;
String lookupDisplayProperty;
if (addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition() < 0) {
Class<?>[] entities = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getAllPolymorphicEntitiesFromCeiling(addMetadataFromFieldTypeRequest.getType().getReturnedClass());
foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory().getClassMetadata(entities[entities.length - 1]);
foreignKeyClass = entities[entities.length - 1].getName();
lookupDisplayProperty = ((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()).getLookupDisplayProperty();
if (StringUtils.isEmpty(lookupDisplayProperty) && AdminMainEntity.class.isAssignableFrom(entities[entities.length - 1])) {
lookupDisplayProperty = AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY;
}
if (StringUtils.isEmpty(lookupDisplayProperty)) {
lookupDisplayProperty = "name";
}
} else {
try {
foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory().getClassMetadata(Class.forName(addMetadataFromFieldTypeRequest.getAdditionalForeignFields()[addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition()].getForeignKeyClass()));
foreignKeyClass = addMetadataFromFieldTypeRequest.getAdditionalForeignFields()[addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition()].getForeignKeyClass();
lookupDisplayProperty = addMetadataFromFieldTypeRequest.getAdditionalForeignFields()[addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition()].getDisplayValueProperty();
if (StringUtils.isEmpty(lookupDisplayProperty) && AdminMainEntity.class.isAssignableFrom(Class.forName(foreignKeyClass))) {
lookupDisplayProperty = AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY;
}
if (StringUtils.isEmpty(lookupDisplayProperty)) {
lookupDisplayProperty = "name";
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
Class<?> foreignResponseType = foreignMetadata.getIdentifierType().getReturnedClass();
if (foreignResponseType.equals(String.class)) {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.ADDITIONAL_FOREIGN_KEY, SupportedFieldType.STRING, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
} else {
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.ADDITIONAL_FOREIGN_KEY, SupportedFieldType.INTEGER, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
}
((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyProperty(foreignMetadata.getIdentifierPropertyName());
((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyClass(foreignKeyClass);
((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyDisplayValueProperty(lookupDisplayProperty);
}
// return type not supported - just skip this property
return MetadataProviderResponse.HANDLED;
}
return MetadataProviderResponse.NOT_HANDLED;
}
use of org.hibernate.metadata.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class UpdateExecutor method executeTargetedCacheInvalidation.
/**
* @param em
* @param entityType
* @param ids
*/
public static void executeTargetedCacheInvalidation(EntityManager em, Class<?> entityType, List<Long> ids) {
Session session = em.unwrap(Session.class);
for (Long id : ids) {
session.getSessionFactory().getCache().evictEntity(entityType, id);
}
// update the timestamp cache for the table so that queries will be refreshed
ClassMetadata metadata = session.getSessionFactory().getClassMetadata(entityType);
String tableName = ((AbstractEntityPersister) metadata).getTableName();
UpdateTimestampsCache timestampsCache = em.unwrap(SessionImplementor.class).getFactory().getUpdateTimestampsCache();
if (timestampsCache != null) {
timestampsCache.invalidate(new Serializable[] { tableName });
}
}
use of org.hibernate.metadata.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class DynamicDaoHelperImpl method getPropertyNames.
@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;
}
Aggregations