-
Notifications
You must be signed in to change notification settings - Fork 181
[2.7] Close #1806 - Have to the ability to dynamically set the Validation Group during a transaction #2010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.7
Are you sure you want to change the base?
Conversation
Hello Eclipselink team, hoping for comment and suggested improvements. Thank you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- For functional changes like this we expect some test (in this case integration test). Please check and maybe extend
jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/beanvalidation/BeanValidationJunitTest.java
- Please ensure, that copyright year in the modified files is updated.
...org.eclipse.persistence.core/src/org/eclipse/persistence/config/EntityManagerProperties.java
Show resolved
Hide resolved
Thank you for the direction! I really appreciate it. We'll get to work on these things. |
This pul isn't abandoned, just still figuring out how to write tests :) |
2f39761
to
ce8a62e
Compare
still haven't abandoned this, I'll get to it. |
fea8c89
to
ce8a62e
Compare
Please check and sign eclipsefdn/eca |
...link.jpa.test/src/org/eclipse/persistence/testing/models/jpa/beanvalidation/GermanPhone.java
Show resolved
Hide resolved
...ipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/beanvalidation/USPhone.java
Show resolved
Hide resolved
e53295c
to
37d396f
Compare
37d396f
to
e50fd9a
Compare
…t the Validation Group during a transaction. Add tests, update copyright headers.
dfffbd6
to
115e40b
Compare
It's all good now! there was an errant signature |
...link.jpa.test/src/org/eclipse/persistence/testing/models/jpa/beanvalidation/GermanPhone.java
Show resolved
Hide resolved
...org.eclipse.persistence.core/src/org/eclipse/persistence/config/EntityManagerProperties.java
Outdated
Show resolved
Hide resolved
// This might be corrected in next iteration of spec | ||
validateOnCallbackEvent(event, "prePersist", groupPrePersit); | ||
Object overrideGroups = event.getSession().getParent().getProperties().get(EntityManagerProperties.VALIDATION_GROUP_PRE_PERSIST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think, that this code should be there.
For EntityManager properties is location
https://github.com/eclipse-ee4j/eclipselink/blob/2.7/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerSetupImpl.java#L4009
https://github.com/eclipse-ee4j/eclipselink/blob/2.7/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerSetupImpl.java#L4038
or
https://github.com/eclipse-ee4j/eclipselink/blob/2.7/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/deployment/BeanValidationInitializationHelper.java#L45
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, I don't think I understand what you're saying.
Perhaps this is the confusion: my patch is designed to allow the user to override these settings during execution. Otherwise the validation groups are fixed in stone and cannot be modified.
Sorry I'm missing something obvious I feel like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, that locations which are mentioned in the links are correct where new EntityManager properties should be evaluated like other properties. Not there. This is not consistent with other EntityManager properties or PersistenceUnit bean validation properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you again for working with me. I read all three of your examples. I'm not trying to say my code is perfect (totally aware of my absolute average skills) however, I don't think I can do use them for this purpose.
Perhaps a use case would be beneficial? Here is the scenario. We have a form where the user provides 'tidbits' of information as they progress through the form. We want to save those off to db as we go. However, eventually all of the fields need to be required. Why is this a problem currently? Well JPA only allows validation groups to be defined for an EntityManger at the time of creation. When your container is injecting these persistence contexts for you, you can override them. Thats what this patch does. Take a look here:
import javax.enterprise.context.ApplicationScoped;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import org.eclipse.persistence.config.EntityManagerProperties;
import lombok.Data;
@ApplicationScoped
@Transactional(TxType.MANDATORY)
public class YeeHahDatabaseService {
@PersistenceContext(unitName = "superbiz-model")
private EntityManager em;
public void addContact(final String contactName) {
final SuperBizContact contact = new SuperBizContact();
contact.setName(contactName);
em.persist(contact); // Would throw an error because phone is marked notNull! But I don't have phone yet!
}
public void addContactWithPhone(final String contactName, final String phone) {
final SuperBizContact contact = new SuperBizContact();
contact.setName(contactName);
contact.setPhone(phone);
em.persist(contact); // All good here, both fields set
}
public void addContatc2(final String contactName) {
final SuperBizContact contact = new SuperBizContact();
contact.setName(contactName);
em.setProperty(EntityManagerProperties.VALIDATION_GROUP_PRE_PERSIST, NameOnly.class);
em.persist(contact); // This time the persist works!
}
@Data
@Entity
public class SuperBizContact {
@Column
@NotNull(groups = { Default.class, NameOnly.class })
private String name;
@Column
@NotNull
private String phone;
}
public @interface NameOnly {
}
}
Example 1/2: EntityManagerSetupImpl.java
as far as I can tell, this code has already fired by the time the dev sees the injection.
Example 3: BeanValidationInitializationHelper
same here. This code has fired already when the dev see the injection.
The purpose of this property is to allow the dev to set the EntityManager for the current @PersistenceContext
rather than reading it from an unmodifiable configuration XML (which is actually already supported).
if(!unitOfWork.isObjectDeleted(source)) { | ||
validateOnCallbackEvent(event, "preUpdate", groupPreUpdate); | ||
if (!unitOfWork.isObjectDeleted(source)) { | ||
Object overrideGroups = event.getSession().getParent().getProperties().get(EntityManagerProperties.VALIDATION_GROUP_PRE_UPDATE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
if(groupPreRemove != null) { //No validation performed on preRemove if user has not explicitly specified a validation group | ||
validateOnCallbackEvent(event, "preRemove", groupPreRemove); | ||
} | ||
Object overrideGroups = event.getSession().getParent().getProperties().get(EntityManagerProperties.VALIDATION_GROUP_PRE_REMOVE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
@rfelcman If I could have a bit of feedback, I added several tests, but I'm not convinced they're actually running, because I never see them in the junit test report html. Whats the ant command to run an individual test class? I've tried several things:
Any help appreciated! |
ant -f antbuild.xml test-jpa22 is better Individual test class in 2.7 (Ant build) is complicated. It's easier start with other higher branches (3.0 and above) which are based on Maven and make backport. |
Hello, this PR Close #1806 for the 2.7 branch. The goal here is to allow a user to specify a validation group that should be executed when persisting. I'm hoping to sort of model this how the schema-per-tenant properties work (Which is very handy btw).
I have a signed ECA, but this is my first PR for EclipseLink so I could use some feedback. If this gets merged, then I'll port the changes to the other branches seeing active development.
Thank you!