by admin ~ October 14, 2008
Following is the example from the chapter “Session Beans” from the book “Enterprise JavaBeans 3.0″ by Bill Bruke & Richard Monson-Haefel. This example I have tried it out on JBoss AS 5.0.0.CR1. I have used Postgresql as the database. And I have used eclipse WTP as IDE.
Example 1: Stateless Session Beans
Client: MakePayment.java
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
| package com.titan.clients;
import com.titan.processpayment.*;
import com.titan.domain.Customer;
import com.titan.access.DataAccess;
import java.util.Calendar;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
/**
* Example demonstrating use of ProcessPayment EJB directly
*
*/
public class MakePayment {
public static void main(String[] args) {
try {
// obtain CustomerHome
Context jndiContext = getInitialContext();
DataAccess access = (DataAccess) jndiContext
.lookup("TitanCruises/DataAccessBean/remote");
access.makePaymentDbTable();
Customer cust = new Customer();
cust.setFirstName("Bill");
cust.setLastName("Burke");
access.createCustomer(cust);
ProcessPaymentRemote procpay = (ProcessPaymentRemote) jndiContext
.lookup("TitanCruises/ProcessPaymentBean/remote");
System.out.println("Making a payment using byCash()..");
procpay.byCash(cust, 1000.0);
System.out.println("Making a payment using byCheck()..");
CheckDO check = new CheckDO("010010101101010100011", 3001);
procpay.byCheck(cust, check, 2000.0);
System.out.println("Making a payment using byCredit()..");
Calendar expdate = Calendar.getInstance();
expdate.set(2025, 1, 28); // month=1 is February
CreditCardDO credit = new CreditCardDO("370000000000002", expdate
.getTime(), "AMERICAN_EXPRESS");
procpay.byCredit(cust, credit, 3000.0);
System.out
.println("Making a payment using byCheck() with a low check number..");
CheckDO check2 = new CheckDO("111000100111010110101", 50);
try {
procpay.byCheck(cust, check2, 9000.0);
System.out
.println("Problem! The PaymentException has not been raised!");
} catch (PaymentException pe) {
System.out.println("Caught PaymentException: "
+ pe.getMessage());
}
access.dropPaymentDbTable();
} catch (Throwable t) {
t.printStackTrace();
}
}
static public Context getInitialContext() throws Exception {
return new InitialContext();
}
} |
jndi.properties
1
2
3
| java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=202.141.151.148:1099 |
DataAccess.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| package com.titan.access;
import javax.ejb.Remote;
import com.titan.domain.Customer;
@Remote
public interface DataAccess{
public int createCustomer(Customer cust);
public Customer findCustomer(int pKey);
public void makePaymentDbTable();
public void dropPaymentDbTable();
} |
DataAccessBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
| package com.titan.access;
import javax.ejb.Remote;
import com.titan.domain.Customer;
@Remote
public interface DataAccess{
public int createCustomer(Customer cust);
public Customer findCustomer(int pKey);
public void makePaymentDbTable();
public void dropPaymentDbTable();
} |
Address.java
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
| package com.titan.domain;
import javax.persistence.*;
@Entity
public class Address implements java.io.Serializable {
private int id;
private String street;
private String city;
private String state;
private String zip;
@Id @GeneratedValue
public int getId() { return id;}
public void setId(int id) { this.id = id; }
public String getStreet() { return street; }
public void setStreet(String street) { this.street = street; }
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
public String getState() { return state; }
public void setState(String state) { this.state = state; }
public String getZip() { return zip; }
public void setZip(String zip) { this.zip = zip; }
} |
Cabin.java
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
| package com.titan.domain;
import javax.persistence.*;
@Entity
public class Cabin implements java.io.Serializable{
private int id;
private String name;
private int bedCount;
private int deckLevel;
private Ship ship;
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getBedCount() { return bedCount; }
public void setBedCount(int count) { this.bedCount = count; }
public int getDeckLevel() { return deckLevel; }
public void setDeckLevel(int level) { this.deckLevel = level; }
@ManyToOne
@JoinColumn(name="SHIP_ID")
public Ship getShip() { return ship; }
public void setShip(Ship ship) { this.ship = ship; }
} |
CreditCard.java
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
| package com.titan.domain;
import javax.persistence.*;
import java.util.Date;
@Entity
public class CreditCard implements java.io.Serializable {
private int id;
private Date expirationDate;
private String number;
private String nameOnCard;
private Customer customer;
private CreditCompany creditCompany;
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public Date getExpirationDate() { return expirationDate; }
public void setExpirationDate(Date date) { expirationDate = date; }
public String getNumber() { return number; }
public void setNumber(String number) { this.number = number; }
public String getNameOnCard() { return nameOnCard; }
public void setNameOnCard(String name) { nameOnCard = name; }
@OneToOne(mappedBy="creditCard")
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
@ManyToOne
public CreditCompany getCreditCompany() { return creditCompany; }
public void setCreditCompany(CreditCompany creditCompany) { this.creditCompany = creditCompany; }
} |
CreditCompany.java
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
| package com.titan.domain;
import javax.persistence.*;
import java.util.*;
@Entity
public class CreditCompany implements java.io.Serializable {
private int id;
private String name;
private Address address;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "ADDRESS_ID")
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
} |
Cruise.java
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
| package com.titan.domain;
import java.util.*;
import javax.persistence.*;
@Entity
public class Cruise implements java.io.Serializable {
private int id;
private String name;
private Ship ship;
private Collection<Reservation> reservations = new ArrayList<Reservation>();
public Cruise() {
}
public Cruise(String name, Ship ship) {
this.name = name;
this.ship = ship;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
public Ship getShip() {
return ship;
}
public void setShip(Ship ship) {
this.ship = ship;
}
@OneToMany(mappedBy = "cruise")
public Collection<Reservation> getReservations() {
return reservations;
}
public void setReservations(Collection<Reservation> res) {
reservations = res;
}
} |
Customer.java
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
| package com.titan.domain;
import javax.persistence.*;
import java.util.*;
@Entity
public class Customer implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
private boolean hasGoodCredit;
private Address address;
private Collection<Phone> phoneNumbers = new ArrayList<Phone>();
private CreditCard creditCard;
private Collection<Reservation> reservations = new ArrayList<Reservation>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean getHasGoodCredit() {
return hasGoodCredit;
}
public void setHasGoodCredit(boolean flag) {
hasGoodCredit = flag;
}
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@JoinColumn(name = "ADDRESS_ID")
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
public CreditCard getCreditCard() {
return creditCard;
}
public void setCreditCard(CreditCard card) {
creditCard = card;
}
@OneToMany(cascade = { CascadeType.ALL })
@JoinColumn(name = "CUSTOMER_ID")
public Collection<Phone> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(Collection<Phone> phones) {
this.phoneNumbers = phones;
}
@ManyToMany(mappedBy = "customers", cascade = CascadeType.REFRESH)
public Collection<Reservation> getReservations() {
return reservations;
}
public void setReservations(Collection<Reservation> reservations) {
this.reservations = reservations;
}
} |
Name.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package com.titan.domain;
public class Name {
private String first;
private String last;
public Name(String first, String last) {
this.first = first;
this.last = last;
}
public String getFirst() { return this.first; }
public String getLast() { return this.last; }
} |
Phone.java
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
| package com.titan.domain;
import javax.persistence.*;
@Entity
public class Phone implements java.io.Serializable {
private int id;
private String number;
private byte type;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public byte getType() {
return type;
}
public void setType(byte type) {
this.type = type;
}
} |
Reservation.java
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
| package com.titan.domain;
import javax.persistence.*;
import java.util.*;
@Entity
public class Reservation implements java.io.Serializable {
private int id;
private Date date;
private double amountPaid;
private Cruise cruise;
private Set<Cabin> cabins = new HashSet<Cabin>();
private Set<Customer> customers = new HashSet<Customer>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public double getAmountPaid() {
return amountPaid;
}
public void setAmountPaid(double amount) {
amountPaid = amount;
}
@ManyToOne
@JoinColumn(name = "CRUISE_ID")
public Cruise getCruise() {
return cruise;
}
public void setCruise(Cruise cruise) {
this.cruise = cruise;
}
@ManyToMany
@JoinTable(name = "RESERVATION_CABIN", joinColumns = { @JoinColumn(name = "RESERVATION_ID") }, inverseJoinColumns = { @JoinColumn(name = "CABIN_ID") })
public Set<Cabin> getCabins() {
return cabins;
}
public void setCabins(Set<Cabin> cabins) {
this.cabins = cabins;
}
@ManyToMany
@JoinTable(name = "RESERVATION_CUSTOMER", joinColumns = { @JoinColumn(name = "RESERVATION_ID") }, inverseJoinColumns = { @JoinColumn(name = "CUSTOMER_ID") })
public Set<Customer> getCustomers() {
return customers;
}
public void setCustomers(Set<Customer> customers) {
this.customers = customers;
}
} |
Ship.java
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
| package com.titan.domain;
import javax.persistence.*;
@Entity
public class Ship implements java.io.Serializable {
private int id;
private String name;
private double tonnage;
public Ship() {
}
public Ship(String name, double tonnage) {
this.name = name;
this.tonnage = tonnage;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getTonnage() {
return tonnage;
}
public void setTonnage(double tonnage) {
this.tonnage = tonnage;
}
} |
CheckDO.java
1
2
3
4
5
6
7
8
9
10
11
| package com.titan.processpayment;
public class CheckDO implements java.io.Serializable {
public String checkBarCode;
public int checkNumber;
public CheckDO(String barCode, int number) {
this.checkBarCode = barCode;
this.checkNumber = number;
}
} |
CreditCardDO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| package com.titan.processpayment;
import java.util.Date;
public class CreditCardDO implements java.io.Serializable {
final static public String MASTER_CARD = "MASTER_CARD";
final static public String VISA = "VISA";
final static public String AMERICAN_EXPRESS = "AMERICAN_EXPRESS";
final static public String DISCOVER = "DISCOVER";
final static public String DINERS_CARD = "DINERS_CARD";
public String number;
public Date expiration;
public String type;
public CreditCardDO(String number, Date expiration, String type) {
this.number = number;
this.expiration = expiration;
this.type = type;
}
} |
PaymentException.java
1
2
3
4
5
6
7
8
9
10
11
| package com.titan.processpayment;
public class PaymentException extends java.lang.Exception {
public PaymentException() {
super();
}
public PaymentException(String msg) {
super(msg);
}
} |
ProcessPayment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| package com.titan.processpayment;
import com.titan.domain.*;
public interface ProcessPayment {
public boolean byCheck(Customer customer, CheckDO check, double amount)
throws PaymentException;
public boolean byCash(Customer customer, double amount)
throws PaymentException;
public boolean byCredit(Customer customer, CreditCardDO card, double amount)
throws PaymentException;
} |
ProcessPaymentLocal.java
1
2
3
4
5
6
| package com.titan.processpayment;
import javax.ejb.Local;
@Local
public interface ProcessPaymentLocal extends ProcessPayment {} |
ProcessPaymentRemote.java
1
2
3
4
5
6
| package com.titan.processpayment;
import javax.ejb.Remote;
@Remote
public interface ProcessPaymentRemote extends ProcessPayment{} |
ProcessPaymentBean.java