Example usage for org.hibernate.cfg Configuration configure

List of usage examples for org.hibernate.cfg Configuration configure

Introduction

In this page you can find the example usage for org.hibernate.cfg Configuration configure.

Prototype

@Deprecated
public Configuration configure(org.w3c.dom.Document document) throws HibernateException 

Source Link

Usage

From source file:MainPrincipal.Main.java

public static boolean inicialitzat() {
    Configuration config = new Configuration();
    config.configure("hibernate.cfg.xml");
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(config.getProperties()).build();
    SessionFactory factory = config.buildSessionFactory(serviceRegistry);
    Session session = factory.openSession();

    Query q = session.createQuery("Select Count(*) From Espectacle");
    int numero = ((Number) q.uniqueResult()).intValue();
    if (numero == 0)
        return false;
    else//from   w ww . j  a  v a  2 s  .  c om
        return true;

}

From source file:model.account.ManageAccount.java

public static void createAccount(Account user) {
    Configuration config = new Configuration();
    config.addAnnotatedClass(Account.class);
    config.configure("hibernate.cfg.xml");
    // Creates a table
    // new SchemaExport(config).create(true, true);
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.openSession();
    session.beginTransaction();/*from   ww  w  . j  a  va2 s  .c o  m*/
    session.save(user);
    session.getTransaction().commit();
    session.close();

}

From source file:model.account.ManageAccount.java

public static Account getAccountByEmail(String email) {
    Configuration config = new Configuration();
    config.addAnnotatedClass(Account.class);
    config.configure("hibernate.cfg.xml");

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.openSession();
    session.beginTransaction();/* www  .j  av a2s. co m*/
    Account user = null;
    String queryString = "from Account where EMAIL = :Email";
    Query query = session.createQuery(queryString);
    query.setString("Email", email);
    Object queryResult = query.uniqueResult();
    user = (Account) queryResult;
    return user;

}

From source file:model.classes.HibernateUtil.java

private static SessionFactory buildSessionFactory() {
    try {/*  w  ww. j ava  2  s .  com*/
        Configuration configuration = new Configuration();
        configuration.configure("model/xml/hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        // Create the SessionFactory from hibernate.cfg.xml
        //return new Configuration().configure("model/xml/hibernate.cfg.xml").buildSessionFactory(
        //       new StandardServiceRegistryBuilder().build() );
        return sessionFactory;
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:model.Data_Handler.java

private SessionFactory getSessionFactory() {
    try {//from   w w w  . ja  v  a 2  s.  co m
        final Configuration config = new Configuration();
        config.configure("mealsHibernate.cfg.xml");
        LOG.info("Connection to hibernate URL = " + config.getProperty("hibernate.connection.url"));
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(config.getProperties()).build();

        return config.buildSessionFactory(serviceRegistry);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return null;
    }
}

From source file:model.MainAuthor.java

public static void main(String h[]) {
    Author au = new Author();

    au.setAname("j.k.row");

    Book bk = new Book();
    bk.setBname("harry potter");
    Book bk2 = new Book();
    bk2.setBname("fire of princess");
    au.getSetOfBook().add(bk);//from  w ww  . j a va  2 s. c  o m
    au.getSetOfBook().add(bk2);
    bk.getSetOfAuthor().add(au);
    Configuration con = new Configuration();
    con.configure("hibernate.cfg.xml");
    SessionFactory fac = con.buildSessionFactory();
    Session sess = fac.openSession();
    Transaction tx = sess.beginTransaction();
    sess.save(au);
    sess.save(bk);
    sess.save(bk2);
    tx.commit();

}

From source file:model.MainPer5.java

public static void main(String h[]) {
    Person5 per = new Person5();
    per.setName("first person");

    Vehicle5 v = new Vehicle5();
    v.setVname("bike");
    Vehicle5 v2 = new Vehicle5();
    v2.setVname("car");
    per.getVeh().add(v);/*from   ww w  .j a  va 2 s.c om*/
    per.getVeh().add(v2);
    v.getSetOfPerson().add(per);
    v2.getSetOfPerson().add(per);
    Configuration con = new Configuration();
    con.configure("hibernate.cfg.xml");
    SessionFactory fac = con.buildSessionFactory();
    Session sec = fac.openSession();
    Transaction tx = sec.beginTransaction();
    sec.save(per);
    sec.save(v);
    sec.save(v2);
    tx.commit();
}

From source file:myrmidia.Database.Connector.java

License:Open Source License

/**
 * This mehtod is used to override a hard-coded parameter in the
 * hibernate.cfg.xml file to give a the application a limited altered
 * configuration. Used to permit hibernate to create non-existing tables.
 * @param file URL the url path to the hibernate.cfg.xml file
 * @throws InitializingException/*from www .  jav  a  2  s .com*/
 */
public void initOverwriteFromXMLFile(URL file) throws InitializingException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(file.openStream());

        String hcf = document.getElementsByTagName("HibernateConfigFile").item(0).getTextContent();

        String descriptionMapFile = document.getElementsByTagName("DescriptionMappingFile").item(0)
                .getTextContent();
        descriptionClassName = document.getElementsByTagName("DescriptionClassName").item(0).getTextContent();

        Configuration hbconfig = new Configuration();
        hbconfig.configure(FileIO.findFile(hcf));
        hbconfig.addURL(FileIO.findFile(descriptionMapFile));

        try {
            String solutionMapFile = document.getElementsByTagName("SolutionMappingFile").item(0)
                    .getTextContent();
            solutionClassName = document.getElementsByTagName("SolutionClassName").item(0).getTextContent();
            hbconfig.addResource(solutionMapFile);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).info("Case does not have solution");
        }

        try {
            String justOfSolutionMapFile = document.getElementsByTagName("JustificationOfSolutionMappingFile")
                    .item(0).getTextContent();
            justOfSolutionClassName = document.getElementsByTagName("JustificationOfSolutionClassName").item(0)
                    .getTextContent();
            hbconfig.addResource(justOfSolutionMapFile);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).info("Case does not have justification of the solution");
        }

        try {
            String resultMapFile = document.getElementsByTagName("ResultMappingFile").item(0).getTextContent();
            resultClassName = document.getElementsByTagName("ResultClassName").item(0).getTextContent();
            hbconfig.addResource(resultMapFile);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).info("Case does not have result");
        }
        hbconfig.setProperty("hibernate.hbm2ddl.auto", "update");
        String currentProperty = hbconfig.getProperty("hibernate.connection.url");
        currentProperty += ";create=true";
        hbconfig.setProperty("hibernate.connection.url", currentProperty);
        sessionFactory = hbconfig.buildSessionFactory();
    } catch (Throwable ex) {
        throw new InitializingException(ex);
    }
}

From source file:myrmidia.Database.Connector.java

License:Open Source License

@Override
public void initFromXMLfile(URL file) throws InitializingException {
    try {/*  ww w  .  java  2s. c o  m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(file.openStream());

        String hcf = document.getElementsByTagName("HibernateConfigFile").item(0).getTextContent();

        String descriptionMapFile = document.getElementsByTagName("DescriptionMappingFile").item(0)
                .getTextContent();
        descriptionClassName = document.getElementsByTagName("DescriptionClassName").item(0).getTextContent();

        Configuration hbconfig = new Configuration();
        hbconfig.configure(FileIO.findFile(hcf));
        hbconfig.addURL(FileIO.findFile(descriptionMapFile));

        try {
            String solutionMapFile = document.getElementsByTagName("SolutionMappingFile").item(0)
                    .getTextContent();
            solutionClassName = document.getElementsByTagName("SolutionClassName").item(0).getTextContent();
            hbconfig.addResource(solutionMapFile);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).info("Case does not have solution");
        }

        try {
            String justOfSolutionMapFile = document.getElementsByTagName("JustificationOfSolutionMappingFile")
                    .item(0).getTextContent();
            justOfSolutionClassName = document.getElementsByTagName("JustificationOfSolutionClassName").item(0)
                    .getTextContent();
            hbconfig.addResource(justOfSolutionMapFile);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).info("Case does not have justification of the solution");
        }

        try {
            String resultMapFile = document.getElementsByTagName("ResultMappingFile").item(0).getTextContent();
            resultClassName = document.getElementsByTagName("ResultClassName").item(0).getTextContent();
            hbconfig.addResource(resultMapFile);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).info("Case does not have result");
        }
        sessionFactory = hbconfig.buildSessionFactory();
    } catch (Throwable ex) {
        throw new InitializingException(ex);
    }
}

From source file:MyServlet.Driver.java

public static void main(String[] args) {
    Student s = new Student();

    s.setId(1);/*w w w.  j a  v a2s  .c  om*/
    s.setName("imtiaz");

    Configuration cf = new Configuration();
    cf.configure("controller/hibernate.cfg.xml");

    SessionFactory sf = cf.buildSessionFactory();
    Session ses = sf.openSession();

    ses.save(s);
    ses.beginTransaction().commit();
    ses.evict(s);
}