Search in sources :

Example 46 with JoinColumn

use of javax.persistence.JoinColumn 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

JoinColumn (javax.persistence.JoinColumn)46 JoinTable (javax.persistence.JoinTable)17 Field (java.lang.reflect.Field)16 Method (java.lang.reflect.Method)14 ManyToOne (javax.persistence.ManyToOne)14 ReflectUtil (org.eweb4j.util.ReflectUtil)14 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)13 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)13 Test (org.junit.Test)12 JoinColumns (javax.persistence.JoinColumns)11 Column (javax.persistence.Column)10 OneToOne (javax.persistence.OneToOne)10 UniqueConstraint (javax.persistence.UniqueConstraint)9 ManyToMany (javax.persistence.ManyToMany)8 OneToMany (javax.persistence.OneToMany)6 DAOException (org.eweb4j.orm.dao.DAOException)6 Id (javax.persistence.Id)5 MapKeyJoinColumns (javax.persistence.MapKeyJoinColumns)5 HashMap (java.util.HashMap)4 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)4