multidemo.java

来自「hibernate_tutorial经典教程」· Java 代码 · 共 90 行

JAVA
90
字号
package tutorial.dev.multi;import java.io.Serializable;import java.util.Iterator;import java.util.List;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.Transaction;import net.sf.hibernate.cfg.Configuration;public class MultiDemo {	public static void main(String[] args) {		try {			new MultiDemo();		} catch(HibernateException he) {			he.printStackTrace();		}	}		public MultiDemo() throws HibernateException {		Configuration config = new Configuration();		// add the classes you want to persist		// make sure the mapping files are on the classpath		config.addClass(BasicView.class);		config.addClass(ComplexView.class);				SessionFactory sf = config.buildSessionFactory();			Session sess = sf.openSession();		Transaction tx = null;		// store the orderlist and all products		Serializable id = null;		BasicView bv = null;		ComplexView cv = null;		try {			tx = sess.beginTransaction();			bv = new BasicView();			bv.setPropA("33333");						cv = new ComplexView();			cv.setValue(new Long(1111));									sess.save(bv);			sess.save(cv);															tx.commit();		} catch (HibernateException e) {		    if (tx!=null) tx.rollback();		    throw e;		} 		finally {		    sess.close();		}		sess = sf.openSession();		// store the orderlist and all products		try {			tx = sess.beginTransaction();					BasicView bv1 = (BasicView) sess.get(BasicView.class, bv.getId());			ComplexView cv1 = (ComplexView) sess.get(ComplexView.class, cv.getId());						System.out.println(""+bv1);			System.out.println(""+cv1);						List list = sess.find("from " + ComplexView.class.getName() +" as cv where cv.value != null");						for (Iterator iter = list.iterator(); iter.hasNext();) {				ComplexView element = (ComplexView) iter.next();				System.out.println("List: " + element);			}									tx.commit();		} catch (HibernateException e) {		    if (tx!=null) tx.rollback();		    throw e;		} 		finally {		    sess.close();		}	}	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?