📄 inheritancedemo.java
字号:
package tutorial.inheritance.tablepersubclass;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(Payment.class); SessionFactory sf = config.buildSessionFactory(); Session sess = sf.openSession(); Transaction tx = null; try { tx = sess.beginTransaction(); CashPayment cash = new CashPayment(); cash.setAmount(new Double(100.55)); cash.setSomething("foo bar baz"); sess.save(cash); CreditcardPayment credit = new CreditcardPayment(); credit.setAmount(new Double(546.4)); credit.setCardNumber("1234-5678-1234-5678"); sess.save(credit); 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 " + Payment.class.getName()); for (Iterator it = pets.iterator(); it.hasNext();) { Payment payment = (Payment) it.next(); System.out.println( "Payment amount " + payment.getAmount() + " its class is: " + payment.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 + -