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

chore: migrate hbm.xml to orm.xml #17821

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

chore: migrate hbm.xml to orm.xml #17821

wants to merge 10 commits into from

Conversation

vietnguyen
Copy link
Contributor

@vietnguyen vietnguyen commented Jun 18, 2024

  • As hibernate legacy hbm.xml mapping has been deprecated for 6.x. We have options to use annotation or jpa orm.xml mapping. After discussed, we decided to use the JPA orm.xml mapping as it helps to avoid modifying all the Java Object Model classes and the object hierarchy.

  • This PR transforms all hibernate hbm.xml files to JPA orm.xml files which is based on JPA xsd schema http://xmlns.jcp.org/xml/ns/persistence/orm version = 2.2. It also adds JPA configuration for supporting this type of mapping.

  • This is the final preparation step for upgrading to Hibernate 6.x

Changes note

  • In MessageConversation.hbm.xml we use below filter supported by hibernate and its not supported in JPA. So I replaced it with a where clause in the affected query in HibernateMessageConversationStore.
 <filter-def name="userMessageUser">
    <filter-param name="userid" type="long" />
  </filter-def>

Mapping refactor samples

Many-to-many

hbm.xml

 <set name="userMessages" table="messageconversation_usermessages" cascade="all,delete-orphan">
      <key column="messageconversationid"
           foreign-key="fk_messageconversation_usermessages_messageconversationid" />
      <many-to-many class="org.hisp.dhis.message.UserMessage" column="usermessageid"
                    unique="true" foreign-key="fk_messageconversation_usermessages_usermessageid">
        <filter name="userMessageUser" condition="userid = :userid" />
      </many-to-many>
    </set>

orm.xml

<many-to-many name="userMessages" target-entity="org.hisp.dhis.message.UserMessage">
      <join-table name="messageconversation_usermessages">
          <join-column name="messageconversationid"/>
          <foreign-key name="fk_messageconversation_usermessages_messageconversationid"/> 
          <inverse-join-column name="usermessageid"/>
      </join-table>
      <cascade><cascade-all/></cascade>
  </many-to-many>

Todo

  • [WIP] Replace Hibernate UserType by JPA AttributeConverter which is used for mapping JsonB column.
  • Fix many-to-many mapping which is not supported by the hbm-to-orm transformation tool.
  • Fix Hibernate <component> mapping which is not supported by JPA orm mapping.
  • Test new mapping files with Hibernate 6.

Notes

Hibernate JPA
ID generator strategy, currently need to support PostgreSQL and H2 <generator class="native" /> <generated-value strategy="AUTO"/>
Fetch mode fetch=[select, subselect, join, batch] Doesn't have equivalent, default is LEFT JOIN unless overrided by JPQL or Native query.
Collection loading mode lazy=false fetch=EAGER
The default fetch-type are:
- ManyToOne: EAGER
- OneToMany: LAZY
- ManyToMany: LAZY
- OneToOne: EAGER

return List.of();
}

class DhisPersistenceUnitManager implements PersistenceUnitManager {

Check notice

Code scanning / CodeQL

Unused classes and interfaces Note

Unused class: DhisPersistenceUnitManager is not referenced within this codebase. If not used as an external API it should be removed.
return List.of();
}

class DhisPersistenceUnitManager implements PersistenceUnitManager {

Check notice

Code scanning / CodeQL

Inner class could be static Note

DhisPersistenceUnitManager should be made static, since the enclosing instance is not used.
Copy link

sonarcloud bot commented Jul 11, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
5 New issues
5 New Code Smells (required ≤ 0)
1 New Critical Issues (required ≤ 0)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

if (attribute == null) {
return null;
} else {
return encryptor.encrypt(attribute.toString());

Check notice

Code scanning / CodeQL

Useless toString on String Note

Redundant call to 'toString' on a String object.
@@ -117,8 +125,8 @@
return parentOrganisationUnit;
}

public void setParentOrganisationUnit(Boolean parentOrganisationUnit) {
this.parentOrganisationUnit = parentOrganisationUnit;
public void setOrganisationUnit(boolean organisationUnit) {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method ReportingParams.setOrganisationUnit(..) could be confused with overloaded method
setOrganisationUnit
, since dispatch depends on static types.
this.reportingPeriod = reportingPeriod;
}

public void setGrandParentOrganisationUnit(boolean grandParentOrganisationUnit) {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method ReportingParams.setGrandParentOrganisationUnit(..) could be confused with overloaded method
setGrandParentOrganisationUnit
, since dispatch depends on static types.
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.

1 participant