Enterprise Java Beans + Facade (Help Please)?
I'm trying to learn Enterprise JavaBeans but cant find out how to get the Facade Pattern to work Between Client applications and the two entity beans. I can get 1 Enterprise JavaBean to work with a client but I cant work out where to start when getting 2 Enterprise JavaBeans to work.
I have this at the moment:-
Java Files
(Entity Bean One)
- MedicationBean.java
- MedicationHomeRemote.java
- MedicationRemote.java
(Entity Bean Two)
- PatientsBean.java
- PatientsHomeRemote.java
- PatientsRemote.java
XML Files
- ejb-jar.xml
- jboss.xml
This is what Im going to try and do in the end: -
Display Medication Records (Entity Bean One)
Medication001 - Patient004
Medication002 - Patient006
Medication003 - Patient005
Medication003 - Patient003
Medication003 - Patient001
Medication004 - Patient002
Display Patients Records (Entity Bean Two)
Patient001 - Medication003
Patient002 - Medication004
Patient003 - Medication003
Patient004 - Medication001
Patient005 - Medication003
Patient006 - Medication002
[1203 byte] By [
o0MattE0oa] at [2007-10-2 10:16:08]

> I'm trying to learn Enterprise JavaBeans but cant
> find out how to get the Facade Pattern to work
> Between Client applications and the two entity beans.
> I can get 1 Enterprise JavaBean to work with a
> a client but I cant work out where to start when
> getting 2 Enterprise JavaBeans to work.
I'm not following you here. You've got two entity EJBs that should participate in a single use case for a client, right?
I think the point is that clients shouldn't be dealing directly with entity EJBs that way. The Facade pattern in this case is the service interface that the client contacts. Let the service implementation do the lookup on the required entity beans, perform some work, and send a response back to the client.
I don't see how the stuff you posted is pertinent. What does "not work" look like?
%
This is what I have working: -
MedicationBean.java
MedicationHomeRemote.java
MedicationRemote.java
And what it dose is print this out: -
pk 1 (hidden)
Medication Name:Medication 01
Medication Serial Number: 12345
I can change my ejb-jar to print off the Patients but how do I get them to?
1. Print off both
2. Work together so I can place them together (example: Patient09 has this Medication01)
Sounds like you're trying to model a relationship between two objects - a Patient has a collection of Medications. Is that it?
In that case, you need a 1:m relationship between Patient and Medication. A Patient will have a Collection data member that holds Medication instances. You'll model the relationship in the CMP descriptor.
%
Yep thats what Im looking for!
But are there any examples on this as im still learning, and this the first time I started using EJB's?
Not shore where to go next
(also Recomed any good books I have Java how to Programe 6th edt. but dont think it covers EJB's)
This is what I have so far:-
MedicationBean
package Server;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
public abstract class MedicationBean implements javax.ejb.EntityBean
{
public Integer ejbCreate(Integer id) throws CreateException
{
this.setId(id);
return null;
}
public void ejbPostCreate(Integer id) {}
public abstract void setId(Integer id);
public abstract Integer getId();
public abstract void setMedicationName(String medicationName);
public abstract String getMedicationName();
public abstract void setMedicationSerialNumber(int SerialNo);
public abstract int getMedicationSerialNumber();
public void setEntityContext(EntityContext ctx) {}
public void unsetEntityContext() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbLoad() {}
public void ejbStore() {}
public void ejbRemove() {}
}
MedicationRemote
package Server;
import java.rmi.RemoteException;
public interface MedicationRemote extends javax.ejb.EJBObject
{
public String getMedicationName() throws RemoteException;
public void setMedicationName(String medicationName) throws RemoteException;
public int getMedicationSerialNumber() throws RemoteException;
public void setMedicationSerialNumber(int medicationSerialNumber) throws RemoteException;
}
MedicationHomeRomote
package Server;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface MedicationHomeRomote extends javax.ejb.EJBHome
{
public MedicationRemote create(Integer id)
throws CreateException, RemoteException;
public MedicationRemote findByPrimaryKey(Integer pk)
throws FinderException, RemoteException;
}
PatientsBean >> Almost Same as MedicationBean
PatientsHomeRemote >> Almost Same as MedicationHomeRemote
PatientsRemote >> Almost Same as MedicationRemote
> Yep thats what Im looking for!
> But are there any examples on this as im still
> learning, and this the first time I started using
> EJB's?
Yes, lots of examples. If you Google for "EJB CMP" you'll find stuff like this:
http://java.sun.com/developer/technicalArticles/ebeans/EJB20CMP/
>
> (also Recomed any good books I have Java how to
> Programe 6th edt. but dont think it covers EJB's)
I'm not a big fan of the Deitel & Deitel books anymore. I'm told that the Head First books are good.
I'd recommend that you try making this work as Plain Old Java Objects (POJOs) first, before attempting it with EJBs. You might understand what the problems are better.
Here's what I'd start with if I had a Patient class with a one-to-many relationship with Medication:
In Patient.java:
package model;
import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;
public class Patient implements Serializable
{
private Long id;
private String name;
private Map medications;
public Patient(Long id, String name, Map medications)
{
this.id = id;
this.name = name;
this.medications = new HashMap(medications);
}
public Long getId() { return id; }
public void setId(Long newId) { id = newId; }
public String getName() { return name; }
public void setName(String newName) { name = newName; }
public Map getMedications() { return medications; }
public void setMedications(Map newMedications) { medications = newMedications; }
public void addMedication(Medication medication)
{
medications.put(medication.getId(), medication);
}
public void removeMedication(Medication medication)
{
medications.remove(medication.getId());
}
public boolean hasMedication(Medication medication)
{
return medications.containsKey(medication.getId());
}
// better write equals, hashCode, and toString
}
In Medication.java:
package model;
import java.io.Serializable;
public class Medication implements Serializable
{
private Long id;
private String name;
private Patient patient;
public Medication(Long id, String name, Patient patient)
{
this.id = id;
this.name = name;
this.patient = patient;
}
public Long getId() { return id; }
public void setId(Long newId) { id = newId; }
public String getName() { return name; }
public void setName(String newName) { name = newName; }
public Patient getPatient() { return patient; }
public void setPatient(Patient newPatient) { patient = newPatient; }
// better write equals, hashCode, and toString
}
You'll need database access for these. Start with a DAO interface for Patient:
package persistence;
import java.util.List;
public interface PatientDAO
{
public Patient find(Long id);
public List findAll();
public void saveOrUpdate(Patient p);
public void delete(Patient p);
}
You'll learn a lot by writing the DAO implementation in JDBC. It'll be a good way to figure out what the EJB CMP is doing for you behind the scenes.
%
