36
36
import javax .validation .ValidatorFactory ;
37
37
import javax .validation .groups .Default ;
38
38
39
+ import org .eclipse .persistence .config .EntityManagerProperties ;
39
40
import org .eclipse .persistence .config .PersistenceUnitProperties ;
40
41
import org .eclipse .persistence .descriptors .ClassDescriptor ;
41
42
import org .eclipse .persistence .descriptors .DescriptorEvent ;
42
43
import org .eclipse .persistence .descriptors .DescriptorEventAdapter ;
43
44
import org .eclipse .persistence .descriptors .FetchGroupManager ;
45
+ import org .eclipse .persistence .exceptions .BeanValidationException ;
44
46
import org .eclipse .persistence .internal .localization .ExceptionLocalization ;
45
47
import org .eclipse .persistence .internal .security .PrivilegedAccessHelper ;
46
48
import org .eclipse .persistence .internal .sessions .UnitOfWorkImpl ;
47
49
import org .eclipse .persistence .mappings .DatabaseMapping ;
48
50
import org .eclipse .persistence .mappings .ForeignReferenceMapping ;
49
51
52
+
50
53
/**
51
54
* Responsible for performing automatic bean validation on call back events.
52
55
* @author Mitesh Meswani
@@ -72,7 +75,7 @@ public BeanValidationListener(ValidatorFactory validatorFactory, Class[] groupPr
72
75
73
76
@ Override
74
77
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
76
79
// following example
77
80
// tx.begin()
78
81
// e = new MyEntity(...);
@@ -81,19 +84,38 @@ public void prePersist (DescriptorEvent event) {
81
84
// tx.commit();
82
85
// "invalid data" would get inserted into database.
83
86
//
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.
85
88
// 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
+ }
87
100
}
88
101
89
- @ Override
90
102
public void aboutToUpdate (DescriptorEvent event ) {
91
103
Object source = event .getSource ();
92
104
UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl )event .getSession ();
93
105
// preUpdate is also generated for deleted objects that were modified in this UOW.
94
106
// 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
+ }
97
119
}
98
120
}
99
121
@@ -104,9 +126,17 @@ public void preUpdateWithChanges(DescriptorEvent event) {
104
126
105
127
@ Override
106
128
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
+ }
110
140
}
111
141
112
142
private void validateOnCallbackEvent (DescriptorEvent event , String callbackEventName , Class [] validationGroup ) {
0 commit comments