Metadata

Each entity with its attributes is reflected in the application metadata which is used by the framework mechanisms to get a comprehensive information about the data model. For example, if some attribute is marked as read-only in metadata, corresponding UI components will be disabled.

Metadata is an internal mechanism of the framework and is rarely used in the application code directly.

The main entry point to the metadata API is the Metadata bean. It gives you access to the information about entities in the form of MetaClass and MetaProperty objects.

In the example below we print out the details of the Order entity attributes:

@Autowired
private Metadata metadata;

public void printOrderProperties() {
    MetaClass metaClass = metadata.getClass(Order.class); (1)
    for (MetaProperty metaProperty : metaClass.getProperties()) { (2)

        String propertyName = metaProperty.getName(); (3)
        MetaProperty.Type propertyType = metaProperty.getType(); (4)
        Class<?> javaType = metaProperty.getJavaType(); (5)
        Range propertyRange = metaProperty.getRange(); (6)

        String info = "name: " + propertyName +
                "\n type: " + propertyType +
                "\n Java type: " + javaType +
                "\n range: " + propertyRange;

        if (propertyRange.isClass()) { (7)
            MetaClass refMetaClass = propertyRange.asClass(); (8)
            Range.Cardinality cardinality = propertyRange.getCardinality(); (9)
            info += "\n reference to: " + refMetaClass;
            info += "\n cardinality: " + cardinality;

        } else if (propertyRange.isEnum()) { (10)
            Enumeration<?> enumeration = propertyRange.asEnumeration(); (11)
            info += "\n enum: " + enumeration;

        } else if (propertyRange.isDatatype()) { (12)
            Datatype<Object> propertyDatatype = propertyRange.asDatatype(); (13)
            info += "\n data type: " + propertyDatatype;
        }

        System.out.println(info);
    }
}
1 Metadata.getClass(javaClass) method return a MetaClass object corresponding to the entity Java class.
2 MetaClass.getProperties() method returns a collection of MetaProperty objects corresponding to all entity attributes.
3 MetaProperty.getName() method returns the entity attribute name.
4 MetaProperty.getType() method returns the attribute type: DATATYPE, ENUM, ASSOCIATION, COMPOSITION.
5 MetaProperty.getJavaType() method returns the Java class of the entity attribute.
6 MetaProperty.getRange() method returns the Range object that describes the target type of the entity attribute.
7 Range.isClass() method returns true if the entity attribute is a reference.
8 You can get the referenced entity meta-class using the Range.asClass() method.
9 Range.getCardinality() method returns the reference cardinality: ONE_TO_ONE, MANY_TO_ONE, ONE_TO_MANY, MANY_TO_MANY. You can also use Range.getCardinality().isMany() to determine if the attribute is a collection.
10 Range.isEnum() method returns true if the entity attribute is an enumeration.
11 Range.asEnumeration() method returns the Enumeration object that can be used to iterate over all enum constants.
12 Range.isDatatype() method returns true if the entity attribute is not a reference or an enumeration.
13 Range.asDatatype() method returns the datatype of the entity attribute. You can use it to format and parse attribute values.