3636import javax .validation .ValidatorFactory ;
3737import javax .validation .groups .Default ;
3838
39+ import org .eclipse .persistence .config .EntityManagerProperties ;
3940import org .eclipse .persistence .config .PersistenceUnitProperties ;
4041import org .eclipse .persistence .descriptors .ClassDescriptor ;
4142import org .eclipse .persistence .descriptors .DescriptorEvent ;
4243import org .eclipse .persistence .descriptors .DescriptorEventAdapter ;
4344import org .eclipse .persistence .descriptors .FetchGroupManager ;
45+ import org .eclipse .persistence .exceptions .BeanValidationException ;
4446import org .eclipse .persistence .internal .localization .ExceptionLocalization ;
4547import org .eclipse .persistence .internal .security .PrivilegedAccessHelper ;
4648import org .eclipse .persistence .internal .sessions .UnitOfWorkImpl ;
4749import org .eclipse .persistence .mappings .DatabaseMapping ;
4850import 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