use of org.hibernate.metadata.ClassMetadata in project uPortal by Jasig.
the class PortalRawEventsAggregatorImpl method getCollectionRoles.
private List<String> getCollectionRoles(final SessionFactory sessionFactory, final Class<?> entityClass) {
List<String> collectionRoles = entityCollectionRoles.get(entityClass);
if (collectionRoles != null) {
return collectionRoles;
}
final com.google.common.collect.ImmutableList.Builder<String> collectionRolesBuilder = ImmutableList.builder();
final ClassMetadata classMetadata = sessionFactory.getClassMetadata(entityClass);
for (final Type type : classMetadata.getPropertyTypes()) {
if (type.isCollectionType()) {
collectionRolesBuilder.add(((CollectionType) type).getRole());
}
}
collectionRoles = collectionRolesBuilder.build();
entityCollectionRoles.put(entityClass, collectionRoles);
return collectionRoles;
}
use of org.hibernate.metadata.ClassMetadata in project dhis2-core by dhis2.
the class AbstractPropertyIntrospectorService method getPropertiesFromHibernate.
protected Map<String, Property> getPropertiesFromHibernate(Class<?> klass) {
updateJoinTables();
ClassMetadata classMetadata = sessionFactory.getClassMetadata(klass);
// is class persisted with hibernate
if (classMetadata == null) {
return new HashMap<>();
}
Map<String, Property> properties = new HashMap<>();
SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
MetadataImplementor metadataImplementor = HibernateMetadata.getMetadataImplementor();
if (metadataImplementor == null) {
return new HashMap<>();
}
PersistentClass persistentClass = metadataImplementor.getEntityBinding(klass.getName());
Iterator<?> propertyIterator = persistentClass.getPropertyClosureIterator();
while (propertyIterator.hasNext()) {
Property property = new Property(klass);
property.setRequired(false);
property.setPersisted(true);
property.setOwner(true);
org.hibernate.mapping.Property hibernateProperty = (org.hibernate.mapping.Property) propertyIterator.next();
Type type = hibernateProperty.getType();
property.setName(hibernateProperty.getName());
property.setCascade(hibernateProperty.getCascade());
property.setCollection(type.isCollectionType());
property.setSetterMethod(hibernateProperty.getSetter(klass).getMethod());
property.setGetterMethod(hibernateProperty.getGetter(klass).getMethod());
if (property.isCollection()) {
CollectionType collectionType = (CollectionType) type;
CollectionPersister persister = sessionFactoryImplementor.getCollectionPersister(collectionType.getRole());
property.setOwner(!persister.isInverse());
property.setManyToMany(persister.isManyToMany());
property.setMin(0d);
property.setMax(Double.MAX_VALUE);
if (property.isOwner()) {
property.setOwningRole(collectionType.getRole());
property.setInverseRole(roleToRole.get(collectionType.getRole()));
} else {
property.setOwningRole(roleToRole.get(collectionType.getRole()));
property.setInverseRole(collectionType.getRole());
}
}
if (SingleColumnType.class.isInstance(type) || CustomType.class.isInstance(type) || ManyToOneType.class.isInstance(type)) {
Column column = (Column) hibernateProperty.getColumnIterator().next();
property.setUnique(column.isUnique());
property.setRequired(!column.isNullable());
property.setMin(0d);
property.setMax((double) column.getLength());
property.setLength(column.getLength());
if (TextType.class.isInstance(type)) {
property.setMin(0d);
property.setMax((double) Integer.MAX_VALUE);
property.setLength(Integer.MAX_VALUE);
} else if (IntegerType.class.isInstance(type)) {
property.setMin(0d);
property.setMax((double) Integer.MAX_VALUE);
property.setLength(Integer.MAX_VALUE);
} else if (LongType.class.isInstance(type)) {
property.setMin(0d);
property.setMax((double) Long.MAX_VALUE);
property.setLength(Integer.MAX_VALUE);
} else if (DoubleType.class.isInstance(type)) {
property.setMin(0d);
property.setMax(Double.MAX_VALUE);
property.setLength(Integer.MAX_VALUE);
}
}
if (ManyToOneType.class.isInstance(type)) {
property.setManyToOne(true);
property.setRequired(property.isRequired() && !property.isCollection());
if (property.isOwner()) {
property.setOwningRole(klass.getName() + "." + property.getName());
} else {
property.setInverseRole(klass.getName() + "." + property.getName());
}
} else if (OneToOneType.class.isInstance(type)) {
property.setOneToOne(true);
}
properties.put(property.getName(), property);
}
return properties;
}
Aggregations