diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java b/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java index d1753f6a5..8e8610751 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java @@ -104,7 +104,7 @@ public class MTable { /** * Compound unique constraints. */ - private List compoundUniqueConstraints = new ArrayList(); + private List uniqueConstraints = new ArrayList(); /** * Compound foreign keys. @@ -216,7 +216,7 @@ public class MTable { createTable.getForeignKey().add(compoundKey.createForeignKey()); } - for (MCompoundUniqueConstraint constraint : compoundUniqueConstraints) { + for (MCompoundUniqueConstraint constraint : uniqueConstraints) { UniqueConstraint uq = new UniqueConstraint(); uq.setName(constraint.getName()); String[] columns = constraint.getColumns(); @@ -404,8 +404,8 @@ public class MTable { return columns; } - public List getCompoundUniqueConstraints() { - return compoundUniqueConstraints; + public List getUniqueConstraints() { + return uniqueConstraints; } public List getCompoundKeys() { @@ -476,21 +476,21 @@ public class MTable { } /** - * Add a compound unique constraint. + * Add a unique constraint. */ - public void addCompoundUniqueConstraint(String[] columns, boolean oneToOne, String constraintName) { - compoundUniqueConstraints.add(new MCompoundUniqueConstraint(columns, oneToOne, constraintName)); + public void addUniqueConstraint(String[] columns, boolean oneToOne, String constraintName) { + uniqueConstraints.add(new MCompoundUniqueConstraint(columns, oneToOne, constraintName)); } /** - * Add a compound unique constraint. + * Add a unique constraint. */ - public void addCompoundUniqueConstraint(List columns, boolean oneToOne, String constraintName) { + public void addUniqueConstraint(List columns, boolean oneToOne, String constraintName) { String[] cols = new String[columns.size()]; for (int i = 0; i < columns.size(); i++) { cols[i] = columns.get(i).getName(); } - addCompoundUniqueConstraint(cols, oneToOne, constraintName); + addUniqueConstraint(cols, oneToOne, constraintName); } /** diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java index da2da0a86..60185d7ab 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java @@ -10,7 +10,7 @@ import com.avaje.ebeaninternal.server.deploy.BeanProperty; import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany; import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne; import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound; -import com.avaje.ebeaninternal.server.deploy.CompoundUniqueConstraint; +import com.avaje.ebeaninternal.server.deploy.IndexDefinition; import com.avaje.ebeaninternal.server.deploy.InheritInfo; import com.avaje.ebeaninternal.server.deploy.TableJoinColumn; import com.avaje.ebeaninternal.server.deploy.id.ImportedId; @@ -45,30 +45,30 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor { this.ctx = ctx; this.table = table; this.beanDescriptor = beanDescriptor; - addCompoundUniqueConstraint(beanDescriptor.getCompoundUniqueConstraints()); + addIndexes(beanDescriptor.getIndexDefinitions()); } /** * Add unique constraints defined via JPA UniqueConstraint annotations. */ - private void addCompoundUniqueConstraint(CompoundUniqueConstraint[] constraints) { + private void addIndexes(IndexDefinition[] indexes) { - if (constraints != null) { - for (int i = 0; i < constraints.length; i++) { - CompoundUniqueConstraint constraint = constraints[i]; - String[] columns = constraint.getColumns(); + if (indexes != null) { + for (int i = 0; i < indexes.length; i++) { + IndexDefinition index = indexes[i]; + String[] columns = index.getColumns(); indexSet.add(columns); - if (constraint.isUnique()) { - String uqName = constraint.getName(); + if (index.isUnique()) { + String uqName = index.getName(); if (uqName == null || uqName.trim().isEmpty()) { uqName = determineUniqueConstraintName(columns); } - table.addCompoundUniqueConstraint(columns, false, uqName); + table.addUniqueConstraint(columns, false, uqName); } else { // 'just' an index (not a unique constraint) - String idxName = constraint.getName(); + String idxName = index.getName(); if (idxName == null || idxName.trim().isEmpty()) { idxName = determineIndexName(columns); } @@ -216,7 +216,7 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor { } else { String uqName = determineUniqueConstraintName(p.getName()); - table.addCompoundUniqueConstraint(modelColumns, true, uqName); + table.addUniqueConstraint(modelColumns, true, uqName); indexSetAdd(modelColumns); } } @@ -260,15 +260,6 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor { col.setCheckConstraintName(determineCheckConstraintName(col.getName())); } - String indexName = p.getIndexName(); - if (indexName != null) { - // single column non-unique index - if (indexName.trim().isEmpty()) { - indexName = determineIndexName(col.getName()); - } - ctx.addIndex(indexName, table.getName(), p.getDbColumn()); - } - lastColumn = col; table.addColumn(col); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java index 4a1173860..face09faf 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java @@ -176,7 +176,7 @@ public class BeanDescriptor implements MetaBeanInfo, BeanType { */ private final ConcurrencyMode concurrencyMode; - private final CompoundUniqueConstraint[] compoundUniqueConstraints; + private final IndexDefinition[] indexDefinitions; private final String[] dependentTables; @@ -431,7 +431,7 @@ public class BeanDescriptor implements MetaBeanInfo, BeanType { this.selectLastInsertedId = deploy.getSelectLastInsertedId(); this.concurrencyMode = deploy.getConcurrencyMode(); this.updateChangesOnly = deploy.isUpdateChangesOnly(); - this.compoundUniqueConstraints = deploy.getCompoundUniqueConstraints(); + this.indexDefinitions = deploy.getIndexDefinitions(); this.readAuditing = deploy.isReadAuditing(); this.draftable = deploy.isDraftable(); @@ -2175,8 +2175,8 @@ public class BeanDescriptor implements MetaBeanInfo, BeanType { /** * Return the compound unique constraints. */ - public CompoundUniqueConstraint[] getCompoundUniqueConstraints() { - return compoundUniqueConstraints; + public IndexDefinition[] getIndexDefinitions() { + return indexDefinitions; } /** diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java index 9bbae9675..7138ebf1b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java @@ -255,10 +255,6 @@ public class BeanProperty implements ElPropertyValue, Property { final String softDeleteDbPredicate; - final boolean indexed; - - final String indexName; - public BeanProperty(DeployBeanProperty deploy) { this(null, deploy); } @@ -268,8 +264,6 @@ public class BeanProperty implements ElPropertyValue, Property { this.descriptor = descriptor; this.name = InternString.intern(deploy.getName()); this.propertyIndex = deploy.getPropertyIndex(); - this.indexed = deploy.isIndexed(); - this.indexName = deploy.getIndexName(); this.unidirectionalShadow = deploy.isUndirectionalShadow(); this.discriminator = deploy.isDiscriminator(); this.localEncrypted = deploy.isLocalEncrypted(); @@ -370,9 +364,6 @@ public class BeanProperty implements ElPropertyValue, Property { this.descriptor = source.descriptor; this.name = InternString.intern(source.getName()); this.propertyIndex = source.propertyIndex; - - this.indexed = source.isIndexed(); - this.indexName = source.getIndexName(); this.dbColumn = InternString.intern(override.getDbColumn()); // override with sqlFormula not currently supported this.sqlFormulaJoin = null; @@ -631,14 +622,6 @@ public class BeanProperty implements ElPropertyValue, Property { return this; } - public boolean isIndexed() { - return indexed; - } - - public String getIndexName() { - return indexName; - } - /** * Return true if this object is part of an inheritance hierarchy. */ diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/CompoundUniqueConstraint.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/IndexDefinition.java similarity index 69% rename from src/main/java/com/avaje/ebeaninternal/server/deploy/CompoundUniqueConstraint.java rename to src/main/java/com/avaje/ebeaninternal/server/deploy/IndexDefinition.java index 2613c6a34..8e1f86c7b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/CompoundUniqueConstraint.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/IndexDefinition.java @@ -3,7 +3,7 @@ package com.avaje.ebeaninternal.server.deploy; /** * Holds multiple column unique constraints defined for an entity. */ -public class CompoundUniqueConstraint { +public class IndexDefinition { private final String[] columns; @@ -11,7 +11,16 @@ public class CompoundUniqueConstraint { private final boolean unique; - public CompoundUniqueConstraint(String[] columns, String name, boolean unique) { + /** + * A single column index. + */ + public IndexDefinition(String column, String name, boolean unique) { + this.columns = new String[]{column}; + this.unique = unique; + this.name = name; + } + + public IndexDefinition(String[] columns, String name, boolean unique) { this.columns = columns; this.unique = unique; this.name = name; @@ -20,7 +29,7 @@ public class CompoundUniqueConstraint { /** * Create a unique constraint given the column names. */ - public CompoundUniqueConstraint(String[] columns) { + public IndexDefinition(String[] columns) { this.columns = columns; this.unique = true; this.name = null; diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java index 72ce1a313..3fd22f4da 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java @@ -23,7 +23,7 @@ import com.avaje.ebeaninternal.server.deploy.ChainedBeanPersistController; import com.avaje.ebeaninternal.server.deploy.ChainedBeanPersistListener; import com.avaje.ebeaninternal.server.deploy.ChainedBeanPostLoad; import com.avaje.ebeaninternal.server.deploy.ChainedBeanQueryAdapter; -import com.avaje.ebeaninternal.server.deploy.CompoundUniqueConstraint; +import com.avaje.ebeaninternal.server.deploy.IndexDefinition; import com.avaje.ebeaninternal.server.deploy.InheritInfo; import com.avaje.ebeaninternal.server.deploy.parse.DeployBeanInfo; import com.avaje.ebeaninternal.server.idgen.UuidIdGenerator; @@ -108,7 +108,7 @@ public class DeployBeanDescriptor { private boolean updateChangesOnly; - private List compoundUniqueConstraints; + private List indexDefinitions; /** * The base database table. @@ -440,21 +440,21 @@ public class DeployBeanDescriptor { /** * Add a compound unique constraint. */ - public void addCompoundUniqueConstraint(CompoundUniqueConstraint c) { - if (compoundUniqueConstraints == null) { - compoundUniqueConstraints = new ArrayList(); + public void addIndex(IndexDefinition c) { + if (indexDefinitions == null) { + indexDefinitions = new ArrayList(); } - compoundUniqueConstraints.add(c); + indexDefinitions.add(c); } /** * Return the compound unique constraints (can be null). */ - public CompoundUniqueConstraint[] getCompoundUniqueConstraints() { - if (compoundUniqueConstraints == null) { + public IndexDefinition[] getIndexDefinitions() { + if (indexDefinitions == null) { return null; } else { - return compoundUniqueConstraints.toArray(new CompoundUniqueConstraint[compoundUniqueConstraints.size()]); + return indexDefinitions.toArray(new IndexDefinition[indexDefinitions.size()]); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanProperty.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanProperty.java index a896806c6..fb5191afa 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanProperty.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanProperty.java @@ -22,7 +22,6 @@ import com.avaje.ebeaninternal.server.el.ElPropertyValue; import com.avaje.ebeaninternal.server.properties.BeanPropertyGetter; import com.avaje.ebeaninternal.server.properties.BeanPropertySetter; import com.avaje.ebeaninternal.server.type.ScalarType; -import com.avaje.ebeaninternal.server.type.ScalarTypeEnum; import com.avaje.ebeaninternal.server.type.ScalarTypeWrapper; import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyOptions; @@ -192,9 +191,6 @@ public class DeployBeanProperty { private int sortOrder; - private boolean indexed; - private String indexName; - private boolean excludedFromHistory; private boolean draft; @@ -849,22 +845,6 @@ public class DeployBeanProperty { return desc.getFullName() + "." + name; } - public boolean isIndexed() { - return indexed; - } - - public void setIndexed() { - this.indexed = true; - } - - public String getIndexName() { - return indexName; - } - - public void setIndexName(String indexName) { - this.indexName = indexName; - } - public boolean isExcludedFromHistory() { return excludedFromHistory; } diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationClass.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationClass.java index ffe34179e..33358f9e1 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationClass.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationClass.java @@ -12,7 +12,7 @@ import com.avaje.ebean.annotation.UpdateMode; import com.avaje.ebean.annotation.View; import com.avaje.ebean.config.TableName; import com.avaje.ebeaninternal.server.deploy.BeanDescriptor.EntityType; -import com.avaje.ebeaninternal.server.deploy.CompoundUniqueConstraint; +import com.avaje.ebeaninternal.server.deploy.IndexDefinition; import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -120,12 +120,12 @@ public class AnnotationClass extends AnnotationParser { Index index = cls.getAnnotation(Index.class); if (index != null) { - descriptor.addCompoundUniqueConstraint(new CompoundUniqueConstraint(index.columnNames(), index.name(), index.unique())); + descriptor.addIndex(new IndexDefinition(index.columnNames(), index.name(), index.unique())); } UniqueConstraint uc = cls.getAnnotation(UniqueConstraint.class); if (uc != null) { - descriptor.addCompoundUniqueConstraint(new CompoundUniqueConstraint(uc.columnNames())); + descriptor.addIndex(new IndexDefinition(uc.columnNames())); } View view = cls.getAnnotation(View.class); @@ -136,7 +136,7 @@ public class AnnotationClass extends AnnotationParser { if (table != null) { UniqueConstraint[] uniqueConstraints = table.uniqueConstraints(); for (UniqueConstraint c : uniqueConstraints) { - descriptor.addCompoundUniqueConstraint(new CompoundUniqueConstraint(c.columnNames())); + descriptor.addIndex(new IndexDefinition(c.columnNames())); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java index 4325da31a..e1510dc78 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java @@ -7,6 +7,7 @@ import com.avaje.ebean.config.dbplatform.DbEncrypt; import com.avaje.ebean.config.dbplatform.DbEncryptFunction; import com.avaje.ebean.config.dbplatform.IdType; import com.avaje.ebean.config.dbplatform.PlatformIdGenerator; +import com.avaje.ebeaninternal.server.deploy.IndexDefinition; import com.avaje.ebeaninternal.server.deploy.generatedproperty.GeneratedPropertyFactory; import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty; import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssoc; @@ -314,8 +315,7 @@ public class AnnotationFields extends AnnotationParser { if (hasRelationshipItem(prop)) { throw new RuntimeException("Can't use Index on foreign key relationships."); } - prop.setIndexed(); - prop.setIndexName(index.name()); + descriptor.addIndex(new IndexDefinition(prop.getDbColumn(), index.name(), index.unique())); } }