persistence.DBUtility.java Source code

Java tutorial

Introduction

Here is the source code for persistence.DBUtility.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.util.HashSet;
import java.util.Set;
import javafx.scene.paint.Color;
import model.LegacySystem;
import model.PMGroup;
import model.PMProcess;
import model.PMUser;
import model.Prozessschritt;
import model.Verantwortlicher;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.Target;

/**
 *
 * @author song
 */
public class DBUtility {

    public static void initializeUsers() {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();

        Transaction tx = null;

        try {
            tx = session.beginTransaction();
            //
            //            PMUser demoUser = new PMUser("song", "song");
            //            PMGroup defaultGroup = new PMGroup("default");
            //
            //            demoUser.getGroups().add(defaultGroup);
            //
            //            session.save(demoUser);
            //            
            Verantwortlicher v1 = new Verantwortlicher();
            v1.setName("Keiner");
            v1.setFarbcode(Utility.convertColorToColorString(Color.RED));

            LegacySystem s1 = new LegacySystem();
            s1.setName("SAP");

            Prozessschritt p1 = new Prozessschritt("P1", v1);
            p1.getSysteme().add(s1);

            session.save(p1);

            session.flush();
            tx.commit();

        } catch (HibernateException he) {
            if (tx != null) {
                tx.rollback();
                he.printStackTrace();
            } else {
                he.printStackTrace();
                throw he;
            }
        } finally {
            try {
                session.close();
            } catch (HibernateException he) {
                he.printStackTrace();
            }
        }

        sessionFactory.close();
    }

    public static void initializeDatabase() {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();

        PMUser demoUser = new PMUser("song", "song");
        PMGroup defaultGroup = new PMGroup("default");

        demoUser.getGroups().add(defaultGroup);

        session.save(demoUser);

        Query query = session.createQuery("from PMUser where name='song'");
        PMUser user = (PMUser) query.uniqueResult();

        try {

            session.beginTransaction();

            Verantwortlicher v1 = new Verantwortlicher();
            v1.setName("Keiner");
            v1.setFarbcode(Utility.convertColorToColorString(Color.RED));

            PMProcess process = new PMProcess();
            process.setUser(user);
            process.setGroup((PMGroup) user.getGroups().toArray()[0]);
            process.setName("Einkaufsprozess");
            process.setCustomer("Vientiane Corp");
            process.setDescription(
                    "Das ist der Einkaufsprozess der Firma Vientiane Corp.\nEin wirklich sehr komplizierter Prozess.");
            session.save(process);

            Prozessschritt p1 = new Prozessschritt("Start", v1);
            p1.setProcess(process);
            session.save(p1);

            session.getTransaction().commit();

        } catch (HibernateException he) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
                he.printStackTrace();
                System.exit(0);
            } else {
                he.printStackTrace();
                System.exit(0);
            }
        } finally {
            session.close();
        }

    }

    public static void schemaExport() {
        Configuration config = new Configuration().configure("hibernate.cfg.xml");
        SchemaExport exporter = new SchemaExport(config);
        exporter.setDelimiter(";");
        exporter.setOutputFile("pentamatrix-2.sql");
        exporter.execute(Target.EXPORT, SchemaExport.Type.BOTH);
    }

    public static void main(String[] args) {
        schemaExport();
        //initializeUsers();
        //initializeDatabase();
    }

}