Skip to content

Commit 2f39761

Browse files
committed
Close #1806 - Have to the ability to dynamically set the Validation Group during a transaction
1 parent 362e4fb commit 2f39761

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/config/EntityManagerProperties.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,21 @@ public class EntityManagerProperties {
319319
* )
320320
*/
321321
public static final String COMPOSITE_UNIT_PROPERTIES = PersistenceUnitProperties.COMPOSITE_UNIT_PROPERTIES;
322+
323+
/**
324+
* Overrides the Bean Validation Group(s) that will execute during a prePersist event. This should be a class or class[].
325+
*/
326+
public static final String VALIDATION_GROUP_PRE_PERSIST = "eclipselink.validation.group.prePersist";
327+
328+
/**
329+
* Overrides the Bean Validation Group(s) that will execute during a preUpdate event. This should be a class or class[].
330+
*/
331+
public static final String VALIDATION_GROUP_PRE_UPDATE = "eclipselink.validation.group.preUpdate";
332+
333+
/**
334+
* Overrides the Bean Validation Group(s) that will execute during a preRemove event. This should be a class or class[].
335+
*/
336+
public static final String VALIDATION_GROUP_PRE_REMOVE = "eclipselink.validation.group.preRemove";
322337

323338
private static final Set<String> supportedProperties = new HashSet<String>() {
324339

@@ -344,6 +359,9 @@ public class EntityManagerProperties {
344359
add(PERSISTENCE_CONTEXT_COMMIT_ORDER);
345360
add(FLUSH_CLEAR_CACHE);
346361
add(COMPOSITE_UNIT_PROPERTIES);
362+
add(VALIDATION_GROUP_PRE_PERSIST);
363+
add(VALIDATION_GROUP_PRE_UPDATE);
364+
add(VALIDATION_GROUP_PRE_REMOVE);
347365
}
348366
};
349367

jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/metadata/listeners/BeanValidationListener.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@
3636
import javax.validation.ValidatorFactory;
3737
import javax.validation.groups.Default;
3838

39+
import org.eclipse.persistence.config.EntityManagerProperties;
3940
import org.eclipse.persistence.config.PersistenceUnitProperties;
4041
import org.eclipse.persistence.descriptors.ClassDescriptor;
4142
import org.eclipse.persistence.descriptors.DescriptorEvent;
4243
import org.eclipse.persistence.descriptors.DescriptorEventAdapter;
4344
import org.eclipse.persistence.descriptors.FetchGroupManager;
45+
import org.eclipse.persistence.exceptions.BeanValidationException;
4446
import org.eclipse.persistence.internal.localization.ExceptionLocalization;
4547
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
4648
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
4749
import org.eclipse.persistence.mappings.DatabaseMapping;
4850
import org.eclipse.persistence.mappings.ForeignReferenceMapping;
4951

52+
5053
/**
5154
* Responsible for performing automatic bean validation on call back events.
5255
* @author Mitesh Meswani
@@ -72,7 +75,7 @@ public BeanValidationListener(ValidatorFactory validatorFactory, Class[] groupPr
7275

7376
@Override
7477
public void prePersist (DescriptorEvent event) {
75-
// since we are using prePersist to perform validation, invlid data may get inserted into database as shown by
78+
// since we are using prePersist to perform validation, invalid data may get inserted into database as shown by
7679
// following example
7780
// tx.begin()
7881
// e = new MyEntity(...);
@@ -81,19 +84,38 @@ public void prePersist (DescriptorEvent event) {
8184
// tx.commit();
8285
// "invalid data" would get inserted into database.
8386
//
84-
// preInsert can be used to work around above issue. Howerver, the JPA spec does not itent it.
87+
// preInsert can be used to work around above issue. However, the JPA spec does not itent it.
8588
// This might be corrected in next iteration of spec
86-
validateOnCallbackEvent(event, "prePersist", groupPrePersit);
89+
Object overrideGroups = event.getSession().getParent().getProperties().get(EntityManagerProperties.VALIDATION_GROUP_PRE_PERSIST);
90+
if (overrideGroups != null) {
91+
if (overrideGroups instanceof Class) {
92+
overrideGroups = new Class[] { (Class) overrideGroups };
93+
} else if (!(overrideGroups instanceof Class[])) {
94+
throw new BeanValidationException("prePersist validation group must be a Class or Class Array:" + overrideGroups);
95+
}
96+
validateOnCallbackEvent(event, "prePersist", (Class[]) overrideGroups);
97+
} else {
98+
validateOnCallbackEvent(event, "prePersist", groupPrePersit);
99+
}
87100
}
88101

89-
@Override
90102
public void aboutToUpdate(DescriptorEvent event) {
91103
Object source = event.getSource();
92104
UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl )event.getSession();
93105
// preUpdate is also generated for deleted objects that were modified in this UOW.
94106
// Do not perform preUpdate validation for such objects as preRemove would have already been called.
95-
if(!unitOfWork.isObjectDeleted(source)) {
96-
validateOnCallbackEvent(event, "preUpdate", groupPreUpdate);
107+
if (!unitOfWork.isObjectDeleted(source)) {
108+
Object overrideGroups = event.getSession().getParent().getProperties().get(EntityManagerProperties.VALIDATION_GROUP_PRE_UPDATE);
109+
if (overrideGroups != null) {
110+
if (overrideGroups instanceof Class) {
111+
overrideGroups = new Class[] { (Class) overrideGroups };
112+
} else if (!(overrideGroups instanceof Class[])) {
113+
throw new BeanValidationException("preUpdate validation group must be a Class or Class Array:" + overrideGroups);
114+
}
115+
validateOnCallbackEvent(event, "preUpdate", (Class[]) overrideGroups);
116+
} else {
117+
validateOnCallbackEvent(event, "preUpdate", groupPreUpdate);
118+
}
97119
}
98120
}
99121

@@ -104,9 +126,17 @@ public void preUpdateWithChanges(DescriptorEvent event) {
104126

105127
@Override
106128
public void preRemove (DescriptorEvent event) {
107-
if(groupPreRemove != null) { //No validation performed on preRemove if user has not explicitly specified a validation group
108-
validateOnCallbackEvent(event, "preRemove", groupPreRemove);
109-
}
129+
Object overrideGroups = event.getSession().getParent().getProperties().get(EntityManagerProperties.VALIDATION_GROUP_PRE_REMOVE);
130+
if (overrideGroups != null) {
131+
if (overrideGroups instanceof Class) {
132+
overrideGroups = new Class[] { (Class) overrideGroups };
133+
} else if (!(overrideGroups instanceof Class[])) {
134+
throw new BeanValidationException("preRemove validation group must be a Class or Class Array:" + overrideGroups);
135+
}
136+
validateOnCallbackEvent(event, "preRemove", (Class[]) overrideGroups);
137+
} else if (groupPreRemove != null) { // No validation performed on preRemove if user has not explicitly specified a validation group
138+
validateOnCallbackEvent(event, "preRemove", groupPreRemove);
139+
}
110140
}
111141

112142
private void validateOnCallbackEvent(DescriptorEvent event, String callbackEventName, Class[] validationGroup) {

0 commit comments

Comments
 (0)