KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > samples > hibernate > Account


1 /*
2   Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.samples.hibernate;
19
20 /**
21  * Sample program using the Hibernate AC.
22  *
23  * Hibernate requires that persistent classes provide:
24  * - getters/setters for all persistent fields
25  * - an empty constructor (Hibernate uses Constructor.newInstance())
26  *
27  * HibernateAC uses JAC object names as a primary key to identify
28  * objects stored in the database. Hence persistent class must
29  * provide a String field (here id) declared as the primary key
30  * in the associated .hbm.xml file.
31  *
32  * Note also that accessing fields directly the (eg balance)
33  * may lead to inconsistencies.
34  * Calling getters and setters ensures that the most recent
35  * values are always fetched from the database.
36  *
37  * @author Lionel Seinturier
38  * @version 1.0
39  */

40 public class Account {
41     
42     private String JavaDoc id;
43     private String JavaDoc name;
44     private double balance;
45
46     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
47         
48         Account bob = new Account();
49         Account robert = new Account();
50         
51         bob.setName("Bob");
52         robert.setName("Robert");
53         
54         double initial = (int) (Math.random()*100) * 2;
55         
56         System.out.println("Set balance Bob: "+initial);
57         bob.setBalance(initial);
58         System.out.println(bob);
59         System.out.println(robert);
60         System.out.println();
61         
62         System.out.println("Transfert from Bob to Robert: "+initial/2);
63         transfert(bob,robert,initial/2);
64         System.out.println();
65         
66         System.out.println("Transfert from Bob to Robert: "+initial*2);
67         transfert(bob,robert,initial*2);
68         System.out.println();
69         
70         System.exit(1);
71     }
72     
73     public void setId(String JavaDoc id){ this.id = id; }
74     public String JavaDoc getId() { return id; }
75     
76     public void setName(String JavaDoc name){ this.name = name; }
77     public String JavaDoc getName() { return name; }
78     
79     public void setBalance(double balance) { this.balance = balance; }
80     public double getBalance() { return balance; }
81     
82     public void credit( double amount ) {
83         String JavaDoc name = getName();
84         double balance = getBalance();
85         
86         System.out.print(name+": "+balance+" -> ");
87         balance += amount;
88         System.out.println(balance);
89         
90         setBalance(balance);
91     }
92     
93     public void withdraw( double amount ) {
94         String JavaDoc name = getName();
95         double balance = getBalance();
96         
97         System.out.print(name+": "+balance+" -> ");
98         balance -= amount;
99         System.out.println(balance);
100         
101         setBalance(balance);
102     }
103     
104     public static void transfert( Account from, Account to, double amount ) {
105         to.credit(amount);
106         from.withdraw(amount);
107     }
108     
109     public String JavaDoc toString() {
110         return "Account: "+getName()+", balance: "+getBalance();
111     }
112 }
113
Popular Tags