Skip to content

Commit

Permalink
Refactor IsTableManaged
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml authored and nPraml committed Oct 18, 2024
1 parent 588d663 commit 2104d34
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1899,13 +1899,6 @@ public <U> BeanDescriptor<U> descriptor(Class<U> otherType) {
return owner.descriptor(otherType);
}

/**
* Returns true, if the table is managed (i.e. an existing m2m relation).
*/
public boolean isTableManaged(String tableName) {
return owner.isTableManaged(tableName);
}

/**
* Return the order column property.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public final class BeanDescriptorManager implements BeanDescriptorMap, SpiBeanTy
private final BootupClasses bootupClasses;
private final String serverName;
private final List<BeanDescriptor<?>> elementDescriptors = new ArrayList<>();
private final Set<String> managedTables = new HashSet<>();
private final Map<Class<?>, BeanTable> beanTableMap = new HashMap<>();
private final Map<String, BeanDescriptor<?>> descMap = new HashMap<>();
private final Map<String, BeanDescriptor<?>> descQueueMap = new HashMap<>();
Expand Down Expand Up @@ -428,8 +429,7 @@ public List<? extends BeanType<?>> beanTypes(String tableName) {

@Override
public boolean isTableManaged(String tableName) {
return tableToDescMap.get(tableName.toLowerCase()) != null
|| tableToViewDescMap.get(tableName.toLowerCase()) != null;
return managedTables.contains(tableName);
}

/**
Expand Down Expand Up @@ -699,6 +699,9 @@ private void readEntityBeanTable() {
for (DeployBeanInfo<?> info : deployInfoMap.values()) {
BeanTable beanTable = createBeanTable(info);
beanTableMap.put(beanTable.getBeanType(), beanTable);
if (beanTable.getBaseTable() != null) {
managedTables.add(beanTable.getBaseTable());
}
}
// register non-id embedded beans (after bean tables are created)
for (DeployBeanInfo<?> info : embeddedBeans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> implements ST
* Join for manyToMany intersection table.
*/
private final TableJoin intersectionJoin;
private final boolean tableManaged;
private final String intersectionPublishTable;
private final String intersectionDraftTable;
private final boolean orphanRemoval;
Expand Down Expand Up @@ -100,9 +101,11 @@ public BeanPropertyAssocMany(BeanDescriptor<?> descriptor, DeployBeanPropertyAss
this.fetchOrderBy = deploy.getFetchOrderBy();
this.intersectionJoin = deploy.createIntersectionTableJoin();
if (intersectionJoin != null) {
this.tableManaged = deploy.isTableManaged();
this.intersectionPublishTable = intersectionJoin.getTable();
this.intersectionDraftTable = deploy.getIntersectionDraftTable();
} else {
this.tableManaged = false;
this.intersectionPublishTable = null;
this.intersectionDraftTable = null;
}
Expand Down Expand Up @@ -986,11 +989,11 @@ public boolean isIncludeCascadeSave() {
// Note ManyToMany always included as we always 'save'
// the relationship via insert/delete of intersection table
// REMOVALS means including PrivateOwned relationships
return cascadeInfo.isSave() || hasJoinTable() || ModifyListenMode.REMOVALS == modifyListenMode;
return cascadeInfo.isSave() || (hasJoinTable() && !tableManaged) || ModifyListenMode.REMOVALS == modifyListenMode;
}

public boolean isIncludeCascadeDelete() {
return cascadeInfo.isDelete() || hasJoinTable() || ModifyListenMode.REMOVALS == modifyListenMode;
return cascadeInfo.isDelete() || (hasJoinTable() && !tableManaged) || ModifyListenMode.REMOVALS == modifyListenMode;
}

boolean isCascadeDeleteEscalate() {
Expand Down Expand Up @@ -1118,13 +1121,20 @@ public void bindElementValue(SqlUpdate insert, Object value) {
targetDescriptor.bindElementValue(insert, value);
}

/**
* Returns true, if this M2M beanproperty has a jointable, where the jointable is managed by an other entity.
*/
public boolean isTableManaged() {
return tableManaged;
}

/**
* Returns true, if we must create a m2m join table.
*/
public boolean createJoinTable() {
if (hasJoinTable() && mappedBy() == null) {
// only create on other 'owning' side
return !descriptor.isTableManaged(intersectionJoin.getTable());
return !tableManaged;
} else {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public class DeployBeanDescriptor<T> implements DeployBeanDescriptorMeta {

private static final Map<String, SpiRawSql> EMPTY_RAW_MAP = new HashMap<>();

/**
* Returns true, if the table is managed (i.e. an existing m2m relation).
*/
public boolean isTableManaged(String tableName) {
return manager.isTableManaged(tableName);
}

private static class PropOrder implements Comparator<DeployBeanProperty> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public TableJoin createIntersectionTableJoin() {
}
}

public boolean isTableManaged() {
return intersectionJoin != null && desc.isTableManaged(intersectionJoin.getTable());
}

/**
* Create the immutable version of the inverse join.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
import io.ebeaninternal.server.deploy.*;

import jakarta.persistence.PersistenceException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -94,6 +98,22 @@ private boolean isSaveIntersection() {
// OneToMany JoinTable
return true;
}
if (many.isTableManaged()) {
List<String> tables = new ArrayList<>(3);
tables.add(many.descriptor().baseTable());
tables.add(many.targetDescriptor().baseTable());
tables.add(many.intersectionTableJoin().getTable());
// put all tables in a deterministic order
tables.sort(Comparator.naturalOrder());

if (transaction.isSaveAssocManyIntersection(String.join("-", tables), many.descriptor().rootName())) {
// notify others, that we do save this transaction
transaction.isSaveAssocManyIntersection(many.intersectionTableJoin().getTable(), many.descriptor().rootName());
return true;
} else {
return false;
}
}
return transaction.isSaveAssocManyIntersection(many.intersectionTableJoin().getTable(), many.descriptor().rootName());
}

Expand Down

0 comments on commit 2104d34

Please sign in to comment.