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

📄 inheritancedemo.java

📁 hibernate_tutorial经典教程
💻 JAVA
字号:
package tutorial.inheritance.tableperconcreteclass;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 InheritanceDemo {	public static void main(String[] args) {		try {			new InheritanceDemo();		} catch(HibernateException he) {			he.printStackTrace();		}	}		public InheritanceDemo() throws HibernateException {		Configuration config = new Configuration();		// add the classes you want to persist		// make sure the mapping files are on the classpath		config.addClass(Superclass.class);		config.addClass(Subclass.class);				SessionFactory sf = config.buildSessionFactory();				Session sess = sf.openSession();		Transaction tx = null;		try {			tx = sess.beginTransaction();						Superclass sup = new Superclass();			sup.setName("abstract class property");			sup.setSuperName("superclass property");			sess.save(sup);						Subclass sub = new Subclass();			sub.setName("abstract class property");			sub.setSuperName("superclass property");			sub.setSubName("subclass property");			sess.save(sub);					tx.commit();		} catch (HibernateException e) {		    if (tx!=null) tx.rollback();		    throw e;		}		finally {		    sess.close();		}		sess = sf.openSession();		tx = null;		try {		    tx = sess.beginTransaction();		    List pets = sess.find("from " + AbstractClass.class.getName());		    for (Iterator it = pets.iterator(); it.hasNext();) {				Superclass sc = (Superclass) it.next();				System.out.println("Class loaded from DB: " + sc.getClass().getName());			}		    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 + -