// person entity import java.io.Serializable; import java.util.*; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratorType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="dbo.Person") public class Person implements Serializable { static final long serialVersionUID = 1000102; // member declaration private int id; private String firstName; private String lastName; private String companyName; private Collection stays; // constructors public Person() { } public Person(String firstName, String lastName, String companyName) { this.firstName = firstName; this.lastName = lastName; this.companyName = companyName; } // properties public String getCompanyName() { return companyName; } public void setCompanyName(String companyName) { this.companyName = companyName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @Id(generate=GeneratorType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } // relationships @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="person") public Collection getStays() { return this.stays; } public void setStays(List stays) { this.stays = stays; } // business logic public void addStay(Date startDate, Date endDate, String vessel, String authoriser, String comments) { Stay stay = new Stay(this, startDate, endDate, vessel, authoriser, comments); addStay(stay); } public void addStay(Stay stay) { Collection stays = getStays(); if (stays == null) stays = new ArrayList(); stays.add(stay); this.stays = stays; } } // stay entity import java.io.Serializable; import java.sql.Blob; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratorType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="dbo.Stay") public class Stay implements Serializable { static final long serialVersionUID = 1000101; // member declaration private int id; private Person person; private Date startDate; private Date endDate; private String vessel; private String authoriser; private String comments; private Collection visits; // constructors public Stay() { } public Stay(int id) { this.id = id; } public Stay(Person person, Date startDate, Date endDate, String vessel, String authoriser, String comments) { this.authoriser = authoriser; this.endDate = endDate; this.person = person; this.startDate = startDate; this.vessel = vessel; this.comments = comments; } // properties public String getAuthoriser() { return authoriser; } public void setAuthoriser(String authoriser) { this.authoriser = authoriser; } public String getComments() { return comments; } public void setComments(String comments) { this.comments = comments; } public Date getEndDate() { return endDate; } public void setEndDate(Date endDate) { this.endDate = endDate; } @Id(generate=GeneratorType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY) @JoinColumn(name="person") public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } public String getVessel() { return vessel; } public void setVessel(String vessel) { this.vessel = vessel; } // relationships @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY) @JoinColumn(name="stay") public Collection getVisits() { return visits; } public void setVisits(Collection visits) { this.visits = visits; } public void addVisit(Date timeEntered, int visitType, String reason, Blob image) { Visit visit = new Visit(); if (visit.getId() == null) visit.setId(new VisitPK()); visit.getId().setStay(this); visit.getId().setTimeEntered(timeEntered); visit.setVisitType(visitType); visit.setReason(reason); visit.setImage(image); addVisit(visit); } public void addVisit(Visit visit) { Collection visits = getVisits(); if (visits == null) visits = new ArrayList(); visits.add(visit); this.visits = visits; } } // session bean (partial) import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.zinifex.wharfcam.ejb.entity.Person; import org.zinifex.wharfcam.ejb.entity.Stay; import org.zinifex.wharfcam.ejb.entity.Visit; import org.zinifex.wharfcam.ejb.interfaces.WharfMgr; @Stateless public class WharfMgrBean implements WharfMgr { @PersistenceContext(unitName="wharfCamEntity") private EntityManager manager; public Person findPerson(int id) { return (Person) manager.find(Person.class, id); } @SuppressWarnings(value={"unchecked"}) public List findAllPersons() { Query q = manager.createQuery("from Person p "); return (List) q.getResultList(); } @SuppressWarnings(value={"unchecked"}) public List findPersons(String searchString) { Query q = manager.createQuery("from Person p where" + searchString); return (List) q.getResultList(); } } // persistence xml project java:/testds