📄 lazydevdemo.java
字号:
package tutorial.dev.lazy;import java.util.HashSet;import java.util.Iterator;import net.sf.hibernate.HibernateException;import net.sf.hibernate.LazyInitializationException;import net.sf.hibernate.LockMode;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.Transaction;import net.sf.hibernate.cfg.Configuration;public class LazyDevDemo { public static void main(String[] args) { try { new LazyDevDemo(); } catch(HibernateException he) { he.printStackTrace(); } } public LazyDevDemo() throws HibernateException { Configuration config = new Configuration(); // add the classes you want to persist // make sure the mapping files are on the classpath config.addClass(InLazySet.class); config.addClass(WithLazySet.class); SessionFactory sf = config.buildSessionFactory(); for (int i = 0; i < 100 ; i++) { Session sess = sf.openSession(); Transaction tx = null; // store the orderlist and all products WithLazySet l = new WithLazySet(); l.setLazy(new HashSet()); l.getLazy().add(new InLazySet()); l.getLazy().add(new InLazySet()); l.getLazy().add(new InLazySet()); l.getLazy().add(new InLazySet()); l.getLazy().add(new InLazySet()); l.getLazy().add(new InLazySet()); try { tx = sess.beginTransaction(); sess.save(l); tx.commit(); } catch (HibernateException e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); } System.out.println("Wrote dummy data"); /* sess = sf.openSession(); tx = null; try { tx = sess.beginTransaction(); System.out.println("Loading and accessing in the same session"); WithLazySet l2 = (WithLazySet) sess.get(WithLazySet.class, l.getId()); for (Iterator iter = l2.getLazy().iterator(); iter.hasNext();) { InLazySet lazy = (InLazySet) iter.next(); System.out.println("loaded: " + lazy.getId()); } tx.commit(); } catch (HibernateException e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); } */ /* System.out.println("Loading, closing and accessing"); sess = sf.openSession(); tx = null; WithLazySet l3 = null; try { tx = sess.beginTransaction(); l3 = (WithLazySet) sess.get(WithLazySet.class, l.getId()); tx.commit(); } catch (HibernateException e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); } // should trow exception try { for (Iterator iter = l3.getLazy().iterator(); iter.hasNext();) { InLazySet lazy = (InLazySet) iter.next(); System.out.println("loaded: " + lazy.getId()); } // getting ugly...junit would not have been that bad ;-) throw new RuntimeException("must not be here"); } catch(LazyInitializationException lie) { System.out.println("Caught expected lazyinit exception"); } */ // with reatach System.out.println("Reatach and accessing"); System.out.println("load L4"); sess = sf.openSession(); tx = null; WithLazySet l4 = null; try { tx = sess.beginTransaction(); l4 = (WithLazySet) sess.get(WithLazySet.class, l.getId()); tx.commit(); } catch (HibernateException e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); } System.out.println("reatach"); sess = sf.openSession(); tx = null; try { tx = sess.beginTransaction(); //sess.lock(l4, LockMode.NONE); sess.update(l4); System.out.println("locked"); for (Iterator iter = l4.getLazy().iterator(); iter.hasNext();) { InLazySet lazy = (InLazySet) iter.next(); System.out.println("loaded: " + lazy.getId()); } tx.commit(); } catch (HibernateException e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); } /* // with disconnect System.out.println("disconnect/connect and accessing"); sess = sf.openSession(); tx = null; WithLazySet l5 = null; try { tx = sess.beginTransaction(); l5 = (WithLazySet) sess.get(WithLazySet.class, l.getId()); tx.commit(); } catch (HibernateException e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.disconnect(); } sess.reconnect(); for (Iterator iter = l5.getLazy().iterator(); iter.hasNext();) { InLazySet lazy = (InLazySet) iter.next(); System.out.println("loaded: " + lazy.getId()); } sess.close(); */ try { Thread.sleep(5000); } catch(InterruptedException ie) { ie.printStackTrace(); } System.out.println("Next round"); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -