Skip to content
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

feat: support JPA annotation mapping for object model #14626

Merged
merged 130 commits into from
Nov 16, 2023
Merged

Conversation

vietnguyen
Copy link
Contributor

@vietnguyen vietnguyen commented Jul 18, 2023

https://dhis2.atlassian.net/browse/TECH-1517

Scope

As mentioned here , hbm.xml mapping format is considered deprecated and will no longer supported beyond 6.x.

The goal of this PR is to support both Hibernate xml and JPA annotation mapping for object model in DHIS2. It doesn't upgrade Hibernate to version 6.x

Tasks

  • Make sure org.hibernate.Criteria is completely replaced by JPA query api.
  • Add javax.persistence.EntityManagerFactory bean to HibernateConfig so all DAO classes can use EntityManager instead of hibernate SessionFactory.
  • Support both hibernate *.hbm.xml mapping files and JPA annotation mapping. Will slowly migrate all hbm files to annotation.
  • Replace SessionFactory with EntityManager.
  • Find a way to inject EntityManager through constructor instead of field injection with @PersistenceContext, this is needed for unit tests with Mockito. Reference spring-projects/spring-data-jpa@7b54253

Next PRs

  • Turn off property hibernate.allow_update_outside_transaction as JPA doesn't allow calling session.update or session.flush outside of a transaction. This requires refactoring in places where we explicitly call session.update or session.flush without @Transactional wrapper.
  • session.flush() inside a transaction should be carefully tested in metadata import service. May need to be removed and let the objects to be managed by JPA PersistenContext.
  • Split the BaseIdentifiableObject into multiple base objects in order to support entity inheritance mapping for only required properties. Such as BaseShareableIdObject, BaseIdObject, BaseAttributeObject, etc..
  • Replace save/update with persist/merge

@vietnguyen vietnguyen marked this pull request as ready for review November 14, 2023 17:21
@vietnguyen vietnguyen changed the title feat: support JPA annotation mapping for object model [WIP] feat: support JPA annotation mapping for object model Nov 14, 2023
@@ -42,8 +42,6 @@ public interface ReservedValueStore extends GenericStore<ReservedValue> {
List<ReservedValue> getAvailableValues(
ReservedValue reservedValue, List<String> values, String ownerObject);

List<ReservedValue> reserveValuesJpa(ReservedValue reservedValue, List<String> values);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a code clean up done by Luca from tracker team. He helped to fix one of the unit test failed after using entityManager.

if (isEmpty(values) || !reservedValue.getOwnerObject().equals(ownerObject)) {
return List.of();
}
List<String> availableValues = getIfAvailable(reservedValue, values);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code clean up done by Luca.

@Override
public Serializable getIdentifier(Object object) {
return sessionFactory.getCurrentSession().getIdentifier(object);
}
Copy link
Contributor Author

@vietnguyen vietnguyen Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those methods are not in use

JsonNode node = mapper.readTree(result.getResponse().getContentAsString());
assertEquals(valueToCheck, node.get("displayName").asText());
}
}
Copy link
Contributor Author

@vietnguyen vietnguyen Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this test class as it is obsolete. The translations api is covered in AbstractCrudControllerTest.replaceTranslationsOk() and some other tests in that class.

public void setDatabaseInfoProvider(DatabaseInfoProvider databaseInfoProvider) {
this.databaseInfoProvider = databaseInfoProvider;
}
private final DatabaseInfoProvider databaseInfoProvider;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to fix a circular dependency between DataBaseInfoProvider and DhisConfigurationProvider

private String[] loadResources() {
try {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:org/hisp/dhis/**/*.hbm.xml");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this file check be more targeted? It looks like it's going to look through every package on the classpath when we know exactly where all hbm.xml files are located.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to do this. Will check later and update on next PR.

Copy link
Contributor

@david-mackessy david-mackessy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work Viet

Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.9% 0.9% Duplication

@vietnguyen vietnguyen merged commit 6eb421d into master Nov 16, 2023
@vietnguyen vietnguyen deleted the TECH-1517-41 branch November 16, 2023 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants