Skip to content

Hibernate

Michael Calmer edited this page Oct 6, 2025 · 3 revisions

Hibernate Database Model

Uyuni uses Hibernate to create Java Objects for Database Tables

Hibernate migration to version 7

To migrate Hibernate from version 5 to 7 we need to do some preparation steps

Migrate from hbm.xml to annotations

Hibernate uses since a while annotations for the mapping of Database tables to Java Objects. In the past this was done in XML files (hbm.xml). Both is supported by Hibernate 5. Future versions of hibernate had some problems with this and we decided to first migrate everything to annotations before we start to update hibernate to a new major version.

The hbm.xml files have 2 sections:

  1. the mapping of tables and columns to objects
  2. Named queries

The mapping will changed to annotations in the java file.

The Queries should be converted to strings directly in Java Factory classes. We will change from "Named" Queries to pure Queries. Named Queries have to defined also as annotations in the Java file. This mess up the file and makes it hard readable. It is easier to read and understand when the query is directly at the place where we also set the parameters and get the results.

We stay with Hibernate version 5 as long as these migration finished. We should run the unit and acceptance tests regular with these changes to find and fix problems as early as possible.

Examples

How to write queries

HQL Query:

getSession().createQuery("""
    FROM ServerPath
    WHERE server = :server
    AND proxyServer = :proxyserver
    """, ServerPath.class)
    .setParameter("server", server)
    .setParameter("proxyserver", proxyServer)
    .uniqueResultOptional();

Native Query;

getSession().createNativeQuery("""
    SELECT * from rhnServerPath
    WHERE server_id = :server
    AND proxy_server_id = :proxyserver
    """, ServerPath.class)
    .setParameter("server", server.getId())
    .setParameter("proxyserver", proxyServer.getId())
    .uniqueResultOptional();
How to handle cascade type "save-update"?

https://docs.jboss.org/hibernate/orm/7.0/migration-guide/migration-guide.html#session-save-update

Relatedly, org.hibernate.annotations.CascadeType#SAVE_UPDATE has been removed
in favor of org.hibernate.annotations.CascadeType#PERSIST and/or
org.hibernate.annotations.CascadeType#MERGE

This means a cascade="save-update" will result into cascade = {CascadeType.PERSIST, CascadeType.MERGE}

How to migrate a "Bag"

Bag is something which does not exist in JPA. It was a unordered list which could have duplicates.

When you find a "Bag", check if this is really needed. If not you can migrate it to a ManyToMany releation or similar.

Migrate to new Hibernate version

We start the migration to a new Hibernate version when we migrated everything to annotations.

  • build new hibernate packages
  • check the dependencies if we have them already in openSUSE or OBS
  • If we need to build a packages, try to build it with xmvn style if possible. If this does not work we have to build them with tetra

Clone this wiki locally