ENH: support for multiple indices added

This commit is contained in:
Roland Praml committed 2016-07-30 16:06:01 +02:00
1 parent 85288a0652
commit 84f32c2ace
4 files changed
+67 -5

No files matched your search

@@ -7,6 +7,7 @@ import com.avaje.ebean.annotation.Draftable;
import com.avaje.ebean.annotation.DraftableElement;
import com.avaje.ebean.annotation.History;
import com.avaje.ebean.annotation.Index;
import com.avaje.ebean.annotation.Indices;
import com.avaje.ebean.annotation.ReadAudit;
import com.avaje.ebean.annotation.UpdateMode;
import com.avaje.ebean.annotation.View;
@@ -118,6 +119,13 @@ public class AnnotationClass extends AnnotationParser {
descriptor.setName("Embeddable:" + cls.getSimpleName());
}
Indices indices = AnnotationBase.findAnnotation(cls, Indices.class);
if (indices != null) {
for (Index index: indices.indices()) {
descriptor.addIndex(new IndexDefinition(index.columnNames(), index.name(), index.unique()));
}
}
Index index = AnnotationBase.findAnnotation(cls,Index.class);
if (index != null) {
descriptor.addIndex(new IndexDefinition(index.columnNames(), index.name(), index.unique()));
@@ -310,15 +310,46 @@ public class AnnotationFields extends AnnotationParser {
}
}
Indices indices = get(prop, Indices.class);
if (indices != null) {
for (Index index: indices.indices()) {
addIndex(prop, index);
}
}
Index index = get(prop, Index.class);
if (index != null) {
if (hasRelationshipItem(prop)) {
throw new RuntimeException("Can't use Index on foreign key relationships.");
}
descriptor.addIndex(new IndexDefinition(prop.getDbColumn(), index.name(), index.unique()));
addIndex(prop, index);
}
}
private void addIndex(DeployBeanProperty prop, Index index) {
String[] columnNames;
if (index.columnNames() == null || index.columnNames().length == 0) {
columnNames = new String[] {prop.getDbColumn()};
} else {
columnNames = new String[index.columnNames().length];
int i=0;
int found = 0;
for (String colName : index.columnNames()) {
if (colName.equals("${fa}") || colName.equals(prop.getDbColumn())) {
columnNames[i++] = prop.getDbColumn();
found++;
} else {
columnNames[i++] = colName;
}
}
if (found != 1) {
throw new RuntimeException("DB-columname has to be specified exactly one time in columnNames.");
}
}
if (columnNames.length == 1 && hasRelationshipItem(prop)) {
throw new RuntimeException("Can't use Index on foreign key relationships.");
}
descriptor.addIndex(new IndexDefinition(columnNames, index.name(), index.unique()));
}
private void readJsonAnnotations(DeployBeanProperty prop) {
if (jacksonAnnotationsPresent) {
com.fasterxml.jackson.annotation.JsonIgnore jsonIgnore = get(prop, com.fasterxml.jackson.annotation.JsonIgnore.class);