Skip to content

Commit

Permalink
[#779] Use inverse flusher for full flushing as well
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed May 28, 2019
1 parent 3df0651 commit 83e1348
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void setOwnedDocuments(Set<Document> ownedDocuments) {

@OneToMany
@JoinColumn(
name = "owner_id",
name = "responsible_person_id",
referencedColumnName = "id"
)
public Set<Document> getOwnedDocuments2() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
Expand Down Expand Up @@ -435,6 +436,7 @@ public void setParent(Document parent) {
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "responsible_person_id")
public Person getResponsiblePerson() {
return responsiblePerson;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,9 @@ public void visit(MapKeyExpression expression) {
if (!(currentPosition.getAttribute() instanceof MapAttribute<?, ?, ?>)) {
invalid(expression, "Does not resolve to java.util.Map!");
} else {
currentPosition.setAttribute(new MapKeyAttribute<>((MapAttribute<?, Object, ?>) currentPosition.getAttribute()));
currentPosition.setCurrentType(((MapAttribute<?, Object, ?>) currentPosition.getAttribute()).getKeyType());
MapKeyAttribute<Object, Object> keyAttribute = new MapKeyAttribute<>((MapAttribute<?, Object, ?>) currentPosition.getAttribute());
currentPosition.setAttribute(keyAttribute);
currentPosition.setCurrentType(keyAttribute.getType());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,38 @@ protected boolean deleteElements(UpdateContext context, Object ownerView, Object
Collection<Object> removedObjects = Collections.emptyList();
if (fusedCollectionActions != null) {
if (fusedCollectionActions.getRemoveCount() > 0) {
deleteCb = createCollectionDeleter(context);
if (inverseFlusher == null) {
deleteCb = createCollectionDeleter(context);
}
if (removeSpecific) {
String entityIdAttributeName = elementDescriptor.getEntityIdAttributeName();
if (entityIdAttributeName != null) {
removedObjects = appendRemoveSpecific(context, deleteCb, fusedCollectionActions);
removedAll = false;
if (removedObjects.isEmpty()) {
deleteCb = null;
if (inverseFlusher == null) {
String entityIdAttributeName = elementDescriptor.getEntityIdAttributeName();
if (entityIdAttributeName != null) {
removedObjects = appendRemoveSpecific(context, deleteCb, fusedCollectionActions);
removedAll = false;
if (removedObjects.isEmpty()) {
deleteCb = null;
}
}
} else {
for (Object o : fusedCollectionActions.getRemoved(context)) {
inverseFlusher.flushQuerySetElement(context, o, ownerView, null, null, null);
}
}
} else if (inverseFlusher != null) {
for (Object o : value) {
inverseFlusher.flushQuerySetElement(context, o, ownerView, null, null, null);
}
return true;
}
} else {
removedAll = false;
}
} else if (inverseFlusher != null) {
for (Object o : value) {
inverseFlusher.flushQuerySetElement(context, o, ownerView, null, null, null);
}
return true;
} else {
deleteCb = createCollectionDeleter(context);
}
Expand All @@ -422,67 +440,77 @@ protected boolean deleteElements(UpdateContext context, Object ownerView, Object
protected void addElements(UpdateContext context, Object ownerView, Object view, Collection<Object> removedAllObjects, boolean flushAtOnce, V value, List<Object> embeddablesToUpdate, FusedCollectionActions fusedCollectionActions) {
Collection<Object> elementsToAdd;
if (fusedCollectionActions == null || !removedAllObjects.isEmpty()) {
if (elementDescriptor.getViewToEntityMapper() == null) {
if (elementDescriptor.getViewToEntityMapper() == null || inverseFlusher != null) {
elementsToAdd = (Collection<Object>) value;
} else {
elementsToAdd = getEntityReferencesForCollectionOperation(context, (Collection<Object>) value);
}
removedAllObjects.removeAll(value);
} else {
removedAllObjects.removeAll(fusedCollectionActions.getAdded());
elementsToAdd = fusedCollectionActions.getAdded(context);
if (inverseFlusher == null) {
elementsToAdd = fusedCollectionActions.getAdded(context);
} else {
elementsToAdd = fusedCollectionActions.getAdded();
}
}
if (elementsToAdd == null || elementsToAdd.isEmpty() || elementsToAdd.size() == 1 && elementsToAdd.iterator().next() == null) {
return;
}

String mapping = getMapping();
InsertCriteriaBuilder<?> insertCb = context.getEntityViewManager().getCriteriaBuilderFactory().insertCollection(context.getEntityManager(), ownerEntityClass, mapping);
if (inverseFlusher == null) {
InsertCriteriaBuilder<?> insertCb = context.getEntityViewManager().getCriteriaBuilderFactory().insertCollection(context.getEntityManager(), ownerEntityClass, mapping);

String entityIdAttributeName = elementDescriptor.getEntityIdAttributeName();
if (flushAtOnce) {
if (entityIdAttributeName == null) {
insertCb.fromValues(ownerEntityClass, mapping, "val", elementsToAdd);
String entityIdAttributeName = elementDescriptor.getEntityIdAttributeName();
if (flushAtOnce) {
if (entityIdAttributeName == null) {
insertCb.fromValues(ownerEntityClass, mapping, "val", elementsToAdd);
} else {
insertCb.fromIdentifiableValues((Class<Object>) elementDescriptor.getJpaType(), "val", elementsToAdd);
}
} else {
insertCb.fromIdentifiableValues((Class<Object>) elementDescriptor.getJpaType(), "val", elementsToAdd);
if (entityIdAttributeName == null) {
insertCb.fromValues(ownerEntityClass, mapping, "val", 1);
} else {
insertCb.fromIdentifiableValues((Class<Object>) elementDescriptor.getJpaType(), "val", 1);
}
}
} else {
if (entityIdAttributeName == null) {
insertCb.fromValues(ownerEntityClass, mapping, "val", 1);
} else {
insertCb.fromIdentifiableValues((Class<Object>) elementDescriptor.getJpaType(), "val", 1);
for (int i = 0; i < ownerIdBindFragments.length; i += 2) {
insertCb.bind(ownerIdBindFragments[i]).select(ownerIdBindFragments[i + 1]);
}
}
for (int i = 0; i < ownerIdBindFragments.length; i += 2) {
insertCb.bind(ownerIdBindFragments[i]).select(ownerIdBindFragments[i + 1]);
}
insertCb.bind(mapping).select("val");
Query insertQuery = insertCb.getQuery();
ownerIdFlusher.flushQuery(context, null, insertQuery, ownerView, view, ownerIdFlusher.getViewAttributeAccessor().getValue(ownerView), null);
insertCb.bind(mapping).select("val");
Query insertQuery = insertCb.getQuery();
ownerIdFlusher.flushQuery(context, null, insertQuery, ownerView, view, ownerIdFlusher.getViewAttributeAccessor().getValue(ownerView), null);

boolean checkTransient = elementDescriptor.isJpaEntity() && !elementDescriptor.shouldJpaPersist();
if (flushAtOnce) {
if (checkTransient) {
boolean checkTransient = elementDescriptor.isJpaEntity() && !elementDescriptor.shouldJpaPersist();
if (flushAtOnce) {
if (checkTransient) {
for (Object o : elementsToAdd) {
if (elementDescriptor.getBasicUserType().shouldPersist(o)) {
throw new IllegalStateException("Collection " + attributeName + " references an unsaved transient instance - save the transient instance before flushing: " + o);
}
}
}
insertQuery.executeUpdate();
} else {
// TODO: Use batching when we implement #657
Object[] singletonArray = new Object[1];
List<Object> singletonList = Arrays.asList(singletonArray);
for (Object o : elementsToAdd) {
if (elementDescriptor.getBasicUserType().shouldPersist(o)) {
throw new IllegalStateException("Collection " + attributeName + " references an unsaved transient instance - save the transient instance before flushing: " + o);
if (o != null) {
if (checkTransient && elementDescriptor.getBasicUserType().shouldPersist(o)) {
throw new IllegalStateException("Collection " + attributeName + " references an unsaved transient instance - save the transient instance before flushing: " + o);
}
singletonArray[0] = o;
insertQuery.setParameter("val", singletonList);
insertQuery.executeUpdate();
}
}
}
insertQuery.executeUpdate();
} else {
// TODO: Use batching when we implement #657
Object[] singletonArray = new Object[1];
List<Object> singletonList = Arrays.asList(singletonArray);
for (Object o : elementsToAdd) {
if (o != null) {
if (checkTransient && elementDescriptor.getBasicUserType().shouldPersist(o)) {
throw new IllegalStateException("Collection " + attributeName + " references an unsaved transient instance - save the transient instance before flushing: " + o);
}
singletonArray[0] = o;
insertQuery.setParameter("val", singletonList);
insertQuery.executeUpdate();
}
inverseFlusher.flushQuerySetElement(context, o, null, ownerView, null, null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

/**
Expand Down Expand Up @@ -75,6 +73,7 @@ public void testAddNewElementToCollection() {
// When
UpdatableDocumentView document = evm.create(UpdatableDocumentView.class);
document.setName("newDoc123");
document.setOwner(getP1View(PersonIdView.class));
newPerson.getOwnedDocuments2().add(document);
update(newPerson);

Expand All @@ -84,7 +83,7 @@ public void testAddNewElementToCollection() {
// Then
restartTransaction();
Document newDocument = em.find(Document.class, document.getId());
Assert.assertEquals(newPerson.getId(), newDocument.getOwner().getId());
Assert.assertEquals(newPerson.getId(), newDocument.getResponsiblePerson().getId());
}

@Test
Expand All @@ -94,6 +93,7 @@ public void testReplaceCollection() {
newPerson.setName("newPers1");
UpdatableDocumentView document = evm.create(UpdatableDocumentView.class);
document.setName("newDoc123");
document.setOwner(getP1View(PersonIdView.class));
newPerson.getOwnedDocuments2().add(document);
update(newPerson);

Expand All @@ -104,7 +104,7 @@ public void testReplaceCollection() {
// Then
restartTransaction();
Document newDocument = em.find(Document.class, document.getId());
Assert.assertEquals(newPerson.getId(), newDocument.getOwner().getId());
Assert.assertEquals(newPerson.getId(), newDocument.getResponsiblePerson().getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
* @author Christian Beikov
* @since 1.3.0
*/
@CreatableEntityView(excludedEntityAttributes = { "owner", "idx", "age" })
@CreatableEntityView(excludedEntityAttributes = { "idx", "age" })
@UpdatableEntityView
@EntityView(Document.class)
public interface UpdatableDocumentView extends DocumentIdView {

String getName();
void setName(String name);

PersonIdView getOwner();
void setOwner(PersonIdView owner);
}

0 comments on commit 83e1348

Please sign in to comment.