NEW: Ebean.checkUniqueness - you can check a bean, if save would work (#1303)

This commit is contained in:
Roland Praml
2018-03-02 11:57:44 +13:00
committed by Rob Bygrave
parent 0c368ed7b3
commit efe325034b
7 changed files with 256 additions and 1 deletions
@@ -350,6 +350,7 @@ public class BeanDescriptor<T> implements BeanType<T> {
protected final BeanProperty[] propertiesIndex;
private final BeanProperty[] propertiesGenInsert;
private final BeanProperty[] propertiesGenUpdate;
private final List<BeanProperty[]> propertiesUnique = new ArrayList<>();
/**
* The bean class name or the table name for MapBeans.
@@ -769,9 +770,43 @@ public class BeanDescriptor<T> implements BeanType<T> {
* Perform last initialisation for the descriptor.
*/
public void initLast() {
for (BeanProperty prop : propertiesNonTransient) {
if (prop.isUnique()) {
propertiesUnique.add(new BeanProperty[] { prop });
}
}
// convert unique columns to properties
if (indexDefinitions != null) {
for (IndexDefinition indexDef : indexDefinitions) {
if (indexDef.isUnique()) {
addUniqueColumns(indexDef);
}
}
}
docStoreEmbeddedInvalidation = docStoreAdapter.hasEmbeddedInvalidation();
}
private void addUniqueColumns(IndexDefinition indexDef) {
String[] cols = indexDef.getColumns();
BeanProperty[] props = new BeanProperty[cols.length];
for (int i = 0; i < cols.length; i++) {
String propName = findBeanPath("", cols[i]);
if (propName == null) {
return;
}
props[i] = findBeanProperty(propName);
}
if (props.length == 1) {
for (BeanProperty[] inserted : propertiesUnique) {
if (inserted.length == 1 && inserted[0].equals(props[0])) {
return; // do not insert duplicates
}
}
}
propertiesUnique.add(props);
}
/**
* Initialise the document mapping.
*/
@@ -3220,4 +3255,7 @@ public class BeanDescriptor<T> implements BeanType<T> {
return jsonHelp.jsonReadObject(jsonRead, path);
}
public List<BeanProperty[]> getUniqueProps() {
return propertiesUnique;
}
}