| 1 | package net.sourceforge.finmodel.example; |
| 2 | |
| 3 | import java.math.BigDecimal; |
| 4 | import java.util.Iterator; |
| 5 | |
| 6 | import net.sourceforge.finmodel.plan.FinancialPlan; |
| 7 | |
| 8 | import org.hibernate.Session; |
| 9 | import org.hibernate.SessionFactory; |
| 10 | import org.hibernate.Transaction; |
| 11 | import org.hibernate.cfg.Configuration; |
| 12 | |
| 13 | public class HibernateExample { |
| 14 | |
| 15 | static final BigDecimal ZERO = BigDecimal.ZERO; |
| 16 | |
| 17 | public static void main(String args[]) { |
| 18 | Session session = null; |
| 19 | |
| 20 | SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); |
| 21 | session = sessionFactory.openSession(); |
| 22 | Transaction tx = session.beginTransaction(); |
| 23 | |
| 24 | try { |
| 25 | tx.begin(); |
| 26 | SimplePlan plan = new SimplePlan(); |
| 27 | |
| 28 | System.out.println("Saving SimplePlan."); |
| 29 | System.out.println(session.save(plan.finPlan)); |
| 30 | |
| 31 | tx.commit(); |
| 32 | |
| 33 | tx.begin(); |
| 34 | Iterator plans = |
| 35 | session.createQuery("from net.sourceforge.finmodel.plan.FinancialPlan").iterate(); |
| 36 | while (plans.hasNext()) { |
| 37 | FinancialPlan plan1 = (FinancialPlan) plans.next(); |
| 38 | System.err.println(">>> " + plan1.getName()); |
| 39 | } |
| 40 | |
| 41 | tx.commit(); |
| 42 | } catch (Exception e) { |
| 43 | tx.rollback(); |
| 44 | e.printStackTrace(); |
| 45 | } finally { |
| 46 | session.flush(); |
| 47 | session.close(); |
| 48 | } |
| 49 | } |
| 50 | } |