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:com.wontheone.hiber03.HibernateUtil.java

private HibernateUtil() {
    try {//from   w w  w .ja v a2  s  . co  m
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        Configuration config = new Configuration();
        config.addAnnotatedClass(com.wontheone.hiber03.Car.class);
        config.addAnnotatedClass(com.wontheone.hiber03.Person.class);
        config.addAnnotatedClass(com.wontheone.hiber03.Student.class);
        config.addAnnotatedClass(com.wontheone.hiber03.UASstudent.class);
        config.addAnnotatedClass(com.wontheone.hiber03.FlyingCar.class);
        config.addAnnotatedClass(com.wontheone.hiber03.JetCar.class);
        config.addAnnotatedClass(com.wontheone.hiber03.Animal.class);
        config.addAnnotatedClass(com.wontheone.hiber03.Mammal.class);
        config.addAnnotatedClass(com.wontheone.hiber03.Dog.class);
        config.configure("hibernate.cfg.xml");
        new SchemaExport(config).create(true, true);
        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
        serviceRegistryBuilder.applySettings(config.getProperties());
        ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
        sessionFactory = config.buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.yahoo.elide.datastores.hibernate3.HibernateDataStoreSupplier.java

License:Apache License

@Override
public DataStore get() {
    // Add additional checks to our static check mappings map.
    // NOTE: This is a bit hacky. We need to do a major overhaul on our test architecture
    TestCheckMappings.MAPPINGS.put("filterCheck", Filtered.FilterCheck.class);
    TestCheckMappings.MAPPINGS.put("filterCheck3", Filtered.FilterCheck3.class);

    // method to force class initialization
    Configuration configuration = new Configuration();
    try {//from w ww.j  av a2 s . c o m
        ClassScanner.getAnnotatedClasses(Parent.class.getPackage(), Entity.class)
                .forEach(configuration::addAnnotatedClass);
    } catch (MappingException e) {
        throw new RuntimeException(e);
    }

    SessionFactory sessionFactory = configuration.configure("hibernate.cfg.xml")
            .setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread")
            .setProperty(Environment.URL,
                    "jdbc:mysql://localhost:" + System.getProperty("mysql.port", "3306")
                            + "/root?serverTimezone=UTC")
            .setProperty(Environment.USER, "root").setProperty(Environment.PASS, "root").buildSessionFactory();

    // create example tables from beans
    SchemaExport schemaExport = new SchemaExport(configuration).setHaltOnError(true);
    schemaExport.drop(false, true);
    schemaExport.execute(false, true, false, true);

    if (!schemaExport.getExceptions().isEmpty()) {
        throw new RuntimeException(schemaExport.getExceptions().toString());
    }

    return new HibernateStore(sessionFactory, true, ScrollMode.FORWARD_ONLY);
}

From source file:com.yahoo.elide.hibernate.AHibernateTest.java

License:Apache License

protected static void databaseManagerInit() {
    // method to force class initialization
    Configuration c = new Configuration();
    try {//from   w w  w .  ja  v  a  2 s  .c  o  m
        ClassScanner.getAnnotatedClasses(Parent.class.getPackage(), Entity.class).forEach(c::addAnnotatedClass);
    } catch (MappingException e) {
        throw new RuntimeException(e);
    }
    sessionFactory = c.configure("hibernate.cfg.xml")
            .setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread")
            .setProperty(Environment.URL,
                    "jdbc:mysql://localhost:" + System.getProperty("mysql.port", "3306") + "/root")
            .setProperty(Environment.USER, "root").setProperty(Environment.PASS, "root").buildSessionFactory();

    // create Example tables from beans
    SchemaExport se = new SchemaExport(c).setHaltOnError(true);
    se.drop(false, true);
    se.execute(false, true, false, true);

    if (se.getExceptions().size() != 0) {
        throw new RuntimeException("" + se.getExceptions());
    }

    hibernateManager = new HibernateManager(sessionFactory);
}

From source file:com.zin.PojoBean.java

public void add() {
    PojoBean p = new PojoBean();
    p.setId(id);// w  w  w .jav  a 2s  .com
    p.setAge(age);
    p.setCity(city);
    p.setName(name);

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

    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();
    Transaction t = s.beginTransaction();

    s.saveOrUpdate(p);
    t.commit();
    System.out.println("Record Inserted");
}

From source file:compecon.engine.util.HibernateUtil.java

License:Open Source License

private static SessionFactory buildSessionFactory() {
    if (HibernateUtil.isActive()) {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        SessionFactory sessionFactory = configuration
                .buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
        return sessionFactory;
    }/*from  ww w . j av  a2 s .  c om*/
    return null;
}

From source file:compositekey.savedata.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    /* TODO output your page here. You may use following sample code. */

    Configuration con = new Configuration();
    con.configure("hibernate.cfg.xml");
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(con.getProperties()).build();
    SessionFactory sf = con.buildSessionFactory(serviceRegistry);
    Session s = sf.openSession();/*w w  w . ja  va2 s  . c om*/

    Transaction tr = s.beginTransaction();

    Compositetesting com = new Compositetesting();

    com.getId().setFname("ishwar");
    com.getId().setName("Rohit");
    com.setId_1(10);
    com.setSalary(100);
    s.save(com);
    tr.commit();

}

From source file:controller.ContextLoader.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServletContext sc = sce.getServletContext();
    try {//from  ww w  .  j ava  2  s. c o  m

        System.out.println("Listiner is called");
        Configuration cf = new Configuration();
        cf.configure("xmlFiles/hibernate.cfg.xml");
        SessionFactory sf = cf.buildSessionFactory();
        sc.setAttribute("sessionFactory", sf);
        sc.setAttribute("hibernateSession", sf.openSession());
    } catch (Exception e) {
        System.out.println("Exception in ContextListener " + e.getMessage());
        e.printStackTrace();
    }
    //To change body of generated methods, choose Tools | Templates.
}

From source file:controller.DriverListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {

    ServletContext sc = sce.getServletContext();
    try {/* ww  w  .  jav  a 2  s  .  c o  m*/

        Configuration cf = new Configuration();
        cf.configure("xmlFiles/hibernate.cfg.xml");
        SessionFactory sf = cf.buildSessionFactory();
        sc.setAttribute("sessionFactory", sf);

    } catch (Exception e) {

    }
}

From source file:controller.InsertAttendance.java

public static void main(String[] args) {
    //configure cfg xml file
    Configuration cf = new Configuration();
    cf.configure("xmlFiles/hibernate.cfg.xml");

    //build session factory
    SessionFactory sf = cf.buildSessionFactory();
    //get session object
    Session session = sf.openSession();/* ww  w .  j a  va 2 s  .  com*/
    //get Transaction object
    Transaction tr = session.beginTransaction();

    //STUDENT ATTANDENCE
    StudentAttendance studentAttendance = new StudentAttendance();

    studentAttendance.setAttendance("P");
    studentAttendance.setLectureDate("20/7/2013");
    studentAttendance.setRollNum("13_CS_19");
    studentAttendance.setTheoryOrPractical("practical");
    studentAttendance.setSubject("BEE");
    studentAttendance.setSemester("2nd");
    studentAttendance.setBatch("13");
    studentAttendance.setDepart("Computer System");
    studentAttendance.setSemesterState("no");

    //INSERT ATTENDANCE OBJECT
    session.save(studentAttendance);
    tr.commit();
    session.evict(studentAttendance);

    //close session and session factory
    session.close();
    sf.close();

}

From source file:controller.InsertSubjects.java

public static void main(String[] args) {
    //configure cfg xml file
    Configuration cf = new Configuration();
    cf.configure("xmlFiles/hibernate.cfg.xml");

    //build session factory
    SessionFactory sf = cf.buildSessionFactory();
    //get session object
    Session session = sf.openSession();//from ww w.  j  a va2 s.co  m
    //get Transaction object
    Transaction tr = session.beginTransaction();

    //SUBJECTS OF CS
    //          Subjects subjects = new Subjects();
    //          subjects.setDepartment("comptuer system");
    //          subjects.setSemester("1st");
    //          subjects.setSubjectName("Calculus");
    //        //INSERT ATTENDANCE OBJECT
    //        session.save(subjects);
    //        tr.commit();
    //        session.evict(subjects);
    //close session and session factory
    session.close();
    sf.close();

}