Example usage for javax.persistence.metamodel SingularAttribute isOptional

List of usage examples for javax.persistence.metamodel SingularAttribute isOptional

Introduction

In this page you can find the example usage for javax.persistence.metamodel SingularAttribute isOptional.

Prototype

boolean isOptional();

Source Link

Document

Can the attribute be null.

Usage

From source file:org.apache.click.extras.jpa.JpaForm.java

/**
 * Applies the <tt>ClassMetadata</tt> validation database meta data to the
 * form fields./*from   w w  w .j  a va2  s  . co m*/
 * <p/>
 * The field validation attributes include:
 * <ul>
 * <li>required - is a mandatory field and cannot be null</li>
 * </ul>
 */
protected void applyMetaData() {
    if (metaDataApplied) {
        return;
    }

    try {
        Class valueClass = Class.forName(classField.getValue());

        /*
        String classname = getClassname(valueClass);
                
        Metamodel metadata =
        ((HibernateEntityManagerFactory) getEntityManagerFactory()).getSessionFactory().getMetamodel(classname);
                
        String[] propertyNames = metadata.getPropertyNames();
                
        boolean[] propertyNullability = metadata.getPropertyNullability();
                 
        for (int i = 0; i < propertyNames.length; i++) {
        Field field = getField(propertyNames[i]);
        if (field != null) {
            field.setRequired(propertyNullability[i]);
        }
        }
         */
        Metamodel classMetadata = getEntityManager().getMetamodel();
        EntityType entityType = classMetadata.entity(valueClass);

        Set<SingularAttribute> attrs = entityType.getAttributes();

        for (SingularAttribute a : attrs) {
            Field field = getField(a.getName());
            if (field != null) {
                field.setRequired(a.isOptional());
            }
        }
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException(cnfe);
    }

    metaDataApplied = true;
}