Deploy Hibernate ORM on Platform.sh
Back to home
On this page
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
Configuration reader
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
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();
}
}
}
Note
You can use the same MySQL driver for MariaDB as well if you wish to do so.
MariaDB
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
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();
}
}
}