-
Notifications
You must be signed in to change notification settings - Fork 4.2k
TRUNK-6533: Replace deprecated Hibernate Criteria with JPA Criteria #5731
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
Conversation
|
@dkayiwa , can you please review! |
|
@dkayiwa, can you please review if all good! |
|
I find it easier to review smaller pull requests. Can you limit the changes to strictly only switching from hibernate criteria to JPA? You can always open new pull requests for further improvements. |
@dkayiwa, can you please review now as I have mention only PR specific changes! |
| */ | ||
| @Test | ||
| public void saveConceptClass_shouldSaveTheGivenConceptClass() { | ||
| int unusedConceptClassId = 123; |
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.
Do you mind explaining how this is related to the ticket?
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.
@dkayiwa, It is using the id which is not even present in the database, which causes row deleted or updated exception!
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.
Why was the exception not happening before your changes?
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.
previously saveOrUpdate method is used for both save and update, here if id is not present in db then it will recreate the row with new Id.
Now we are using persist() for save and merge for update. If the Id is given for an object it will go for merge and try to fetch corresponding row with the given Id which cause this error!
|
|
||
| String hql = ""; | ||
|
|
||
| StringBuilder hql = new StringBuilder(); |
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.
Is this also related to the ticket?
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.
with String, it does not pass security checks of sonar.
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.
That is exactly what i mean by using separate pull requests to improve the existing code. I am just trying to reduce the cognitive load when doing these reviews. Can you take a second look at this? https://opensource.com/article/18/6/anatomy-perfect-pull-request
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.
That is exactly what i mean by using separate pull requests to improve the existing code. I am just trying to reduce the cognitive load when doing these reviews. Can you take a second look at this? https://opensource.com/article/18/6/anatomy-perfect-pull-request
I will replace this section with the required once!
| @Override | ||
| public ConceptReferenceRange saveConceptReferenceRange(ConceptReferenceRange conceptReferenceRange) { | ||
| sessionFactory.getCurrentSession().saveOrUpdate(conceptReferenceRange); | ||
| if(conceptReferenceRange == null) |
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.
This change is intentional to fail fast by explicitly checking the ID and throwing a clear exception, instead of relying on saveOrUpdate() to throw an internal IllegalArgumentException for null values.
|
@dkayiwa, can you review if all okay? |
| * @return the single result or null if no result is found | ||
| * @throws NonUniqueResultException if more than one result is found | ||
| */ | ||
| @Deprecated |
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.
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.
@dkayiwa, as per the deprecation documentation, internal DAO methods are not subject to deprecation. Since this is an internal utility, I’ve removed the annotation.
| public ConceptClass saveConceptClass(ConceptClass cc) throws DAOException { | ||
| sessionFactory.getCurrentSession().saveOrUpdate(cc); | ||
| Session session = sessionFactory.getCurrentSession(); | ||
| if(cc.getId() != null) |
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.
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.
@dkayiwa, reimplemented as per the project’s code style.
| public ConceptClass saveConceptClass(ConceptClass cc) throws DAOException { | ||
| sessionFactory.getCurrentSession().saveOrUpdate(cc); | ||
| Session session = sessionFactory.getCurrentSession(); | ||
| if(cc.getId() != null) { |
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.
We put space after if as you can see here: https://openmrs.atlassian.net/wiki/spaces/docs/pages/25477044/Java+Conventions#Code-Style
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.
@dkayiwa, I have fixed the changes!
8e431ff to
f8405fb
Compare
| public ConceptClass saveConceptClass(ConceptClass cc) throws DAOException { | ||
| sessionFactory.getCurrentSession().saveOrUpdate(cc); | ||
| Session session = sessionFactory.getCurrentSession(); | ||
| if (cc.getId() != null) { |
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.
This logic is sprinkled in lots of other places. Doesn't it call for creating a utility method?
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.
@dkayiwa, can you please review this change!
| sessionFactory.getCurrentSession().saveOrUpdate(cc); | ||
| return cc; | ||
| Session session = sessionFactory.getCurrentSession(); | ||
| return JpaUtils.saveOrUpdate(session,cc); |
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.
We recommend putting a space after the comms.
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.
done!
| public ConceptDatatype saveConceptDatatype(ConceptDatatype cd) throws DAOException { | ||
| sessionFactory.getCurrentSession().saveOrUpdate(cd); | ||
| return cd; | ||
| Session session = sessionFactory.getCurrentSession(); |
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.
Can we get rid of the session variable?
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.
Done!
| public ConceptReferenceRange saveConceptReferenceRange(ConceptReferenceRange conceptReferenceRange) { | ||
| sessionFactory.getCurrentSession().saveOrUpdate(conceptReferenceRange); | ||
| return conceptReferenceRange; | ||
| if (conceptReferenceRange == null) { |
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.
Is this part of the ticket?
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.
@dkayiwa, There is a test case that verifies this method throws an IllegalArgumentException when null is passed. Previously, saveOrUpdate would throw this exception through its internal logic, but now we need to add an explicit null check first so the expected exception is thrown before proceeding.
| * @return the saved or updated entity | ||
| */ | ||
| public static <T extends BaseOpenmrsObject> T saveOrUpdate(Session session, T entity) { | ||
| if (entity.getId() != null) { |
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.
Shouldn't this also use the utility method?
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.
Sure, I will add null check in this utility method!
|
@dkayiwa, can you review if all good! |
| Query query = sessionFactory.getCurrentSession().createQuery("from ConceptNumeric where conceptId = :conceptId") | ||
| .setParameter("conceptId", i); | ||
| obj = JpaUtils.getSingleResultOrNull(query); | ||
| TypedQuery<ConceptNumeric> sql = sessionFactory.getCurrentSession() |
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.
Is this sql or query? In other words, is there a reason why you changed the variable name?
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.
The variable name change was unintentional and happened while making multiple changes. I’ll revert it to keep the naming consistent.
|
@dkayiwa, can you please review! |
|



TRUNK-6533: Replace deprecated Hibernate Criteria with JPA Criteria
Description of what I changed
Issue I worked on
see https://issues.openmrs.org/browse/TRUNK-6533
Checklist: I completed these to help reviewers :)
My IDE is configured to follow the code style of this project.
I have added tests to cover my changes. (If you refactored
existing code that was well tested you do not have to add tests)
I ran
mvn clean packageright before creating this pull request andadded all formatting changes to my commit.
All new and existing tests passed.
My pull request is based on the latest changes of the master branch.