Java Code Examples for org.hibernate.SessionFactory#isClosed()

The following examples show how to use org.hibernate.SessionFactory#isClosed() . 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: HibernateSessionFactoryListener.java    From journaldev with MIT License 5 votes vote down vote up
public void contextDestroyed(ServletContextEvent servletContextEvent) {
	SessionFactory sessionFactory = (SessionFactory) servletContextEvent.getServletContext().getAttribute("SessionFactory");
	if(sessionFactory != null && !sessionFactory.isClosed()){
		logger.info("Closing sessionFactory");
		sessionFactory.close();
	}
	logger.info("Released Hibernate sessionFactory resource");
}
 
Example 2
Source File: AbsSupportDao.java    From jeesupport with MIT License 5 votes vote down vote up
protected Session _get_session( String _db ) {
	if ( sessionFactoryMap.containsKey( _db ) ) {
		SessionFactory sessionFactory = sessionFactoryMap.get( _db );
		if( sessionFactory.isClosed()) return sessionFactory.openSession();
		return sessionFactory.getCurrentSession();
	}

	throw new NullPointerException( "没有找到数据库[" + _db + "]的对应Session容器,请检查配置文件中是否正确。" );
}
 
Example 3
Source File: HibernateManyToManyAnnotationMain.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	Item1 item1 = new Item1();
	item1.setDescription("samsung"); item1.setPrice(300);
	Item1 item2 = new Item1();
	item2.setDescription("nokia"); item2.setPrice(200);
	Cart1 cart = new Cart1();
	cart.setTotal(500);
	Set<Item1> items = new HashSet<Item1>();
	items.add(item1); items.add(item2);
	cart.setItems(items);
	
	SessionFactory sessionFactory = null;
	try{
	sessionFactory = HibernateAnnotationUtil.getSessionFactory();
	Session session = sessionFactory.getCurrentSession();
	Transaction tx = session.beginTransaction();
	session.save(cart);
	System.out.println("Before committing transaction");
	tx.commit();
	sessionFactory.close();
	
	System.out.println("Cart ID="+cart.getId());
	System.out.println("Item1 ID="+item1.getId());
	System.out.println("Item2 ID="+item2.getId());
	
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		if(sessionFactory != null && !sessionFactory.isClosed()) sessionFactory.close();
	}
}
 
Example 4
Source File: HibernateOneToOneMain.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	
	Txn txn = buildDemoTransaction();
	
	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction tx = null;
	try{
	//Get Session
	sessionFactory = HibernateUtil.getSessionFactory();
	session = sessionFactory.getCurrentSession();
	System.out.println("Session created");
	//start transaction
	tx = session.beginTransaction();
	//Save the Model object
	session.save(txn);
	//Commit transaction
	tx.commit();
	System.out.println("Transaction ID="+txn.getId());
	
	//Get Saved Trasaction Data
	printTransactionData(txn.getId(), sessionFactory);
	
	}catch(Exception e){
		System.out.println("Exception occured. "+e.getMessage());
		e.printStackTrace();
	}finally{
		if(!sessionFactory.isClosed()){
			System.out.println("Closing SessionFactory");
			sessionFactory.close();
		}
	}
}
 
Example 5
Source File: HibernateOneToOneAnnotationMain.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	
	Txn1 txn = buildDemoTransaction();
	
	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction tx = null;
	try{
	//Get Session
	sessionFactory = HibernateAnnotationUtil.getSessionFactory();
	session = sessionFactory.getCurrentSession();
	System.out.println("Session created using annotations configuration");
	//start transaction
	tx = session.beginTransaction();
	//Save the Model object
	session.save(txn);
	//Commit transaction
	tx.commit();
	System.out.println("Annotation Example. Transaction ID="+txn.getId());
	
	//Get Saved Trasaction Data
	printTransactionData(txn.getId(), sessionFactory);
	}catch(Exception e){
		System.out.println("Exception occured. "+e.getMessage());
		e.printStackTrace();
	}finally{
		if(sessionFactory != null && !sessionFactory.isClosed()){
			System.out.println("Closing SessionFactory");
			sessionFactory.close();
		}
	}
}
 
Example 6
Source File: HibernateManyisOwningSide.java    From tutorials with MIT License 4 votes vote down vote up
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 7
Source File: HibernateOneisOwningSide.java    From tutorials with MIT License 4 votes vote down vote up
public static void main(String[] args) {

        CartOIO cart = new CartOIO();
        CartOIO cart2 = new CartOIO();

        ItemsOIO item1 = new ItemsOIO(cart);
        ItemsOIO item2 = new ItemsOIO(cart2);
        Set<ItemsOIO> itemsSet = new HashSet<ItemsOIO>();
        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 = (ItemsOIO) session.get(ItemsOIO.class, new Long(1));
            item2 = (ItemsOIO) session.get(ItemsOIO.class, new Long(2));
            tx.commit();

            System.out.println("item1 ID=" + item1.getId() + ", Foreign Key CartOIO ID=" + item1.getCartOIO()
                .getId());
            System.out.println("item2 ID=" + item2.getId() + ", Foreign Key CartOIO ID=" + item2.getCartOIO()
                .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 8
Source File: HibernateOneToManyAnnotationMain.java    From tutorials with MIT License 4 votes vote down vote up
public static void main(String[] args) {

        Cart cart = new Cart();

        Items item1 = new Items(cart);
        Items item2 = new Items(cart);
        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(item1);
            session.save(item2);
            // Commit transaction
            tx.commit();
            System.out.println("Cart ID=" + cart.getId());
            System.out.println("item1 ID=" + item1.getId() + ", Foreign Key Cart ID=" + item1.getCart().getId());
            System.out.println("item2 ID=" + item2.getId() + ", Foreign Key Cart ID=" + item1.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 9
Source File: DAOService.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
private Session getSession() {
    SessionFactory sf = com.packtpub.pf.blueprint.persistence.HibernateUtil.getSessionFactory();
    return sf.isClosed() ? sf.openSession() : sf.getCurrentSession();
}
 
Example 10
Source File: ProductService.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
private Session getSession() {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    return sf.isClosed() ? sf.openSession() : sf.getCurrentSession();
}
 
Example 11
Source File: DAOService.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
private Session getSession() {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    return sf.isClosed() ? sf.openSession() : sf.getCurrentSession();
}
 
Example 12
Source File: DAOService.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
private Session getSession() {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    return sf.isClosed() ? sf.openSession() : sf.getCurrentSession();
}
 
Example 13
Source File: ProductsDAO.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
private Session getSession() {
	SessionFactory sf = HibernateUtil.getSessionFactory();
	return sf.isClosed() ? sf.openSession() : sf.getCurrentSession();
}
 
Example 14
Source File: SessionFactoryCreator.java    From mamute with Apache License 2.0 4 votes vote down vote up
void destroy(@Disposes SessionFactory factory) {
	if (!factory.isClosed()) {
		factory.close();
	}
	factory = null;
}
 
Example 15
Source File: HibernateOneToManyMain.java    From journaldev with MIT License 4 votes vote down vote up
public static void main(String[] args) {

		Cart cart = new Cart();
		cart.setName("MyCart");
		
		Items item1 = new Items("I1", 10, 1, cart);
		Items item2 = new Items("I2", 20, 2, cart);
		Set<Items> itemsSet = new HashSet<Items>();
		itemsSet.add(item1); itemsSet.add(item2);
		
		cart.setItems(itemsSet);
		cart.setTotal(10*1 + 20*2);
		
		SessionFactory sessionFactory = null;
		Session session = null;
		Transaction tx = null;
		try{
		//Get Session
		sessionFactory = HibernateUtil.getSessionFactory();
		session = sessionFactory.getCurrentSession();
		System.out.println("Session created");
		//start transaction
		tx = session.beginTransaction();
		
		//Save the Model objects
		session.save(cart);
		session.save(item1);
		session.save(item2);
		
		//Commit transaction
		tx.commit();
		System.out.println("Cart ID="+cart.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 16
Source File: HibernateOneToManyAnnotationMain.java    From journaldev with MIT License 4 votes vote down vote up
public static void main(String[] args) {

		Cart1 cart = new Cart1();
		cart.setName("MyCart1");
		
		Items1 item1 = new Items1("I10", 10, 1, cart);
		Items1 item2 = new Items1("I20", 20, 2, cart);
		Set<Items1> itemsSet = new HashSet<Items1>();
		itemsSet.add(item1); itemsSet.add(item2);
		
		cart.setItems1(itemsSet);
		cart.setTotal(10*1 + 20*2);
		
		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(item1);
		session.save(item2);
		//Commit transaction
		tx.commit();
		System.out.println("Cart1 ID="+cart.getId());
		System.out.println("item1 ID="+item1.getId()+", Foreign Key Cart ID="+item1.getCart1().getId());
		System.out.println("item2 ID="+item2.getId()+", Foreign Key Cart ID="+item1.getCart1().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 17
Source File: HibernateManyToManyMain.java    From journaldev with MIT License 4 votes vote down vote up
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 18
Source File: HibernateBiDirectionalManyToManyMain.java    From journaldev with MIT License 4 votes vote down vote up
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();
	}
	
}