Java Code Examples for org.hibernate.SessionFactory#close()
The following examples show how to use
org.hibernate.SessionFactory#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InHibernate.java From Transwarp-Sample-Code with MIT License | 8 votes |
public static void main(String[] args) { String path = "hibernate.cfg.xml"; Configuration cfg = new Configuration().configure(path); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); User user = new User(); user.setId("443"); user.setName("baa"); session.save(user); // session.close(); session.getTransaction().commit(); sessionFactory.close(); }
Example 2
Source File: InHibernate.java From Transwarp-Sample-Code with MIT License | 6 votes |
public static void main(String[] args) { String path = "hibernate.cfg.xml"; Configuration cfg = new Configuration().configure(path); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); User user = new User(); user.setId("443"); user.setName("baa"); session.save(user); // session.close(); session.getTransaction().commit(); sessionFactory.close(); }
Example 3
Source File: HibernateTest.java From java-jdbc with Apache License 2.0 | 6 votes |
@Test public void hibernate_with_active_span_only() { SessionFactory sessionFactory = createSessionFactory(true); Session session = sessionFactory.openSession(); Employee employee = new Employee(); session.beginTransaction(); session.save(employee); session.getTransaction().commit(); session.close(); sessionFactory.close(); assertNotNull(employee.id); List<MockSpan> finishedSpans = mockTracer.finishedSpans(); assertEquals(0, finishedSpans.size()); assertNull(mockTracer.activeSpan()); }
Example 4
Source File: InHibernate.java From Transwarp-Sample-Code with MIT License | 6 votes |
public static void main(String[] args) { String path = "hibernate.cfg.xml"; Configuration cfg = new Configuration().configure(path); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); User user = new User(); user.setId("46"); user.setName("aaa"); session.save(user); // session.close(); session.getTransaction().commit(); sessionFactory.close(); }
Example 5
Source File: HibernateTest.java From java-jdbc with Apache License 2.0 | 6 votes |
@Test public void hibernate_with_parent_and_active_span_only() { final MockSpan parent = mockTracer.buildSpan("parent").start(); try (Scope ignored = mockTracer.activateSpan(parent)) { SessionFactory sessionFactory = createSessionFactory(true); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(new Employee()); session.save(new Employee()); session.getTransaction().commit(); session.close(); sessionFactory.close(); } parent.finish(); List<MockSpan> spans = mockTracer.finishedSpans(); assertEquals(12, spans.size()); checkSameTrace(spans); assertNull(mockTracer.activeSpan()); }
Example 6
Source File: CRUDTest.java From Project with Apache License 2.0 | 6 votes |
@Test public void deleteTest(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = session.get(User.class, 1); session.delete(user); transaction.commit(); session.close(); sessionFactory.close(); }
Example 7
Source File: Main.java From chuidiang-ejemplos with GNU Lesser General Public License v3.0 | 6 votes |
public static void main(String[] args) { SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory(); try { insertGeometry(sessionFactory); queryGeometry(sessionFactory); updateGeometry(sessionFactory); queryGeometry(sessionFactory); deleteGeometry(sessionFactory); } catch (Exception e){ e.printStackTrace(); } finally { sessionFactory.close(); } }
Example 8
Source File: CRUDTest.java From Project with Apache License 2.0 | 6 votes |
@Test public void addTest(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setUsername("GJXAIOU"); user.setPassword("GJXAIOU"); user.setAddress("江苏"); session.save(user); transaction.commit(); session.close(); sessionFactory.close(); }
Example 9
Source File: CRUDTest.java From Project with Apache License 2.0 | 6 votes |
@Test public void deleteTest(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = session.get(User.class, 1); session.delete(user); transaction.commit(); session.close(); sessionFactory.close(); }
Example 10
Source File: CRUDTest.java From Project with Apache License 2.0 | 6 votes |
@Test public void updateTest(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = session.get(User.class, 1); user.setUsername("gjxaiou"); session.update(user); transaction.commit(); session.close(); sessionFactory.close(); }
Example 11
Source File: CRUDTest.java From Project with Apache License 2.0 | 6 votes |
@Test public void addTest(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setUsername("GJXAIOU"); user.setPassword("GJXAIOU"); user.setAddress("江苏"); session.save(user); transaction.commit(); session.close(); sessionFactory.close(); }
Example 12
Source File: App.java From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 | 6 votes |
/** * The main method that does the CRUD operations. */ public static void main(String[] args) { // create a Hibernate sessionFactory and session StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build(); SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata() .buildSessionFactory(); Session session = sessionFactory.openSession(); clearData(session); writeData(session); readData(session); // close Hibernate session and sessionFactory session.close(); sessionFactory.close(); }
Example 13
Source File: HQLCacheExample.java From journaldev with MIT License | 5 votes |
public static void main(String[] args) { // Prep work SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); // Get All Employees Transaction tx = session.beginTransaction(); // Get Employee with id=3 Query query = session.createQuery("from Employee where id= 3"); Employee emp = (Employee) query.uniqueResult(); System.out.println("Employee Name=" + emp.getName() + ", City=" + emp.getAddress().getCity()); // Get Employee with id=3 query = session.createQuery("from Employee where id= 3"); emp = (Employee) query.uniqueResult(); System.out.println("Employee Name=" + emp.getName() + ", City=" + emp.getAddress().getCity()); // Get Employee with id=3 query = session.createQuery("from Employee where id= 3"); emp = (Employee) query.uniqueResult(); System.out.println("Employee Name=" + emp.getName() + ", City=" + emp.getAddress().getCity()); // rolling back to save the test data tx.commit(); // closing hibernate resources sessionFactory.close(); }
Example 14
Source File: HibernateStoreTest.java From code_quality_principles with Apache License 2.0 | 5 votes |
/** * Create a new user and find it. */ @Test public void whenCreateAndFind() { SessionFactory factory = create(HibernateFactory.FACTORY); Session session = factory.openSession(); HibernateStore store = new HibernateStore(factory); User user = store.add(new User(-1, "Petr Arsentev")); assertThat( store.findById(user.getId()).getLogin(), is("Petr Arsentev") ); session.clear(); assertThat(store.findAll().isEmpty(), is(true)); factory.close(); }
Example 15
Source File: QueryPlanCacheBenchmark.java From tutorials with MIT License | 5 votes |
@TearDown public void tearDownState() { LOGGER.info("State - Teardown"); SessionFactory sessionFactory = session.getSessionFactory(); session.close(); sessionFactory.close(); LOGGER.info("State - Teardown complete"); }
Example 16
Source File: HibernateSaveExample.java From journaldev with MIT License | 4 votes |
public static void main(String[] args) { // Prep Work SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); //save example - without transaction Session session = sessionFactory.openSession(); Employee emp = getTestEmployee(); long id = (Long) session.save(emp); System.out.println("1. Employee save called without transaction, id="+id); session.flush(); System.out.println("*****"); //save example - with transaction Transaction tx1 = session.beginTransaction(); Session session1 = sessionFactory.openSession(); Employee emp1 = getTestEmployee(); long id1 = (Long) session1.save(emp1); System.out.println("2. Employee save called with transaction, id="+id1); System.out.println("3. Before committing save transaction"); tx1.commit(); System.out.println("4. After committing save transaction"); System.out.println("*****"); //save example - existing row in table Session session6 = sessionFactory.openSession(); Transaction tx6 = session6.beginTransaction(); Employee emp6 = (Employee) session6.load(Employee.class, new Long(20)); //update some data System.out.println("Employee Details="+emp6); emp6.setName("New Name"); emp6.getAddress().setCity("New City"); long id6 = (Long) session6.save(emp6); emp.setName("New Name1"); System.out.println("5. Employee save called with transaction, id="+id6); System.out.println("6. Before committing save transaction"); tx6.commit(); System.out.println("7. After committing save transaction"); System.out.println("*****"); // Close resources sessionFactory.close(); }
Example 17
Source File: HibernateManyisOwningSide.java From tutorials with MIT License | 4 votes |
public static void main(String[] args) { Cart cart = new Cart(); Cart cart2 = new Cart(); Items item1 = new Items(cart); Items item2 = new Items(cart2); Set<Items> itemsSet = new HashSet<Items>(); itemsSet.add(item1); itemsSet.add(item2); cart.setItems(itemsSet); SessionFactory sessionFactory = null; Session session = null; Transaction tx = null; try { // Get Session sessionFactory = HibernateAnnotationUtil.getSessionFactory(); session = sessionFactory.getCurrentSession(); System.out.println("Session created"); // start transaction tx = session.beginTransaction(); // Save the Model object session.save(cart); session.save(cart2); session.save(item1); session.save(item2); // Commit transaction tx.commit(); session = sessionFactory.getCurrentSession(); tx = session.beginTransaction(); item1 = (Items) session.get(Items.class, new Long(1)); item2 = (Items) session.get(Items.class, new Long(2)); tx.commit(); System.out.println("item1 ID=" + item1.getId() + ", Foreign Key CartOIO ID=" + item1.getCart() .getId()); System.out.println("item2 ID=" + item2.getId() + ", Foreign Key CartOIO ID=" + item2.getCart() .getId()); } catch (Exception e) { System.out.println("Exception occured. " + e.getMessage()); e.printStackTrace(); } finally { if (!sessionFactory.isClosed()) { System.out.println("Closing SessionFactory"); sessionFactory.close(); } } }
Example 18
Source File: HibernateCacheExample.java From journaldev with MIT License | 4 votes |
public static void main(String[] args) throws InterruptedException { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); //Get employee with id=1 Employee emp = (Employee) session.load(Employee.class, new Long(1)); printData(emp,1); //waiting for sometime to change the data in backend Thread.sleep(10000); //Fetch same data again, check logs that no query fired Employee emp1 = (Employee) session.load(Employee.class, new Long(1)); printData(emp1,2); //Create new session Session newSession = sessionFactory.openSession(); //Get employee with id=1, notice the logs for query Employee emp2 = (Employee) newSession.load(Employee.class, new Long(1)); printData(emp2,3); //START: evict example to remove specific object from hibernate first level cache //Get employee with id=2, first time hence query in logs Employee emp3 = (Employee) session.load(Employee.class, new Long(2)); printData(emp3,4); //evict the employee object with id=1 session.evict(emp); System.out.println("Session Contains Employee with id=1?"+session.contains(emp)); //since object is removed from first level cache, you will see query in logs Employee emp4 = (Employee) session.load(Employee.class, new Long(1)); printData(emp4,5); //this object is still present, so you won't see query in logs Employee emp5 = (Employee) session.load(Employee.class, new Long(2)); printData(emp5,6); //END: evict example //START: clear example to remove everything from first level cache session.clear(); Employee emp6 = (Employee) session.load(Employee.class, new Long(1)); printData(emp6,7); Employee emp7 = (Employee) session.load(Employee.class, new Long(2)); printData(emp7,8); System.out.println("Session Contains Employee with id=2?"+session.contains(emp7)); tx.commit(); sessionFactory.close(); }
Example 19
Source File: HibernateManyToManyMain.java From journaldev with MIT License | 4 votes |
public static void main(String[] args) { Item iphone = new Item(); iphone.setPrice(100); iphone.setDescription("iPhone"); Item ipod = new Item(); ipod.setPrice(50); ipod.setDescription("iPod"); Set<Item> items = new HashSet<Item>(); items.add(iphone); items.add(ipod); Cart cart = new Cart(); cart.setItems(items); cart.setTotal(150); Cart cart1 = new Cart(); Set<Item> items1 = new HashSet<Item>(); items1.add(iphone); cart1.setItems(items1); cart1.setTotal(100); SessionFactory sessionFactory = null; try{ sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); session.save(cart); session.save(cart1); System.out.println("Before committing transaction"); tx.commit(); sessionFactory.close(); System.out.println("Cart ID="+cart.getId()); System.out.println("Cart1 ID="+cart1.getId()); System.out.println("Item1 ID="+iphone.getId()); System.out.println("Item2 ID="+ipod.getId()); }catch(Exception e){ e.printStackTrace(); }finally{ if(sessionFactory != null && !sessionFactory.isClosed()) sessionFactory.close(); } }
Example 20
Source File: HibernateBiDirectionalManyToManyMain.java From journaldev with MIT License | 4 votes |
public static void main(String[] args) { Item iphone = new Item(); iphone.setPrice(100); iphone.setDescription("iPhone"); Item ipod = new Item(); ipod.setPrice(50); ipod.setDescription("iPod"); Cart cart = new Cart(); cart.setTotal(150); Cart cart1 = new Cart(); cart1.setTotal(100); Set<Cart> cartSet = new HashSet<Cart>(); cartSet.add(cart);cartSet.add(cart1); Set<Cart> cartSet1 = new HashSet<Cart>(); cartSet1.add(cart); iphone.setCarts(cartSet1); ipod.setCarts(cartSet); SessionFactory sessionFactory = null; try{ sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); session.save(iphone); session.save(ipod); tx.commit(); sessionFactory.close(); System.out.println("Cart ID="+cart.getId()); System.out.println("Cart1 ID="+cart1.getId()); System.out.println("Item1 ID="+iphone.getId()); System.out.println("Item2 ID="+ipod.getId()); }catch(Exception e){ e.printStackTrace(); }finally{ if(sessionFactory != null && !sessionFactory.isClosed()) sessionFactory.close(); } }