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();
}
Aggregations