KickJava   Java API By Example, From Geeks To Geeks.

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


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: DemoChannelHibernate.java,v 1.3 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 net.sf.hibernate.HibernateException;
35 import net.sf.hibernate.Session;
36 import net.sf.hibernate.Transaction;
37
38 /**
39  * Class demonstrating the use of the hibernate backend to persist the
40  * channel object model to a relational database.
41  *
42  * @author Niko Schmuck
43  */

44 public class DemoChannelHibernate {
45
46   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
47     SessionHandler handler = SessionHandler.getInstance();
48     Session session = handler.getSession();
49     ChannelBuilder builder = new ChannelBuilder(session);
50     Transaction tx = null;
51
52     // --- list
53
try {
54       tx = session.beginTransaction();
55       // Query q = session.createQuery("select ch.id from Channel as ch");
56
// List result = q.list();
57
List JavaDoc result = session.find("from Channel");
58       // List chs = session.find("from Channel as ch where cat.title = ?",
59
// "Another category title", Hibernate.STRING);
60
tx.commit();
61       Iterator JavaDoc it = result.iterator();
62       while (it.hasNext()) {
63         Channel c = (Channel) it.next();
64         System.out.println("retrieved channel --> " + c.getId());
65         System.out.println(" c: " + c);
66         Iterator JavaDoc it_items = c.getItems().iterator();
67         while (it_items.hasNext()) {
68           Item item = (Item) it_items.next();
69           System.out.println(" * " + item);
70         }
71       }
72     } catch (HibernateException he2) {
73       if (tx != null) tx.rollback();
74       throw he2;
75     }
76     finally {
77       session.close();
78     }
79     
80     // --- create
81
/*
82     session = handler.getSession();
83     
84     try {
85       tx = session.beginTransaction();
86       ChannelIF chA = builder.createChannel("Channel A");
87       chA.setDescription("test channel for hibernate backend");
88       System.out.println("created chA: " + chA);
89       ItemIF itA = builder.createItem("Simple item", "oh what a desc",
90                                       new URL("http://www.sf.net/"));
91       System.out.println("created itA: " + itA);
92       chA.addItem(itA);
93       System.out.println("itA -> chA assigned");
94       session.save(chA);
95       System.out.println("saved chA");
96       session.save(itA);
97       System.out.println("saved itA");
98       tx.commit();
99       System.out.println("Saved channel with id " + chA.getId());
100     }
101     catch (HibernateException he) {
102       if (tx != null) tx.rollback();
103       throw he;
104     }
105     finally {
106       session.close();
107     }
108     */

109     
110     // --- as if nothing happened
111
// chA = null;
112
// itA = null;
113

114   }
115
116 }
117
Popular Tags