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:MainVehicle.java

public static void main(String h[]) {
    Vehicle v1 = new Vehicle();
    v1.setVid(1);//from  w  w  w  . j a  va2s  .  c o  m
    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:TestPurchase.java

@Test
public void testSave() {
    Purchase entity = new Purchase();
    entity.setId(0);// w  w w. ja v  a  2  s  .  co  m
    entity.setStatus(PurchaseStatus.W);

    PurchaseItemService sitem = new PurchaseItemService();
    sitem.setId(0);
    Service service = new Service();
    service.setId(1);
    sitem.setService(service);
    sitem.setFlagVat(FlagVat.I);
    entity.getServices().add(sitem);
    sitem = new PurchaseItemService();
    sitem.setId(0);
    service = new Service();
    service.setId(2);
    sitem.setService(service);
    sitem.setFlagVat(FlagVat.E);
    entity.getServices().add(sitem);
    PurchaseItemProduct pitem = new PurchaseItemProduct();
    pitem.setId(0);
    Product p = new Product();
    p.setId(1);
    pitem.setProduct(p);
    pitem.setFlagVat(FlagVat.I);
    entity.getProducts().add(pitem);

    pitem = new PurchaseItemProduct();
    pitem.setId(0);
    p = new Product();
    p.setId(2);
    pitem.setProduct(p);
    pitem.setFlagVat(FlagVat.I);
    entity.getProducts().add(pitem);
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Purchase data = (Purchase) session.get(Purchase.class, entity.getId());
    Purchase result = (Purchase) session.merge(entity);
    tran.commit();
}

From source file:PersistenceTest.java

/**
 * Operaciones que se realizan antes de ejecutar el banco de pruebas.
 * En este caso se crea una misma sesin que ser usada en todas las
 * pruebas.//  www  .j av a  2 s .c o m
 */
@Before
public void setupSession() {

    Configuration configuration = new Configuration();
    configuration.configure("hibernate-inmemory.cfg.xml");
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
            .buildServiceRegistry();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    session = sessionFactory.openSession();

}

From source file:TestEmployee.java

@Test
public void saveEmployee() {
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Employee entity = new Employee();
    entity.setId(0);/* w  w w. j a  va  2  s  .  c om*/
    Education item = new Education();
    item.setId(0);
    item.setEmployeeId(null);
    item.setEducationLevel(null);
    entity.getEducations().add(item);
    Employee data = (Employee) session.get(Employee.class, entity.getId());
    Employee result = (Employee) session.merge(entity);
    System.out.println(result.getId());
    tran.commit();

    //Employee data =(Employee) session.get(Employee.class, 0);
    //Employee employee = (Employee) session.get(Employee.class, 3);
    //System.out.println(employee.getId());

}

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:TestCustomer.java

@Test
public void saveCustomer() {
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Customer entity = new Customer();
    entity.setId(0);//ww w  .j a  v  a2 s  .co m
    entity.setCustomerType(CustomerType.P);
    Customer data = (Customer) session.get(Customer.class, entity.getId());
    Customer result = (Customer) session.merge(entity);
    tran.commit();
    System.out.print(result.getId());
}

From source file:PruebasConsultaUno.java

/**
 * Operaciones que se realizan antes de ejecutar el banco de pruebas.
 * En este caso se crea una misma sesin que ser usada en todas las
 * pruebas./*  w w  w . j a  v  a  2 s .  c  o  m*/
 */
@Before
public void setupSession() {

    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
            .buildServiceRegistry();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    session = sessionFactory.openSession();

}

From source file:Home.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);/* w ww.  j a  va2s  .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:a.A.java

/**
 * @param args the command line arguments
 *//*from  w  w  w.  j  a v a2s.c  o  m*/
public static void main(String[] args) {
    // TODO code application logic here
    String URL = "jdbc:mysql://localhost/MyBD";
    String USER = "root";
    String PASSWORD = "root";
    String DRIVER_CLASS = "com.mysql.jdbc.Driver";

    try {

        Class.forName(DRIVER_CLASS);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    Connection connection = null;
    try {

        connection = DriverManager.getConnection(URL, USER, PASSWORD);
    } catch (SQLException e) {
        System.out.println("ERROR: Unable to Connect to Database.");
    }

    Configuration cfg = new Configuration();
    cfg.configure("a/hibernate.cfg.xml");//populates the data of the configuration file  
    //creating seession factory object  
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties())
            .build();
    SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);

    String line;
    String csv = "C:\\Users\\hp\\Desktop\\GeoLiteCity-Location.csv";
    try (BufferedReader br = new BufferedReader(new FileReader(csv))) {

        count = 0;
        City e1 = new City();

        while ((line = br.readLine()) != null) {
            if (count > 1) {
                Session session = factory.openSession();
                // creating transaction object  
                Transaction t = session.beginTransaction();
                System.out.println(line);
                String[] s = line.split("[,]");
                System.out.println(s[0]);
                System.out.println(s[1]);
                System.out.println(Float.parseFloat(s[5]));
                System.out.println(Float.parseFloat(s[6]));
                e1.setId(Integer.parseInt(s[0]));
                e1.setName(s[1]);
                e1.setC(s[3]);
                e1.setlat(Float.parseFloat(s[5]));
                e1.setlon(Float.parseFloat(s[6]));
                session.save(e1);
                t.commit();//transaction is commited  
                session.close();
            } else {
                count++;
            }
        }

        //                Session currentSession = factory.openSession();  
        //                List <City> list=null;
        //    //  while(list==null){
        //    list = currentSession.createCriteria(City.class)  
        //                             .add(Restrictions.eq("Name", "NL"))  
        //                             .list();
        //      for(int i=0;i<list.size();i++){
        //          
        //    System.out.println(list.get(i));
        //            }

    } catch (IOException e) {
        e.printStackTrace();
    }
    String a, b;
    float lats = 0;
    float lats2 = 0;
    float longs = 0;
    float longs2 = 0;
    System.out.print("Enter name of first city");
    Distance d1 = new Distance();

    Scanner scanner = new Scanner(System.in);
    a = scanner.nextLine();
    a = "\"" + a + "\"";
    System.out.print("Enter name of second city");
    //Scanner scanner=new Scanner(System.in);
    b = scanner.nextLine();
    b = "\"" + b + "\"";
    Transaction tx;

    Session session = factory.openSession();
    try {
        // int index=0;
        tx = session.beginTransaction();
        List employees = session.createQuery("FROM City").list();
        for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
            City c = (City) iterator.next();

            if (a.contentEquals(c.getC())) {

                System.out.print("Name: " + c.getName());
                System.out.print(" City: " + c.getC());
                lats = c.getlon();
                longs = c.getlat();
                System.out.print("  Longitude: " + c.getlon());
                System.out.println("  Latitude: " + c.getlat());
                break;
            }
        }
        List<String> citynames = new ArrayList<String>();
        List<Double> doubleList = new ArrayList<Double>();
        System.out.println("Enter number of cities");
        Scanner s = new Scanner(System.in);
        int num = Integer.parseInt(s.nextLine());

        Distance obj = new Distance();
        int i = 0;
        for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
            City c = (City) iterator.next();
            if (i < num) {
                doubleList.add(obj.GreatD(lats, longs, c.getlat(), c.getlon()));
                citynames.add(c.getC());
                i++;
            } else {
                //get max value from list
                Integer j = 0, maxIndex = -1;
                Double max = null;
                for (Double x : doubleList) {
                    if ((x != null) && ((max == null) || (x > max))) {
                        max = x;
                        maxIndex = j;
                    }
                    j++;
                }
                double dare = obj.GreatD(lats, longs, c.getlat(), c.getlon());
                if (dare < max) {
                    citynames.set(maxIndex, c.getC());
                    doubleList.set(maxIndex, dare);
                    //list
                }
            }
            // For first n values add to list

        }
        for (int k = 0; k < citynames.size(); k++) {
            System.out.println(citynames.get(k));
        }
        //         

        int check = 0;
        for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
            City c = (City) iterator.next();

            if (check == 2) {
                break;
            } else {
                if (b.contentEquals(c.getC())) {

                    System.out.print("Name: " + c.getName());
                    System.out.print(" City: " + c.getC());
                    longs = c.getlon();
                    lats = c.getlat();
                    System.out.print("  Longitude: " + c.getlon());
                    System.out.println("  Latitude: " + c.getlat());
                    check++;
                }
                if (a.contentEquals(c.getC())) {

                    System.out.print("Name: " + c.getName());
                    System.out.print(" City: " + c.getC());
                    longs2 = c.getlon();
                    lats2 = c.getlat();
                    System.out.print("  Longitude: " + c.getlon());
                    System.out.println("  Latitude: " + c.getlat());
                    check++;
                }
            }
        }
        System.out.println(d1.GreatD(lats, longs, lats2, longs2));

        tx.commit();
        return;
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:aa.PersonDog.dao.HibernateFactory.java

/**
 *
 * @return/*  w  w  w  . j a  v  a2 s .  c  o m*/
 * @throws HibernateException
 */
private static SessionFactory configureSessionFactory() throws HibernateException {

    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");
    log.info("Hibernate Configuration created successfully");

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();
    log.info("ServiceRegistry created successfully");
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    log.info("SessionFactory created successfully");

    return sessionFactory;
}