Example usage for org.hibernate.cfg Configuration buildSessionFactory

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

Introduction

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

Prototype

public SessionFactory buildSessionFactory() throws HibernateException 

Source Link

Document

Create a SessionFactory using the properties and mappings in this configuration.

Usage

From source file:MainVehicle.java

public static void main(String h[]) {
    Vehicle v1 = new Vehicle();
    v1.setVid(1);/* ww w. ja va  2  s  . c  om*/
    v1.setVname("some vehicle");

    TwoWheeler tw = new TwoWheeler();
    tw.setVid(12);
    tw.setVname("bike");
    tw.setHandleBar("bike handle bar");

    FourWheeler fw = new FourWheeler();
    fw.setVid(13);
    fw.setVname("car");
    fw.setSteeringWheel("car's streeing wheel");

    Configuration conf = new Configuration();
    conf.configure("hibernate.cfg.xml");
    SessionFactory factory = conf.buildSessionFactory();
    Session sess = factory.openSession();
    Transaction trx = sess.beginTransaction();
    sess.save(v1);
    sess.save(tw);
    sess.save(fw);

    trx.commit();
    sess.close();

}

From source file:Bazica.java

public Bazica(HttpServletResponse response) throws IOException {
    PrintWriter out = response.getWriter();
    this.out = out;
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    this.session = session;
}

From source file:HibernateCoursework.java

public HibernateCoursework() {
    Configuration configuration = h2Config(new Class[] { Customer.class, Project.class, Employee.class });
    sessionFactory = configuration.buildSessionFactory();
}

From source file:Home.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);/*from  w w w  .j  a v a  2 s . co  m*/

    System.out.println("run");

    //configureer connectie
    Configuration cfg = new Configuration();
    cfg.configure("Hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();
    Transaction tx = s.beginTransaction();

    //maak een employee 
    Scoutslid scouts = new Scoutslid();
    scouts.setNaam(request.getParameter("Naam"));
    scouts.setScoutsnaam(request.getParameter("Scoutsnaam"));
    scouts.setEmailadres(request.getParameter("Emailadres"));

    //bereid voor om weg te schrijven naar db
    s.save(scouts);
    s.flush();

    //schrijf weg
    tx.commit();

    //sluit connectie
    s.close();

    //haal de employees op
    //open een sessie
    s = sf.openSession();

    //Haal alle scoutsleden op
    List scoutsleden = s.createCriteria(Scoutslid.class).list();
    ArrayList<Scoutslid> aScout = new ArrayList();
    for (Iterator it1 = scoutsleden.iterator(); it1.hasNext();) {
        Scoutslid Sc = (Scoutslid) it1.next();
        aScout.add(Sc);

    }

    request.getSession().setAttribute("Scoutslid", aScout);

    //sluit de connectie
    s.close();

    request.getRequestDispatcher("results.jsp").forward(request, response);

}

From source file:abstractDao.HibernateFactory.java

/**
 *
 * @return @throws HibernateException/*ww w. j  a  v  a2 s .  c  o m*/
 */
private static SessionFactory configureSessionFactory() throws HibernateException {
    Configuration configuration = new AnnotationConfiguration();
    configuration.configure();
    sessionFactory = configuration.buildSessionFactory();
    return sessionFactory;
}

From source file:backend.core.controller.ProductsController.java

public static SessionFactory getInstance() {

    if (sf == null) {

        Configuration cfg = new Configuration();
        cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        cfg.setProperty("hibernate.connection.url", "jdbc:mysql://db4free.net:3306/iomarket");
        cfg.setProperty("hibernate.connection.username", "iomarket");
        cfg.setProperty("hibernate.connection.password", "iomarket123");
        cfg.setProperty("hibernate.hbm2ddl.auto", "update");

        cfg.setProperty("show_sql", "true");

        cfg.addResource("Products.hbm.xml");
        cfg.addResource("Suppliers.hbm.xml");
        cfg.addResource("Attributes.hbm.xml");
        cfg.addResource("AttrValues.hbm.xml");
        cfg.addResource("Category.hbm.xml");
        cfg.addResource("Users.hbm.xml");
        cfg.addResource("Orders.hbm.xml");
        cfg.addResource("Groups.hbm.xml");
        cfg.addResource("Monitoring.hbm.xml");
        cfg.addResource("MonitoringWorkers.hbm.xml");

        sf = cfg.buildSessionFactory();
    }/*from ww  w  .j a va2  s  . c  o  m*/

    return sf;
}

From source file:backend.core.controller.UserController.java

public static SessionFactory getInstance() {

    if (sf == null) {

        Configuration cfg = new Configuration();
        cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        cfg.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/io");
        cfg.setProperty("hibernate.connection.username", "root");
        cfg.setProperty("hibernate.connection.password", "");
        cfg.setProperty("hibernate.hbm2ddl.auto", "update");

        cfg.setProperty("show_sql", "true");

        cfg.addResource("Groups.hbm.xml");
        cfg.addResource("Users.hbm.xml");

        sf = cfg.buildSessionFactory();
    }//from   w  w  w . j  a va  2  s . com

    return sf;
}

From source file:bd2.Test.Main.java

protected static Session getSession() {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    return session;
}

From source file:be.shad.tsqb.test.TypeSafeQueryTest.java

License:Apache License

/**
 * Initialize the sessionFactory and helper once.
 * The helper has an override to generate shorter entity names
 * for readability (and it also works in hibernate...)
 *//*from   w  ww.java2  s  .c om*/
@BeforeClass
public static void initializeClass() {
    if (sessionFactory == null) {
        Configuration config = new Configuration();
        config.configure("be/shad/tsqb/tests/hibernate.cfg.xml");
        sessionFactory = config.buildSessionFactory();
        helper = new TypeSafeQueryHelperImpl(sessionFactory) {
            // trim package for readability:
            @Override
            public String getEntityName(Class<?> entityClass) {
                String entityName = super.getEntityName(entityClass);
                return entityName.substring(entityName.lastIndexOf(".") + 1);
            }
        };
        typeSafeQueryDao = new TypeSafeQueryDaoImpl(sessionFactory, helper);
    }
}

From source file:beans.Driver.java

public static void main(String[] args) {
    Student s = new Student();
    s.setId(4);//from  w  ww  . ja  v  a 2s  .  c om
    s.setName("Amjad");

    try {

        Configuration cf = new Configuration();
        cf.configure("xml/hibernate.cfg.xml");
        SessionFactory sf = cf.buildSessionFactory();
        Session ses = sf.openSession();

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

        ses.close();
        //                
        // ses = sf.openSession();

        // s = (Student)ses.get(Student.class, 1);
        // System.out.println("Name="+s.getName());
        //                System.out.println("Id="+s.getId());
    } catch (Exception e) {
        e.printStackTrace();
    }
}