From 418f1a1c56be3c60d7cf17806768142b5ff1ffa9 Mon Sep 17 00:00:00 2001 From: Lukas Jungmann Date: Tue, 4 Jun 2019 00:05:10 +0200 Subject: [PATCH] [2.6] Rollback the fix for bug 411013 (#459) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Revert "Bug 411013: Entities in persistene context get validated on commit ev… (#270) (#271)" This reverts commit 95176b96d7f04e6a4585b59fa2943a9cb624060b. * Revert "[2.6] Bug 411013: Entities in persistence context get validated on commit even when unchanged (#248) (#252)" This reverts commit 4318a80e71568cd41f7f78217a970d577150328a. * update copyrights Signed-off-by: Lukas Jungmann --- .../DeferredChangeDetectionPolicy.java | 33 ++++++++-------- .../BeanValidationJunitTest.java | 38 ++----------------- .../listeners/BeanValidationListener.java | 20 +++++----- 3 files changed, 30 insertions(+), 61 deletions(-) diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/descriptors/changetracking/DeferredChangeDetectionPolicy.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/descriptors/changetracking/DeferredChangeDetectionPolicy.java index 213a0ce1266..3456c76ff34 100644 --- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/descriptors/changetracking/DeferredChangeDetectionPolicy.java +++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/descriptors/changetracking/DeferredChangeDetectionPolicy.java @@ -1,10 +1,10 @@ /******************************************************************************* - * Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved. - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 - * which accompanies this distribution. + * Copyright (c) 1998, 2019 Oracle and/or its affiliates. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Eclipse Distribution License is available at + * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: @@ -78,6 +78,7 @@ public ObjectChangeSet calculateChangesForExistingObject(Object clone, UnitOfWor public ObjectChangeSet calculateChanges(Object clone, Object backUp, boolean isNew, UnitOfWorkChangeSet changeSet, UnitOfWorkImpl unitOfWork, ClassDescriptor descriptor, boolean shouldRaiseEvent) { // PERF: Avoid events if no listeners. if (descriptor.getEventManager().hasAnyEventListeners() && shouldRaiseEvent) { + // The query is built for compatibility to old event mechanism. WriteObjectQuery writeQuery = new WriteObjectQuery(clone.getClass()); writeQuery.setObject(clone); writeQuery.setBackupClone(backUp); @@ -94,18 +95,16 @@ public ObjectChangeSet calculateChanges(Object clone, Object backUp, boolean isN } ObjectChangeSet changes = createObjectChangeSet(clone, backUp, changeSet, isNew, unitOfWork, descriptor); - if (changes.hasChanges()) { - if (descriptor.hasMappingsPostCalculateChanges() && ! changes.isNew() && ! unitOfWork.getCommitManager().isActive() && !unitOfWork.isNestedUnitOfWork()) { - // if we are in the commit because of an event skip this postCalculateChanges step as we have already executed it. - int size = descriptor.getMappingsPostCalculateChanges().size(); - for (int i=0; i < size; i++) { - DatabaseMapping mapping = descriptor.getMappingsPostCalculateChanges().get(i); - org.eclipse.persistence.sessions.changesets.ChangeRecord record = changes.getChangesForAttributeNamed(mapping.getAttributeName()); - if (record != null) { - // Deferred attributes will already have been acted on, therefore we need - // to post calculate changes to ensure orphaned objects are removed. - mapping.postCalculateChanges(record, unitOfWork); - } + if(changes.hasChanges() && descriptor.hasMappingsPostCalculateChanges() && ! changes.isNew() && ! unitOfWork.getCommitManager().isActive() && !unitOfWork.isNestedUnitOfWork()) { + // if we are in the commit because of an event skip this postCalculateChanges step as we have already executed it. + int size = descriptor.getMappingsPostCalculateChanges().size(); + for(int i=0; i < size; i++) { + DatabaseMapping mapping = descriptor.getMappingsPostCalculateChanges().get(i); + org.eclipse.persistence.sessions.changesets.ChangeRecord record = changes.getChangesForAttributeNamed(mapping.getAttributeName()); + if(record != null) { + // Deferred attributes will already have been acted on, therefore we need + // to post calculate changes to ensure orphaned objects are removed. + mapping.postCalculateChanges(record, unitOfWork); } } } diff --git a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/beanvalidation/BeanValidationJunitTest.java b/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/beanvalidation/BeanValidationJunitTest.java index f2ef271252c..56f5acdc467 100644 --- a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/beanvalidation/BeanValidationJunitTest.java +++ b/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/beanvalidation/BeanValidationJunitTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2019 Oracle and/or its affiliates. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. @@ -15,17 +15,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; -import java.util.Map; import java.util.Set; import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.TypedQuery; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; -import org.eclipse.persistence.logging.SessionLog; import org.eclipse.persistence.mappings.ForeignReferenceMapping; import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; import org.eclipse.persistence.testing.models.jpa.beanvalidation.Address; @@ -60,7 +55,7 @@ public static Test suite() { suite.addTest(new BeanValidationJunitTest("testUpdateWithInvalidData")); suite.addTest(new BeanValidationJunitTest("testRemoveWithInvalidData")); suite.addTest(new BeanValidationJunitTest("testTraversableResolverPreventsLoadingOfLazyRelationships")); - suite.addTest(new BeanValidationJunitTest("testValidateChangedData")); + suite.addTest(new BeanValidationJunitTest("testTraversableResolverPreventsTraversingRelationshipdMultipleTimes")); } return suite; } @@ -325,34 +320,7 @@ public void testTraversableResolverPreventsTraversingRelationshipdMultipleTimes( // If it ever finds an entity with such flag set, the entity has visited the validator twice. It should be flagged as error. } - //Bug #411013 - public void testValidateChangedData() { - try { - getDatabaseSession().executeNonSelectingSQL("insert into CMP3_BV_PROJECT values (895, \"some long name\")"); - } catch (Throwable t) { - getDatabaseSession().getSessionLog().logThrowable(SessionLog.WARNING, t); - } - clearCache(); - Map props = new HashMap<>(); - props.put("eclipselink.weaving", "false"); - EntityManagerFactory factory = getEntityManagerFactory(props); - EntityManager em = factory.createEntityManager(); - try { - beginTransaction(em); - TypedQuery query = em.createQuery("select p from CMP3_BV_PROJECT p", Project.class); - for (Project p: query.getResultList()) { - System.out.println(p.getName()); - } - commitTransaction(em); - } catch (RuntimeException ex) { - if (isTransactionActive(em)) { - rollbackTransaction(em); - } - throw ex; - } finally { - closeEntityManager(em); - } - } + //--------------------Helper Methods ---------------// private boolean isInstantiated(Object entityObject, String attributeName, org.eclipse.persistence.sessions.Project project) { diff --git a/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/metadata/listeners/BeanValidationListener.java b/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/metadata/listeners/BeanValidationListener.java index 44412bf1a7a..20b4a85da90 100644 --- a/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/metadata/listeners/BeanValidationListener.java +++ b/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/metadata/listeners/BeanValidationListener.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Sun Microsystems, Inc, IBM Corporation. All rights reserved. + * Copyright (c) 2009, 2019 Sun Microsystems, Inc, IBM Corporation. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. @@ -15,6 +15,11 @@ package org.eclipse.persistence.internal.jpa.metadata.listeners; +import java.lang.annotation.ElementType; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import javax.validation.Path; @@ -24,20 +29,15 @@ import javax.validation.groups.Default; import org.eclipse.persistence.config.PersistenceUnitProperties; -import org.eclipse.persistence.descriptors.DescriptorEventAdapter; -import org.eclipse.persistence.descriptors.DescriptorEvent; import org.eclipse.persistence.descriptors.ClassDescriptor; +import org.eclipse.persistence.descriptors.DescriptorEvent; +import org.eclipse.persistence.descriptors.DescriptorEventAdapter; import org.eclipse.persistence.descriptors.FetchGroupManager; import org.eclipse.persistence.internal.jpa.metadata.beanvalidation.BeanValidationHelper; import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl; import org.eclipse.persistence.mappings.DatabaseMapping; import org.eclipse.persistence.mappings.ForeignReferenceMapping; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.lang.annotation.ElementType; - /** * Responsible for performing automatic bean validation on call back events. * @author Mitesh Meswani @@ -79,7 +79,7 @@ public void prePersist (DescriptorEvent event) { } @Override - public void aboutToUpdate (DescriptorEvent event) { + public void preUpdate (DescriptorEvent event) { Object source = event.getSource(); UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl )event.getSession(); // preUpdate is also generated for deleted objects that were modified in this UOW. @@ -150,6 +150,7 @@ private static class AutomaticLifeCycleValidationTraversableResolver implements /** * @return false for any lazily loaded property of root object being validated */ + @Override public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class rootBeanType, Path pathToTraversableObject, ElementType elementType) { boolean reachable = true; String attributeName = null; @@ -180,6 +181,7 @@ public boolean isReachable(Object traversableObject, Path.Node traversableProper * Called only if isReachable returns true * @return false for any associatons of root object being validated true otherwise */ + @Override public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class rootBeanType, Path pathToTraversableObject, ElementType elementType) { boolean cascadable = true; if (isRootObjectPath(pathToTraversableObject)) {