blob: de5e59ab667c61ff271cc059e15a70f5dac93576 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
package examples.entity.intro;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.persistence.*;
/**
* Stateless session bean facade for account entities, remotely accessible
*/
@Stateless
@Remote(Bank.class)
@EntityListener(MyListener.class)
public class BankBean implements Bank {
/**
* The entity manager object, injected by the container
*/
@PersistenceContext
private EntityManager manager;
public List<Account> listAccounts() {
//Query query = manager.createQuery("SELECT a FROM Account a");
Query query = manager.createNamedQuery("findThem");
return query.getResultList();
}
public void printBigAccounts() {
Query query =
manager.createQuery("SELECT a FROM Account a WHERE a.balance > 1000");
System.out.println("Got " + query.getResultList().size() + " records.");
// do sth else in the meantime
try {
Thread.sleep(15000);
} catch (InterruptedException e )
{}
query =
manager.createQuery("SELECT a FROM Account a WHERE a.balance > 1000");
System.out.println("Got " + query.getResultList().size() + " records.");
}
public Account openAccount(String ownerName) {
Account account = new Account();
account.ownerName = ownerName;
System.out.println("Entity belongs to persistence context: " +
manager.contains(account));
manager.persist(account);
System.out.println("Entity belongs to persistence context: " +
manager.contains(account));
return account;
}
public Account openAccount(String ownerName, int accNum) {
Account account = new Account();
account.ownerName = ownerName;
account.accountNumber = accNum;
System.out.println("Entity belongs to persistence context: " +
manager.contains(account));
manager.persist(account);
System.out.println("Entity belongs to persistence context: " +
manager.contains(account));
return account;
}
public int getBalance(int accountNumber) {
Account account = manager.find(Account.class, accountNumber);
return account.balance;
}
public void deposit(int accountNumber, int amount) {
Account account = manager.find(Account.class, accountNumber);
account.balance += amount;
}
public int withdraw(int accountNumber, int amount) {
Account account = manager.find(Account.class, accountNumber);
System.out.println("Withdrawing... " );
account.balance -= amount;
System.out.println("... amount now... " + account.balance );
manager.flush();
return amount;
}
public void close(int accountNumber) {
Account account = manager.find(Account.class, accountNumber);
try {
Thread.sleep(20000);
} catch (InterruptedException e )
{}
manager.remove(account);
try {
Thread.sleep(20000);
} catch (InterruptedException e )
{}
}
public void update(Account a) {
manager.merge(a);
}
public void checkBalance(int accountNumber) {
System.out.println("Checking... sleep");
try {
Thread.sleep(3000);
} catch (InterruptedException e )
{}
Account account = manager.find(Account.class, accountNumber);
manager.refresh(account);
int b = account.balance;
System.out.println("Checking... " + b );
}
}
|