⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multidemo.java

📁 hibernate_tutorial经典教程
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -