KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > impl > hibernate > DemoCategoryHibernate


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002, 2003 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27 // $Id: DemoCategoryHibernate.java,v 1.2 2003/08/30 11:02:20 niko_schmuck Exp $
28

29 package de.nava.informa.impl.hibernate;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import de.nava.informa.core.CategoryIF;
35
36 import net.sf.hibernate.Hibernate;
37 import net.sf.hibernate.HibernateException;
38 import net.sf.hibernate.Session;
39 import net.sf.hibernate.Transaction;
40
41 /**
42  * Class demonstrating the use of the hibernate backend to persist the
43  * channel object model to a relational database.
44  *
45  * @author Niko Schmuck
46  */

47 public class DemoCategoryHibernate {
48
49   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
50     SessionHandler handler = SessionHandler.getInstance();
51     Session session = handler.getSession();
52     ChannelBuilder builder = new ChannelBuilder(session);
53     
54     // --- create a new cat
55
CategoryIF catA = builder.createCategory(null, "News Category");
56     Long JavaDoc catId = null;
57     
58     Transaction tx = null;
59     try {
60       tx = session.beginTransaction();
61       session.save(catA);
62       tx.commit();
63       catId = new Long JavaDoc(catA.getId());
64       System.out.println("Saved category with id " + catId + " persistently");
65     }
66     catch (HibernateException he) {
67       if (tx != null) tx.rollback();
68       throw he;
69     }
70     finally {
71       session.close();
72     }
73     
74     // --- update
75
session = handler.getSession();
76     try {
77       tx = session.beginTransaction();
78       Category theCat = (Category) session.load(Category.class, catId);
79       theCat.setTitle("Another category title");
80       tx.commit();
81       System.out.println("Updated category title for id: " + catId);
82     } catch (HibernateException he) {
83       if (tx != null) tx.rollback();
84       throw he;
85     }
86     finally {
87       session.close();
88     }
89     
90     // --- list
91
session = handler.getSession();
92     try {
93       tx = session.beginTransaction();
94       // Query q = session.createQuery("select cat.id from Category as cat");
95
// List result = q.list();
96
List JavaDoc cats = session.find("from Category as cat where cat.title = ?",
97                                "Another category title", Hibernate.STRING);
98       tx.commit();
99       Iterator JavaDoc it = cats.iterator();
100       while (it.hasNext()) {
101         Category c = (Category) it.next();
102         System.out.println("--> " + c.getId());
103       }
104     } catch (HibernateException he2) {
105       if (tx != null) tx.rollback();
106       throw he2;
107     }
108     finally {
109       session.close();
110     }
111   }
112
113 }
114
Popular Tags