Platform.sh is now Upsun. Click here to learn more
Upsun Fixed User Documentation

Deploy Hibernate ORM on Upsun Fixed

Try Upsun for 15 days
After that, enjoy the same, game-changing Upsun features for less with the First Project Incentive!¹ A monthly $19 perk!
Activate your 15-day trial
¹Terms and conditions apply

Hibernate ORM is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate handles object-relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.

Services Anchor to this heading

Configuration reader Anchor to this heading

While you can read the environment directly from your app, you might want to use theJava configuration reader library. It decodes service credentials, the correct port, and other information for you.

Note that the Java configuration reader library is used in the following examples.

MySQL Anchor to this heading

MySQL is an open-source relational database technology. Define the driver for MySQL, and the Java dependencies. Then determine the SessionFactory client programmatically:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import sh.platform.config.Config;
import sh.platform.config.Hibernate;

public class HibernateApp {

    public static void main(String[] args) {
        Config config = new Config();
        Configuration configuration = new Configuration();
        configuration.addAnnotatedClass(Address.class);
        final Hibernate credential = config.getCredential("database", Hibernate::new);
        final SessionFactory sessionFactory = credential.getMySQL(configuration);
        try (Session session = sessionFactory.openSession()) {
            Transaction transaction = session.beginTransaction();
            //...
            transaction.commit();
        }
    }
}

MariaDB Anchor to this heading

MariaDB is an open-source relational database technology. Define the driver for MariaDB, and the Java dependencies. Then determine the SessionFactory client programmatically:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import sh.platform.config.Config;
import sh.platform.config.Hibernate;

public class HibernateApp {

    public static void main(String[] args) {
        Config config = new Config();
        Configuration configuration = new Configuration();
        configuration.addAnnotatedClass(Address.class);
        final Hibernate credential = config.getCredential("database", Hibernate::new);
        final SessionFactory sessionFactory = credential.getMariaDB(configuration);
        try (Session session = sessionFactory.openSession()) {
            Transaction transaction = session.beginTransaction();
            //...
            transaction.commit();
        }
    }
}

PostgreSQL Anchor to this heading

PostgreSQL is an open-source relational database technology. Define the driver for PostgreSQL, and the Java dependencies. Then determine the SessionFactory client programmatically:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import sh.platform.config.Config;
import sh.platform.config.Hibernate;

public class HibernateApp {

    public static void main(String[] args) {
        Config config = new Config();
        Configuration configuration = new Configuration();
        configuration.addAnnotatedClass(Address.class);
        final Hibernate credential = config.getCredential("database", Hibernate::new);
        final SessionFactory sessionFactory = credential.getPostgreSQL(configuration);
        try (Session session = sessionFactory.openSession()) {
            Transaction transaction = session.beginTransaction();
            //...
            transaction.commit();
        }
    }
}