view flys-backend/src/main/java/de/intevation/flys/App.java @ 169:7929f4144d2f

Bound Apache Commons DBCP with Hibernate. flys-backend/trunk@1459 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 11 Mar 2011 16:23:42 +0000
parents 86a1bd9cc50e
children 249390dd24e7
line wrap: on
line source
package de.intevation.flys;

import org.hibernate.cfg.Configuration;

import org.hibernate.SessionFactory;

import de.intevation.flys.model.River;

import org.hibernate.dialect.resolver.DialectFactory;

import java.util.Properties;

import org.hibernate.cfg.Environment;

import org.hibernate.Session;

public class App 
{
    private static final String USER =
        System.getProperty("flys.user", "flys");

    private static final String PASS =
        System.getProperty("flys.pass", "flys");

    public static void main(String [] args)
    throws Exception
    {
        Configuration cfg = new Configuration();

        cfg.addAnnotatedClass(River.class);

        String [] setupScript = cfg.generateSchemaCreationScript(
            DialectFactory.constructDialect(
                "org.hibernate.dialect.PostgreSQLDialect"));

        for (String line: setupScript) {
            System.out.println(line);
        }

        Properties props = new Properties();

        props.setProperty(
            Environment.DIALECT,
            "org.hibernate.dialect.PostgreSQLDialect");

        props.setProperty(
            "hibernate.connection.provider_class",
            "org.hibernate.connection.DBCPConnectionProvider");

        props.setProperty(
            Environment.USER,
            USER);

        props.setProperty(
            Environment.PASS,
            PASS);

        props.setProperty(
            Environment.DRIVER,
            "org.postgresql.Driver");

        props.setProperty(
            Environment.URL,
            "jdbc:postgresql://localhost:54321/flystest1");

        cfg.mergeProperties(props);

        SessionFactory sessionFactory = cfg.buildSessionFactory();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        River river = new River("Hase-" + new java.util.Date());

        session.save(river);

        System.out.println("river id: " + river.getId());

        session.getTransaction().commit();
        session.close();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org