mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#368 - ENH: Enhance @Index to support multiple columns, unique flag and hence also be able to put on the class (annotation target TYPE)
This commit is contained in:
1 parent
641ada001e
commit
ccba5d08ef
14 files changed
+152
-53
No files matched your search
@@ -133,7 +133,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
|
||||
*/
|
||||
private final ConcurrencyMode concurrencyMode;
|
||||
|
||||
private final CompoundUniqueContraint[] compoundUniqueConstraints;
|
||||
private final CompoundUniqueConstraint[] compoundUniqueConstraints;
|
||||
|
||||
/**
|
||||
* The base database table.
|
||||
@@ -1352,7 +1352,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
|
||||
|
||||
@Override
|
||||
public Object getBeanId(T bean) {
|
||||
return getId((EntityBean)bean);
|
||||
return getId((EntityBean) bean);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1620,7 +1620,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
|
||||
/**
|
||||
* Return the compound unique constraints.
|
||||
*/
|
||||
public CompoundUniqueContraint[] getCompoundUniqueConstraints() {
|
||||
public CompoundUniqueConstraint[] getCompoundUniqueConstraints() {
|
||||
return compoundUniqueConstraints;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
/**
|
||||
* Holds multiple column unique constraints defined for an entity.
|
||||
*/
|
||||
public class CompoundUniqueConstraint {
|
||||
|
||||
private final String[] columns;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final boolean unique;
|
||||
|
||||
public CompoundUniqueConstraint(String[] columns, String name, boolean unique) {
|
||||
this.columns = columns;
|
||||
this.unique = unique;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a unique constraint given the column names.
|
||||
*/
|
||||
public CompoundUniqueConstraint(String[] columns) {
|
||||
this.columns = columns;
|
||||
this.unique = true;
|
||||
this.name = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this is a unique constraint.
|
||||
*/
|
||||
public boolean isUnique() {
|
||||
return unique;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the index name (can be null).
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the columns that make up this unique constraint.
|
||||
*/
|
||||
public String[] getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
/**
|
||||
* Holds multiple column unique constraints defined for an entity.
|
||||
*/
|
||||
public class CompoundUniqueContraint {
|
||||
|
||||
private final String[] columns;
|
||||
|
||||
public CompoundUniqueContraint(String[] columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the columns that make up this unique constraint.
|
||||
*/
|
||||
public String[] getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,7 +16,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.CompoundUniqueContraint;
|
||||
import com.avaje.ebeaninternal.server.deploy.CompoundUniqueConstraint;
|
||||
import com.avaje.ebeaninternal.server.deploy.DRawSqlMeta;
|
||||
import com.avaje.ebeaninternal.server.deploy.DeployNamedQuery;
|
||||
import com.avaje.ebeaninternal.server.deploy.DeployNamedUpdate;
|
||||
@@ -107,7 +107,7 @@ public class DeployBeanDescriptor<T> {
|
||||
|
||||
private boolean updateChangesOnly;
|
||||
|
||||
private List<CompoundUniqueContraint> compoundUniqueConstraints;
|
||||
private List<CompoundUniqueConstraint> compoundUniqueConstraints;
|
||||
|
||||
/**
|
||||
* The base database table.
|
||||
@@ -371,9 +371,9 @@ public class DeployBeanDescriptor<T> {
|
||||
/**
|
||||
* Add a compound unique constraint.
|
||||
*/
|
||||
public void addCompoundUniqueConstraint(CompoundUniqueContraint c) {
|
||||
public void addCompoundUniqueConstraint(CompoundUniqueConstraint c) {
|
||||
if (compoundUniqueConstraints == null) {
|
||||
compoundUniqueConstraints = new ArrayList<CompoundUniqueContraint>();
|
||||
compoundUniqueConstraints = new ArrayList<CompoundUniqueConstraint>();
|
||||
}
|
||||
compoundUniqueConstraints.add(c);
|
||||
}
|
||||
@@ -381,11 +381,11 @@ public class DeployBeanDescriptor<T> {
|
||||
/**
|
||||
* Return the compound unique constraints (can be null).
|
||||
*/
|
||||
public CompoundUniqueContraint[] getCompoundUniqueConstraints() {
|
||||
public CompoundUniqueConstraint[] getCompoundUniqueConstraints() {
|
||||
if (compoundUniqueConstraints == null) {
|
||||
return null;
|
||||
} else {
|
||||
return compoundUniqueConstraints.toArray(new CompoundUniqueContraint[compoundUniqueConstraints.size()]);
|
||||
return compoundUniqueConstraints.toArray(new CompoundUniqueConstraint[compoundUniqueConstraints.size()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,13 +11,14 @@ import com.avaje.ebean.annotation.CacheStrategy;
|
||||
import com.avaje.ebean.annotation.CacheTuning;
|
||||
import com.avaje.ebean.annotation.EntityConcurrencyMode;
|
||||
import com.avaje.ebean.annotation.History;
|
||||
import com.avaje.ebean.annotation.Index;
|
||||
import com.avaje.ebean.annotation.NamedUpdate;
|
||||
import com.avaje.ebean.annotation.NamedUpdates;
|
||||
import com.avaje.ebean.annotation.UpdateMode;
|
||||
import com.avaje.ebean.config.TableName;
|
||||
import com.avaje.ebeaninternal.server.core.CacheOptions;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor.EntityType;
|
||||
import com.avaje.ebeaninternal.server.deploy.CompoundUniqueContraint;
|
||||
import com.avaje.ebeaninternal.server.deploy.CompoundUniqueConstraint;
|
||||
import com.avaje.ebeaninternal.server.deploy.DeployNamedQuery;
|
||||
import com.avaje.ebeaninternal.server.deploy.DeployNamedUpdate;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
@@ -76,9 +77,14 @@ public class AnnotationClass extends AnnotationParser {
|
||||
descriptor.setName("Embeddable:" + cls.getSimpleName());
|
||||
}
|
||||
|
||||
Index index = cls.getAnnotation(Index.class);
|
||||
if (index != null) {
|
||||
descriptor.addCompoundUniqueConstraint(new CompoundUniqueConstraint(index.columnNames(), index.name(), index.unique()));
|
||||
}
|
||||
|
||||
UniqueConstraint uc = cls.getAnnotation(UniqueConstraint.class);
|
||||
if (uc != null) {
|
||||
descriptor.addCompoundUniqueConstraint(new CompoundUniqueContraint(uc.columnNames()));
|
||||
descriptor.addCompoundUniqueConstraint(new CompoundUniqueConstraint(uc.columnNames()));
|
||||
}
|
||||
|
||||
Table table = cls.getAnnotation(Table.class);
|
||||
@@ -86,7 +92,7 @@ public class AnnotationClass extends AnnotationParser {
|
||||
UniqueConstraint[] uniqueConstraints = table.uniqueConstraints();
|
||||
if (uniqueConstraints != null) {
|
||||
for (UniqueConstraint c : uniqueConstraints) {
|
||||
descriptor.addCompoundUniqueConstraint(new CompoundUniqueContraint(c.columnNames()));
|
||||
descriptor.addCompoundUniqueConstraint(new CompoundUniqueConstraint(c.columnNames()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,7 +287,7 @@ public class AnnotationFields extends AnnotationParser {
|
||||
throw new RuntimeException("Can't use Index on foreign key relationships.");
|
||||
}
|
||||
prop.setIndexed();
|
||||
prop.setIndexName(index.value());
|
||||
prop.setIndexName(index.name());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user