mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-17 22:56:28 +00:00
#1142 - Mapping - Add support for @OrderColumn (Ebean automatically maintains an order column)
This commit is contained in:
1 parent
31cac60393
commit
628af31ddd
23 files changed
+746
-209
No files matched your search
@@ -8,7 +8,6 @@ import io.ebean.Update;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.bean.BeanCollection.ModifyListenMode;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import io.ebean.bean.PersistenceContext;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
@@ -21,7 +20,6 @@ import io.ebeaninternal.server.core.PersistRequestCallableSql;
|
||||
import io.ebeaninternal.server.core.PersistRequestOrmUpdate;
|
||||
import io.ebeaninternal.server.core.PersistRequestUpdateSql;
|
||||
import io.ebeaninternal.server.core.Persister;
|
||||
import io.ebeaninternal.server.deploy.BeanCollectionUtil;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptorManager;
|
||||
import io.ebeaninternal.server.deploy.BeanManager;
|
||||
@@ -29,7 +27,6 @@ import io.ebeaninternal.server.deploy.BeanProperty;
|
||||
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
|
||||
import io.ebeaninternal.server.deploy.IntersectionRow;
|
||||
import io.ebeaninternal.server.deploy.ManyType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -413,7 +410,7 @@ public final class DefaultPersister implements Persister {
|
||||
}
|
||||
}
|
||||
|
||||
private void saveRecurse(EntityBean bean, Transaction t, Object parentBean, boolean insertMode, boolean publish) {
|
||||
void saveRecurse(EntityBean bean, Transaction t, Object parentBean, boolean insertMode, boolean publish) {
|
||||
|
||||
// determine insert or update taking into account stateless updates
|
||||
PersistRequestBean<?> request = createRequestRecurse(bean, t, parentBean, insertMode, publish);
|
||||
@@ -826,86 +823,6 @@ public final class DefaultPersister implements Persister {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to wrap the details when saving a OneToMany or ManyToMany
|
||||
* relationship.
|
||||
*/
|
||||
private static class SaveManyPropRequest {
|
||||
private final boolean insertedParent;
|
||||
private final BeanPropertyAssocMany<?> many;
|
||||
private final EntityBean parentBean;
|
||||
private final SpiTransaction transaction;
|
||||
private final boolean cascade;
|
||||
private final boolean deleteMissingChildren;
|
||||
private final boolean publish;
|
||||
|
||||
private SaveManyPropRequest(boolean insertedParent, BeanPropertyAssocMany<?> many, EntityBean parentBean, PersistRequestBean<?> request) {
|
||||
this.insertedParent = insertedParent;
|
||||
this.many = many;
|
||||
this.cascade = many.getCascadeInfo().isSave();
|
||||
this.parentBean = parentBean;
|
||||
this.transaction = request.getTransaction();
|
||||
this.deleteMissingChildren = request.isDeleteMissingChildren();
|
||||
this.publish = request.isPublish();
|
||||
}
|
||||
|
||||
public boolean isSaveIntersection() {
|
||||
return transaction.isSaveAssocManyIntersection(many.getIntersectionTableJoin().getTable(), many.getBeanDescriptor().getName());
|
||||
}
|
||||
|
||||
private Object getValue() {
|
||||
return many.getValue(parentBean);
|
||||
}
|
||||
|
||||
private boolean isModifyListenMode() {
|
||||
return ModifyListenMode.REMOVALS == many.getModifyListenMode();
|
||||
}
|
||||
|
||||
private boolean isDeleteMissingChildren() {
|
||||
return deleteMissingChildren;
|
||||
}
|
||||
|
||||
private boolean isInsertedParent() {
|
||||
return insertedParent;
|
||||
}
|
||||
|
||||
private BeanPropertyAssocMany<?> getMany() {
|
||||
return many;
|
||||
}
|
||||
|
||||
private EntityBean getParentBean() {
|
||||
return parentBean;
|
||||
}
|
||||
|
||||
private SpiTransaction getTransaction() {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
private boolean isCascade() {
|
||||
return cascade;
|
||||
}
|
||||
|
||||
public void modifyListenReset(BeanCollection<?> c) {
|
||||
if (insertedParent) {
|
||||
// after insert set the modify listening mode
|
||||
// for private owned etc
|
||||
c.setModifyListening(many.getModifyListenMode());
|
||||
}
|
||||
c.modifyReset();
|
||||
}
|
||||
|
||||
public boolean isPublish() {
|
||||
return publish;
|
||||
}
|
||||
|
||||
private void resetModifyState() {
|
||||
Object details = getValue();
|
||||
if (details instanceof BeanCollection<?>) {
|
||||
modifyListenReset((BeanCollection<?>) details);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void saveMany(SaveManyPropRequest saveMany, boolean insertMode) {
|
||||
|
||||
if (saveMany.getMany().isManyToMany()) {
|
||||
@@ -967,107 +884,7 @@ public final class DefaultPersister implements Persister {
|
||||
*/
|
||||
private void saveAssocManyDetails(SaveManyPropRequest saveMany, boolean deleteMissingChildren, boolean insertMode) {
|
||||
|
||||
BeanPropertyAssocMany<?> prop = saveMany.getMany();
|
||||
|
||||
Object details = saveMany.getValue();
|
||||
|
||||
// check that the list is not null and if it is a BeanCollection
|
||||
// check that is has been populated (don't trigger lazy loading)
|
||||
// For a Map this is a collection of Map.Entry objects and not beans
|
||||
Collection<?> collection = BeanCollectionUtil.getActualEntries(details);
|
||||
|
||||
if (collection == null) {
|
||||
// nothing to do here
|
||||
return;
|
||||
}
|
||||
|
||||
BeanDescriptor<?> targetDescriptor = prop.getTargetDescriptor();
|
||||
if (saveMany.isInsertedParent()) {
|
||||
// performance optimisation for large collections
|
||||
targetDescriptor.preAllocateIds(collection.size());
|
||||
}
|
||||
|
||||
SpiTransaction t = saveMany.getTransaction();
|
||||
boolean isMap = ManyType.MAP == prop.getManyType();
|
||||
EntityBean parentBean = saveMany.getParentBean();
|
||||
|
||||
if (deleteMissingChildren) {
|
||||
// collect the Id's (to exclude from deleteManyDetails)
|
||||
List<Object> detailIds = collectIds(collection, targetDescriptor, isMap);
|
||||
// deleting missing children - children not in our collected detailIds
|
||||
deleteManyDetails(t, prop.getBeanDescriptor(), parentBean, prop, detailIds, false);
|
||||
}
|
||||
|
||||
// increase depth for batching order
|
||||
t.depth(+1);
|
||||
|
||||
// if a map, then we get the key value and
|
||||
// set it to the appropriate property on the
|
||||
// detail bean before we save it
|
||||
Object mapKeyValue = null;
|
||||
|
||||
boolean saveSkippable = prop.isSaveRecurseSkippable();
|
||||
boolean skipSavingThisBean;
|
||||
|
||||
for (Object detailBean : collection) {
|
||||
if (isMap) {
|
||||
// its a map so need the key and value
|
||||
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) detailBean;
|
||||
mapKeyValue = entry.getKey();
|
||||
detailBean = entry.getValue();
|
||||
}
|
||||
|
||||
if (detailBean instanceof EntityBean) {
|
||||
EntityBean detail = (EntityBean) detailBean;
|
||||
EntityBeanIntercept ebi = detail._ebean_getIntercept();
|
||||
if (prop.isManyToMany()) {
|
||||
skipSavingThisBean = targetDescriptor.isReference(ebi);
|
||||
} else {
|
||||
if (targetDescriptor.isReference(ebi)) {
|
||||
// we can skip this one
|
||||
skipSavingThisBean = true;
|
||||
|
||||
} else if (ebi.isNewOrDirty()) {
|
||||
skipSavingThisBean = false;
|
||||
// set the parent bean to detailBean
|
||||
prop.setJoinValuesToChild(parentBean, detail, mapKeyValue);
|
||||
|
||||
} else {
|
||||
// unmodified so skip depending on prop.isSaveRecurseSkippable();
|
||||
skipSavingThisBean = saveSkippable;
|
||||
}
|
||||
}
|
||||
|
||||
if (!skipSavingThisBean) {
|
||||
saveRecurse(detail, t, parentBean, insertMode, saveMany.isPublish());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.depth(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect the Id values of the details to remove 'missing children' for stateless updates.
|
||||
*/
|
||||
private List<Object> collectIds(Collection<?> collection, BeanDescriptor<?> targetDescriptor, boolean isMap) {
|
||||
|
||||
List<Object> detailIds = new ArrayList<>();
|
||||
// stateless update with deleteMissingChildren so first
|
||||
// collect the Id values to remove the 'missing children'
|
||||
for (Object detailBean : collection) {
|
||||
if (isMap) {
|
||||
detailBean = ((Map.Entry<?, ?>) detailBean).getValue();
|
||||
}
|
||||
if (detailBean instanceof EntityBean) {
|
||||
Object id = targetDescriptor.getId((EntityBean) detailBean);
|
||||
if (!DmlUtil.isNullOrZero(id)) {
|
||||
// remember the Id (other details not in the collection) will be removed
|
||||
detailIds.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
return detailIds;
|
||||
saveMany.saveDetails(this, deleteMissingChildren, insertMode);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1266,7 +1083,7 @@ public final class DefaultPersister implements Persister {
|
||||
* collection (and should not be deleted).
|
||||
* </p>
|
||||
*/
|
||||
private void deleteManyDetails(SpiTransaction t, BeanDescriptor<?> desc, EntityBean parentBean,
|
||||
void deleteManyDetails(SpiTransaction t, BeanDescriptor<?> desc, EntityBean parentBean,
|
||||
BeanPropertyAssocMany<?> many, List<Object> excludeDetailIds, boolean softDelete) {
|
||||
|
||||
if (many.getCascadeInfo().isDelete()) {
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
package io.ebeaninternal.server.persist;
|
||||
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import io.ebeaninternal.server.core.PersistRequestBean;
|
||||
import io.ebeaninternal.server.deploy.BeanCollectionUtil;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.BeanProperty;
|
||||
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
import io.ebeaninternal.server.deploy.ManyType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Helper to wrap the details when saving a OneToMany or ManyToMany relationship.
|
||||
*/
|
||||
class SaveManyPropRequest {
|
||||
|
||||
private final boolean insertedParent;
|
||||
private final BeanPropertyAssocMany<?> many;
|
||||
private final EntityBean parentBean;
|
||||
private final SpiTransaction transaction;
|
||||
private final boolean cascade;
|
||||
private final boolean deleteMissingChildren;
|
||||
private final boolean publish;
|
||||
|
||||
private final Object value;
|
||||
private final BeanDescriptor<?> targetDescriptor;
|
||||
private final boolean isMap;
|
||||
private final boolean saveRecurseSkippable;
|
||||
|
||||
private Collection<?> collection;
|
||||
private DefaultPersister persister;
|
||||
private boolean deleteMissing;
|
||||
private boolean insertMode;
|
||||
private int sortOrder;
|
||||
|
||||
SaveManyPropRequest(boolean insertedParent, BeanPropertyAssocMany<?> many, EntityBean parentBean, PersistRequestBean<?> request) {
|
||||
this.insertedParent = insertedParent;
|
||||
this.many = many;
|
||||
this.cascade = many.getCascadeInfo().isSave();
|
||||
this.parentBean = parentBean;
|
||||
this.transaction = request.getTransaction();
|
||||
this.deleteMissingChildren = request.isDeleteMissingChildren();
|
||||
this.publish = request.isPublish();
|
||||
this.value = many.getValue(parentBean);
|
||||
this.targetDescriptor = many.getTargetDescriptor();
|
||||
this.isMap = ManyType.MAP == many.getManyType();
|
||||
this.saveRecurseSkippable = many.isSaveRecurseSkippable();
|
||||
}
|
||||
|
||||
public boolean isSaveIntersection() {
|
||||
return transaction.isSaveAssocManyIntersection(many.getIntersectionTableJoin().getTable(), many.getBeanDescriptor().getName());
|
||||
}
|
||||
|
||||
Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
boolean isModifyListenMode() {
|
||||
return BeanCollection.ModifyListenMode.REMOVALS == many.getModifyListenMode();
|
||||
}
|
||||
|
||||
boolean isDeleteMissingChildren() {
|
||||
return deleteMissingChildren;
|
||||
}
|
||||
|
||||
boolean isInsertedParent() {
|
||||
return insertedParent;
|
||||
}
|
||||
|
||||
BeanPropertyAssocMany<?> getMany() {
|
||||
return many;
|
||||
}
|
||||
|
||||
EntityBean getParentBean() {
|
||||
return parentBean;
|
||||
}
|
||||
|
||||
SpiTransaction getTransaction() {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
boolean isCascade() {
|
||||
return cascade;
|
||||
}
|
||||
|
||||
boolean isPublish() {
|
||||
return publish;
|
||||
}
|
||||
|
||||
void modifyListenReset(BeanCollection<?> c) {
|
||||
if (insertedParent) {
|
||||
// after insert set the modify listening mode for private owned etc
|
||||
c.setModifyListening(many.getModifyListenMode());
|
||||
}
|
||||
c.modifyReset();
|
||||
}
|
||||
|
||||
void resetModifyState() {
|
||||
if (value instanceof BeanCollection<?>) {
|
||||
modifyListenReset((BeanCollection<?>) value);
|
||||
}
|
||||
}
|
||||
|
||||
void saveDetails(DefaultPersister persister, boolean deleteMissing, boolean insertMode) {
|
||||
|
||||
this.persister = persister;
|
||||
this.deleteMissing = deleteMissing;
|
||||
this.insertMode = insertMode;
|
||||
|
||||
// check that the list is not null and if it is a BeanCollection
|
||||
// check that is has been populated (don't trigger lazy loading)
|
||||
// For a Map this is a collection of Map.Entry objects and not beans
|
||||
collection = BeanCollectionUtil.getActualEntries(value);
|
||||
if (collection != null) {
|
||||
processDetails();
|
||||
}
|
||||
}
|
||||
|
||||
private void processDetails() {
|
||||
|
||||
BeanProperty orderColumn = null;
|
||||
boolean hasOrderColumn = many.hasOrderColumn();
|
||||
if (hasOrderColumn) {
|
||||
if (!insertedParent && canSkipForOrderColumn()) {
|
||||
return;
|
||||
}
|
||||
orderColumn = targetDescriptor.getOrderColumn();
|
||||
}
|
||||
|
||||
if (insertedParent) {
|
||||
// performance optimisation for large collections
|
||||
targetDescriptor.preAllocateIds(collection.size());
|
||||
}
|
||||
|
||||
if (deleteMissing) {
|
||||
// collect the Id's (to exclude from deleteManyDetails)
|
||||
List<Object> detailIds = collectIds(collection, targetDescriptor, isMap);
|
||||
// deleting missing children - children not in our collected detailIds
|
||||
persister.deleteManyDetails(transaction, many.getBeanDescriptor(), parentBean, many, detailIds, false);
|
||||
}
|
||||
|
||||
transaction.depth(+1);
|
||||
saveAllBeans(orderColumn);
|
||||
if (hasOrderColumn) {
|
||||
resetModifyState();
|
||||
}
|
||||
transaction.depth(-1);
|
||||
}
|
||||
|
||||
|
||||
private void saveAllBeans(BeanProperty orderColumn) {
|
||||
|
||||
// if a map, then we get the key value and
|
||||
// set it to the appropriate property on the
|
||||
// detail bean before we save it
|
||||
Object mapKeyValue = null;
|
||||
boolean skipSavingThisBean;
|
||||
|
||||
for (Object detailBean : collection) {
|
||||
sortOrder++;
|
||||
if (isMap) {
|
||||
// its a map so need the key and value
|
||||
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) detailBean;
|
||||
mapKeyValue = entry.getKey();
|
||||
detailBean = entry.getValue();
|
||||
}
|
||||
|
||||
if (detailBean instanceof EntityBean) {
|
||||
EntityBean detail = (EntityBean) detailBean;
|
||||
EntityBeanIntercept ebi = detail._ebean_getIntercept();
|
||||
if (many.isManyToMany()) {
|
||||
skipSavingThisBean = targetDescriptor.isReference(ebi);
|
||||
} else {
|
||||
if (orderColumn != null) {
|
||||
orderColumn.setValue(detail, sortOrder);
|
||||
ebi.setDirty(true);
|
||||
}
|
||||
if (targetDescriptor.isReference(ebi)) {
|
||||
// we can skip this one
|
||||
skipSavingThisBean = true;
|
||||
|
||||
} else if (ebi.isNewOrDirty()) {
|
||||
skipSavingThisBean = false;
|
||||
// set the parent bean to detailBean
|
||||
many.setJoinValuesToChild(parentBean, detail, mapKeyValue);
|
||||
|
||||
} else {
|
||||
// unmodified so skip depending on prop.isSaveRecurseSkippable();
|
||||
skipSavingThisBean = saveRecurseSkippable;
|
||||
}
|
||||
}
|
||||
|
||||
if (!skipSavingThisBean) {
|
||||
persister.saveRecurse(detail, transaction, parentBean, insertMode, publish);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if we can skip based on .. no modifications to the collection and no beans are dirty.
|
||||
*/
|
||||
private boolean canSkipForOrderColumn() {
|
||||
return value instanceof BeanCollection
|
||||
&& !((BeanCollection<?>) value).wasTouched()
|
||||
&& noDirtyBeans();
|
||||
}
|
||||
|
||||
private boolean noDirtyBeans() {
|
||||
for (Object bean : collection) {
|
||||
if (bean instanceof EntityBean && ((EntityBean) bean)._ebean_getIntercept().isDirty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect the Id values of the details to remove 'missing children' for stateless updates.
|
||||
*/
|
||||
private List<Object> collectIds(Collection<?> collection, BeanDescriptor<?> targetDescriptor, boolean isMap) {
|
||||
|
||||
List<Object> detailIds = new ArrayList<>();
|
||||
// stateless update with deleteMissingChildren so first
|
||||
// collect the Id values to remove the 'missing children'
|
||||
for (Object detailBean : collection) {
|
||||
if (isMap) {
|
||||
detailBean = ((Map.Entry<?, ?>) detailBean).getValue();
|
||||
}
|
||||
if (detailBean instanceof EntityBean) {
|
||||
Object id = targetDescriptor.getId((EntityBean) detailBean);
|
||||
if (!DmlUtil.isNullOrZero(id)) {
|
||||
// remember the Id (other details not in the collection) will be removed
|
||||
detailIds.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
return detailIds;
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,12 @@ package io.ebeaninternal.server.persist.dml;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.config.dbplatform.DbEncrypt;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.BeanProperty;
|
||||
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
|
||||
import io.ebeaninternal.server.persist.dmlbind.Bindable;
|
||||
import io.ebeaninternal.server.persist.dmlbind.BindableId;
|
||||
import io.ebeaninternal.server.persist.dmlbind.BindableList;
|
||||
import io.ebeaninternal.server.persist.dmlbind.BindableOrderColumn;
|
||||
import io.ebeaninternal.server.persist.dmlbind.BindableUnidirectional;
|
||||
import io.ebeaninternal.server.persist.dmlbind.FactoryAssocOnes;
|
||||
import io.ebeaninternal.server.persist.dmlbind.FactoryBaseProperties;
|
||||
@@ -62,6 +64,11 @@ public class MetaFactory {
|
||||
embeddedFact.create(setList, desc, DmlMode.UPDATE, includeLobs);
|
||||
assocOneFact.create(setList, desc, DmlMode.UPDATE);
|
||||
|
||||
BeanProperty orderColumn = desc.getOrderColumn();
|
||||
if (orderColumn != null) {
|
||||
setList.add(new BindableOrderColumn(orderColumn));
|
||||
}
|
||||
|
||||
BindableId id = idFact.createId(desc);
|
||||
Bindable version = versionFact.create(desc);
|
||||
Bindable tenantId = versionFact.createTenantId(desc);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package io.ebeaninternal.server.persist.dmlbind;
|
||||
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.server.core.PersistRequestBean;
|
||||
import io.ebeaninternal.server.deploy.BeanProperty;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Bindable for the synthetic order column.
|
||||
*/
|
||||
public class BindableOrderColumn extends BindableProperty {
|
||||
|
||||
public BindableOrderColumn(BeanProperty prop) {
|
||||
super(prop);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
|
||||
int sortOrder = request.getEntityBeanIntercept().getSortOrder();
|
||||
if (sortOrder > 0) {
|
||||
list.add(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normal binding of a property value from the bean.
|
||||
*/
|
||||
@Override
|
||||
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
|
||||
|
||||
int sortOrder = bean._ebean_getIntercept().getSortOrder();
|
||||
request.bind(sortOrder, prop);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user