Example usage for org.hibernate.cfg Configuration Configuration

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

Introduction

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

Prototype

public Configuration() 

Source Link

Usage

From source file:com.GoSafe.Controller.LoadMapController.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   www . j ava 2 s  . co  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        response.setContentType("text/html;charset=UTF-8");
        Integer city_id = Integer.valueOf(request.getParameter("City"));
        Integer area_id = Integer.valueOf(request.getParameter("Area"));
        Integer locality_id = Integer.valueOf(request.getParameter("Locality"));
        //System.out.println("Anuja " + city_id +" " + area_id + " " + locality_id);

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Locality locality;
        if (locality_id != null && locality_id > 0) {
            locality = (Locality) session.get(Locality.class, locality_id);
            System.out.println("Anuja values are id = " + locality.getId() + " latitude="
                    + locality.getLatitude() + " locality= " + locality.getLongitude() + " comments= "
                    + locality.getComments() + " severity= " + locality.getSeverity() + "area id = "
                    + locality.getArea_id() + " city id= " + locality.getCity_id());
            request.setAttribute("latitude", locality.getLatitude());
            request.setAttribute("longitude", locality.getLongitude());
            request.setAttribute("severity", locality.getSeverity());
            request.setAttribute("comments", locality.getComments());
        } else {

        }

        RequestDispatcher reqDispatcher = request.getRequestDispatcher("Map.jsp");

        session.close();
        reqDispatcher.forward(request, response);
    }
}

From source file:com.GoSafe.Controller.LoadTableController.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w  w  w  .  j  a  va 2  s.  co  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        //Create and insert values to City Table 
        Query query = session.createQuery("select count(*) from City");
        Long city_count = (Long) query.uniqueResult();
        if (city_count == null || city_count == 0) {
            City city = new City();
            city.setId(1);
            city.setName("Chennai");
            session.save(city);
            City city1 = new City();
            city1.setId(2);
            city1.setName("New Delhi");
            session.save(city1);
        }

        //Create and insert values to Area Table
        query = session.createQuery("select count(*) from Area");
        Long area_count = (Long) query.uniqueResult();
        if (area_count == null || area_count == 0) {
            Area area = new Area();
            area.setId(1);
            area.setCity_id(1);
            area.setName("Guindy");
            area.setLatitude(13.006708);
            area.setLongitude(80.2197411);
            session.save(area);

            Area area1 = new Area();
            area1.setId(2);
            area1.setCity_id(1);
            area1.setName("Thiruvanmiyur");
            area1.setLatitude(12.9837304);
            area1.setLongitude(80.2606625);
            session.save(area1);

            Area area2 = new Area();
            area2.setId(3);
            area2.setCity_id(2);
            area2.setName("Sarojini Nagar");
            area2.setLatitude(28.5755005);
            area2.setLongitude(77.197573);
            session.save(area2);

            Area area3 = new Area();
            area3.setId(4);
            area3.setCity_id(2);
            area3.setName("Chanakyapuri");
            area3.setLatitude(28.595374);
            area3.setLongitude(77.1844946);
            session.save(area3);
        }

        //Create and insert values to Locality Table
        query = session.createQuery("select count(*) from Locality");
        Long locality_count = (Long) query.uniqueResult();
        if (locality_count == null || locality_count == 0) {

            Locality loc1 = new Locality();
            loc1.setId(1);
            loc1.setName("Bus Stand");
            loc1.setArea_id(1);
            loc1.setCity_id(1);
            loc1.setLatitude(13.008044);
            loc1.setLongitude(80.2092531);
            loc1.setComments("Crowed, but chain snatching and Pick Pocket complaints are very frequent");
            loc1.setPopular("False");
            loc1.setSeverity(3);
            session.save(loc1);

            Locality loc2 = new Locality();
            loc2.setId(2);
            loc2.setName("Railway Station");
            loc2.setArea_id(1);
            loc2.setCity_id(1);
            loc2.setLatitude(13.008357);
            loc2.setLongitude(80.212318);
            loc2.setComments("This place is safe to go");
            loc2.setPopular("False");
            loc2.setSeverity(1);
            session.save(loc2);

            Locality loc3 = new Locality();
            loc3.setId(3);
            loc3.setName("Bus Stand");
            loc3.setArea_id(2);
            loc3.setCity_id(1);
            loc3.setLatitude(12.9869735);
            loc3.setLongitude(80.2595622);
            loc3.setComments("Pick Pocket complaints are frequent during mid-nnon and late night timings.");
            loc3.setPopular("False");
            loc3.setSeverity(2);
            session.save(loc3);

            Locality loc4 = new Locality();
            loc4.setId(4);
            loc4.setName("Railway Station");
            loc4.setArea_id(2);
            loc4.setCity_id(1);
            loc4.setLatitude(12.989678);
            loc4.setLongitude(80.251386);
            loc4.setComments("This place is safe to go");
            loc4.setPopular("False");
            loc4.setSeverity(1);
            session.save(loc4);

            Locality loc5 = new Locality();
            loc5.setId(5);
            loc5.setName("Market");
            loc5.setArea_id(3);
            loc5.setCity_id(2);
            loc5.setLatitude(28.576907);
            loc5.setLongitude(77.196263);
            loc5.setComments("Harassement complaints have been registered behind Keshav Park");
            loc5.setPopular("False");
            loc5.setSeverity(4);
            session.save(loc5);

            Locality loc6 = new Locality();
            loc6.setId(6);
            loc6.setName("Railway Station");
            loc6.setArea_id(3);
            loc6.setCity_id(2);
            loc6.setLatitude(28.581112);
            loc6.setLongitude(77.196232);
            loc6.setComments("This place is safe to go");
            loc6.setPopular("False");
            loc6.setSeverity(1);
            session.save(loc6);

            Locality loc7 = new Locality();
            loc7.setId(7);
            loc7.setName("Railway Station");
            loc7.setArea_id(4);
            loc7.setCity_id(2);
            loc7.setLatitude(28.591632);
            loc7.setLongitude(77.171747);
            loc7.setComments("This place is safe to go");
            loc7.setPopular("False");
            loc7.setSeverity(1);
            session.save(loc7);

            Locality loc8 = new Locality();
            loc8.setId(8);
            loc8.setName("Yashwant Place Mall");
            loc8.setArea_id(4);
            loc8.setCity_id(2);
            loc8.setLatitude(28.584277);
            loc8.setLongitude(77.191862);
            loc8.setComments(
                    "Robbery and Harassement complaints have been registered near China Sizzlers town");
            loc8.setPopular("False");
            loc8.setSeverity(4);
            session.save(loc8);
        }
        session.getTransaction().commit();
        session.close();

        RequestDispatcher reqDispatcher = request.getRequestDispatcher("Home.html");
        reqDispatcher.forward(request, response);

        /* RequestDispatcher reqDispatcher = request.getRequestDispatcher("Home.jsp");
         Query query1 = session.createQuery("from city");
         List<City> cities = query1.list();
         request.setAttribute("cities", cities);*/
    }
}

From source file:com.grand.ids.audit.db.HibernateUtils.java

License:Apache License

public static SessionFactory createSessionFactory(String connectionUrl, String username, String password,
        String driverClassName, String dialect) throws HibernateException {
    SessionFactory factory = new Configuration().setProperty("hibernate.dialect", dialect)
            .setProperty("hibernate.connection.driver_class", driverClassName)
            .setProperty("hibernate.connection.url", connectionUrl)
            .setProperty("hibernate.connection.username", username)
            .setProperty("hibernate.connection.password", password)
            .setProperty("hibernate.hbm2ddl.auto", "update")
            //.setProperty("hibernate.show_sql", "true")
            .addPackage("com.grand.ids.audit.db.domain").addAnnotatedClass(Event.class).buildSessionFactory();

    return factory;
}

From source file:com.grand.ids.jdbc.knowledgemodule.id.IdKnowledgeModule.java

License:Apache License

private SessionFactory createSessionFactory(String connectionUrl, String username, String password,
        String driverClassName, String dialect, boolean cleanDatabase) throws HibernateException {
    SessionFactory factory = new Configuration()//
            .setProperty("hibernate.dialect", dialect)//
            .setProperty("hibernate.connection.driver_class", driverClassName)//
            .setProperty("hibernate.connection.url", connectionUrl)//
            .setProperty("hibernate.connection.username", username)//
            .setProperty("hibernate.connection.password", password)//
            .setProperty("hibernate.hbm2ddl.auto", cleanDatabase ? "create" : "update")//
            //.setProperty("hibernate.show_sql", "true")//
            .addPackage("com.grand.ids.jdbc.knowledgemodule.id.")//
            .addAnnotatedClass(TableInfo.class)//
            .addAnnotatedClass(RecordInfo.class)//
            .buildSessionFactory();//  w w w  . ja v a 2s.  c om
    return factory;
}

From source file:com.hazelcast.hibernate.HibernateTestSupport.java

License:Open Source License

protected SessionFactory createSessionFactory(Properties props, IHazelcastInstanceLoader customInstanceLoader) {
    Configuration conf = new Configuration();
    URL xml = HibernateTestSupport.class.getClassLoader().getResource("test-hibernate.cfg.xml");
    props.put(CacheEnvironment.EXPLICIT_VERSION_CHECK, "true");
    if (customInstanceLoader != null) {
        props.put("com.hazelcast.hibernate.instance.loader", customInstanceLoader);
        customInstanceLoader.configure(props);
    } else {//from   ww  w. ja  va 2  s  .com
        props.remove("com.hazelcast.hibernate.instance.loader");
    }
    conf.configure(xml);
    conf.setCacheConcurrencyStrategy(DummyEntity.class.getName(), getCacheStrategy());
    conf.setCacheConcurrencyStrategy(DummyProperty.class.getName(), getCacheStrategy());
    conf.setCollectionCacheConcurrencyStrategy(DummyEntity.class.getName() + ".properties", getCacheStrategy());
    conf.addProperties(props);
    final SessionFactory sf = conf.buildSessionFactory();
    sf.getStatistics().setStatisticsEnabled(true);
    return sf;
}

From source file:com.hibernate.app.Program.java

/**
 * @param args the command line arguments
 */// w w w  .j  a v  a2  s .c o m
public static void main(String[] args) {
    Configuration configuration = new Configuration().configure("resources/hibernate.cfg.xml");
    StandardServiceRegistryBuilder registry = new StandardServiceRegistryBuilder();
    registry.applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = registry.build();
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    Session session = sessionFactory.openSession();
    //Transaction trans=session.beginTransaction();
    //session.save(new Discount(0,"50% OFF",50,null,true));
    //trans.commit();
    List<Discount> discounts = session.getNamedQuery("Discount.findAll").list();
    discounts.forEach(d -> {
        System.out.println(d.getDiscountTitle());
    });
    session.close();
    sessionFactory.close();

    System.out.println("Finish");
    System.exit(0);
}

From source file:com.hibernate.facturation2.control.DaoControl.java

public static SessionFactory sfCreation() {
    Configuration config = new Configuration().configure("hibernate.cfg.xml");
    ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(config.getProperties())
            .build();//from w ww.  j  ava 2  s. co  m
    SessionFactory sf = config.buildSessionFactory(registry);

    return sf;
}

From source file:com.hibernateGenericDAO.HibernateUtil.java

License:Open Source License

/**
 * Creates the db schema/* w w  w  .j  a va  2  s.  co m*/
 */
@SuppressWarnings("unused")
private static void createSchema() {
    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");
    new SchemaExport(configuration).create(true, true);
}

From source file:com.hibernateGenericDAO.HibernateUtil.java

License:Open Source License

/**
 * Creates a signle session factory//from   w w  w .j a v a 2  s  .c  om
 * @return SessionFactory
 */
private static synchronized final SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }
    return sessionFactory;
}

From source file:com.hibernateutil.org.ConnectionUtil.java

private static SessionFactory buildSessionFactory() {
    try {//from www .ja  va2 s. com
        conf = new Configuration();
        conf.configure("hibernate.cfg.xml");
        System.out.println("Configurao carregada com sucesso!");
        factory = conf.buildSessionFactory();
        System.out.println("SessionFactory carregada com sucesso!");
        return factory;

    } catch (Throwable e) {
        System.err.println("Falha na criao da SessionFactory ");
        e.printStackTrace();
        throw new ExceptionInInitializerError(e);
    }
}