summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/entity/intro/AccountBean.java
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/entity/intro/AccountBean.java
downloadStudium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz
Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/entity/intro/AccountBean.java')
-rw-r--r--Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/entity/intro/AccountBean.java70
1 files changed, 70 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/entity/intro/AccountBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/entity/intro/AccountBean.java
new file mode 100644
index 0000000..45f05c3
--- /dev/null
+++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/entity/intro/AccountBean.java
@@ -0,0 +1,70 @@
+package examples.entity.intro;
+
+import javax.ejb.Interceptors;
+import javax.ejb.Remote;
+import javax.ejb.Stateful;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.EntityListener;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+
+/**
+ * Stateful session bean facade for account entities, remotely accessible
+ */
+
+@Stateful
+@Remote(AccountInterface.class)
+public class AccountBean implements AccountInterface {
+
+ /** The entity manager, injected by the container */
+ @PersistenceContext(type=PersistenceContextType.EXTENDED)
+ private EntityManager manager;
+
+ private Account account;
+
+ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+ public void open(int accountNumber) {
+ account = manager.find(Account.class, accountNumber);
+ if(account == null)
+ account = new Account();
+
+ account.ownerName = "anonymous";
+ account.accountNumber = accountNumber;
+ manager.persist(account);
+ }
+
+ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+ public int getBalance() {
+ if(account==null)
+ throw new IllegalStateException();
+ return account.balance;
+ }
+
+ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+ public void deposit(int amount) {
+ if(account==null)
+ throw new IllegalStateException();
+ //manager = emf.createEntityManager(PersistenceContextType.EXTENDED);
+ //account = manager.merge(account);
+ account.balance += amount;
+ //manager.flush();
+ }
+
+ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+ public int withdraw(int amount) {
+ if(account==null)
+ throw new IllegalStateException();
+ // manager.lock(account, LockMode.WRITE);
+ if (amount > account.balance) {
+ return 0;
+ } else {
+ account.balance -= amount;
+ return amount;
+ }
+ }
+
+
+
+}