Posted on Leave a comment

Using Hibernate In Standalone App (without Spring) using MariaDB

So recently I need to set up a quick project to store the results of my crawler in a database. Previously, I would use JDBC for this job. However, with the recent knowledge of Hibernate, why don’t I give it a go?

In this post, I’m going to show you how you can use Hibernate to store entities just like you can with frameworks like Spring but on a standalone app (commandline app/Desktop app such as JavaFX).

The application will save an entity Book into a database called book_db

Quick MariaDB setup

To quickly set up a MariaDB database, I use the following docker-compose file. I use mariadb:10.5 and PHPMyAdmin for easy access to the database. If you prefer, you can skip using PHPMyAdmin and use another version of MariaDB:

version: "3"
    image: mariadb:10.5
    container_name: db
      - MYSQL_DATABASE=speech_db
    restart: always
    - 13308:3306
      - mysql_vol:/var/lib/mysql
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
      - PMA_HOST=db
      - PMA_PORT=3306
    restart: always
      - 18811:80

Now, simply run docker-compose up -d, you have a fully functional MariaDB instance up and running.

Import Hibernates and other convenient libraries using Maven

Here is the dependency section in pom.xml


        <!-- -->



Other than hibernate and MySQL connector, the other are optional.

Create hibernate.cfg.xml to setup database connection

The next step would be to create a hibernate.cfg.xml file to configure the connection to the database. Where should you put this file? It should be under resources directory like I have here:

In this file, you specify the db configuration like so:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM

        <property name = "hibernate.dialect">

        <property name = "hibernate.connection.driver_class">

        <!-- Assume test is the database name -->

        <property name = "hibernate.connection.url">

        <property name = "hibernate.connection.username">

        <property name = "hibernate.connection.password">

Also, open phpMyadmin and create the following table:

Now the hard part is done. Let’s create some entities and save them to the database.

Actually save an entity to database

Let’s create an entity like this:

package entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Table(name = "book")
public class Book {
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    String title;
    String author;
    int pages;


And to save the entity, we need a session, which provided by SessionFactory. Let’s create a class that provides SessionFactory

import entity.Book;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactoryMaker {
    private static SessionFactory factory;

    private static void configureFactory()
        try {
            factory = new Configuration()
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);

    public static org.hibernate.SessionFactory getFactory() {
        if (factory == null) {

        return factory;


One very important line in this class is #12. It is very important that you add the entity to the configuration. Without this step, hibernate will not know Book is an entity.

Finally, let’s create a book and insert it:

import entity.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Runner {
    public static void main(String[] args) {

        Book b = new Book();
        b.setAuthor("No name");
        b.setTitle("Some book");
        SessionFactory factory = SessionFactoryMaker.getFactory();

        try (Session session = factory.openSession()) {
            Transaction tx = session.beginTransaction();
        } catch (Exception ex) {

Running this would create a record in the database:

You can find the full repo here

Posted on Leave a comment

Building Simple HTML Server Using JavaFX & Undertow

I recently had a customer who wants to view swagger-ui documentation for his APIs but he didn’t know how to set up a server and he wants to run in on his own laptop, not hosting anywhere else. With developers like us, there is no problem setting up a quick docker project and view the content. However, installing Docker is quite a lot for a non-tech user.

So, I decided to create a small JavaFX application that can run right away (given that the customer has JRE installed or can download and install quickly).

Application source code

You can download the source code from github and try yourself here:

After that, you can simply run the Launcher file to have the server running.

How to use the application

When you launch the application, a small window appears like this:

Simple http server

There isn’t much thing to do, simply select the folder to be the web root and the port you want to run and click on start server.

If I visit the link http://localhost:9988 in my browser, this is what I see:

And you can easily navigate through the files.

One nice thing is you can use this to serve your videos (*.mp4) and watch on your phone (by accessing via your computer’s IP).

Posted on Leave a comment

JavaFX Starter Project – Quickly create and run

JavaFX is a great tool to quickly deploy desktop applications. However, the setup is always a pain for me, especially after JDK8, you need to download JavaFX SDK separately.

I create this JavaFX starter project to help myself and people who want to start working with JavaFX without going through the pain of setting up.

You can download the code here on github:

Let’s get started.

Step 1: Download JavaFX SDK

Head to this site and download JavaFX SDK 11. Save it somewhere on you computer. You will need it later.

Step 2: Clone the starter project

Clone the starter project here if you haven’t

I assume that you use IntelliJ. Click on File->Project Structure->Libraries and then the (+) button:

Add javafx library to the project

Select Java and navigate the /lib folder inside the JavaFX SDK you have just downloaded.

select the bin folder

Click OK twice and you are ready.

Step 3: Run the application in the IDE

As you can see, we have two classes with the main function in the starter project. The one inside Main is created by the IDE. If you run the main function in this class, you may face some weird problems. The reason is given here.

So, instead of running Main, we run Launcher and your application can start just fine.

Step 4: Package the application as a Jar file

You can package and distribute the application with just a few clicks:

Go to Project Structure->Artifacts, click on the (+) icon:

Package javafx starter project as jar

On Main class selection, make sure you select Launcher, not Main:

To build the artifact, click on Build->Build artifact

Your fat jar file should be inside the out folder.

So, that’s all you need to do to create and run a JavaFX application. Using this starter project, you don’t have to deal with problems that can easily cost you hours to solve (Location is required!, for example).