Search in sources :

Example 1 with UpperIndex

use of org.jowidgets.cap.service.jpa.api.ddl.UpperIndex in project jo-client-platform by jo-source.

the class IndexDdlCreator method createIndexDdl.

public static String createIndexDdl(final String persistenceUnitName) {
    final StringBuilder builder = new StringBuilder();
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);
    for (final EntityType<?> entityType : emf.getMetamodel().getEntities()) {
        final Class<?> javaType = entityType.getJavaType();
        final Table table = javaType.getAnnotation(Table.class);
        final String tableName = table.name();
        final Field[] fields = javaType.getDeclaredFields();
        for (final Field field : fields) {
            final UpperIndex annotationUpper = field.getAnnotation(UpperIndex.class);
            if (null != annotationUpper) {
                final Column column = field.getAnnotation(Column.class);
                String columnName = null;
                if (null != column) {
                    columnName = column.name();
                } else {
                    final JoinColumn joinColumn = field.getAnnotation(JoinColumn.class);
                    if (joinColumn != null) {
                        columnName = joinColumn.name();
                    }
                }
                if (columnName != null) {
                    final boolean unique = annotationUpper.unique();
                    final String indexName = annotationUpper.name();
                    builder.append("CREATE ");
                    if (unique) {
                        builder.append("UNIQUE ");
                    }
                    builder.append("INDEX " + indexName + " ON " + tableName + "(UPPER(" + columnName + "));\n");
                }
            }
        }
    }
    return builder.toString();
}
Also used : Field(java.lang.reflect.Field) Table(javax.persistence.Table) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) JoinColumn(javax.persistence.JoinColumn) EntityManagerFactory(javax.persistence.EntityManagerFactory) UpperIndex(org.jowidgets.cap.service.jpa.api.ddl.UpperIndex)

Aggregations

Field (java.lang.reflect.Field)1 Column (javax.persistence.Column)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 JoinColumn (javax.persistence.JoinColumn)1 Table (javax.persistence.Table)1 UpperIndex (org.jowidgets.cap.service.jpa.api.ddl.UpperIndex)1