
本动手实验室介绍 Java EE 5 的基本功能。其目标是让您了解 Java EE 5 平台的关键功能。本实验室将介绍的功能包括
预计时间:120 分钟
开始之前,需要在您的计算机中安装以下软件。
在本步骤中,首先将在示例数据库中创建 CD 和 LOCATION 表。 稍后,将在这些表中创建实体。
1. 启动 Sun Java System App Server 附带的 Java DB(Derby)数据库服务器



| DROP TABLE CD; DROP TABLE LOCATION; create table "LOCATION" ( "LOCATION_ID" INTEGER NOT NULL PRIMARY KEY, "LOCATION" VARCHAR(40) ); create table "CD" ( "CD_ID" INTEGER NOT NULL PRIMARY KEY, "TITLE" VARCHAR(40), "AUTHOR" VARCHAR(40), "YEAR_CREATED" INTEGER, "LOCATION" INTEGER, "RATING" INTEGER, CONSTRAINT FK_CD_LOCATION FOREIGN KEY (LOCATION) REFERENCES LOCATION(LOCATION_ID) ); INSERT INTO LOCATION VALUES (0,'Cha Cha Cha bar'); INSERT INTO LOCATION VALUES (1,'Limon Restaurant'); INSERT INTO LOCATION VALUES (2,'Taqueria el Balazo'); INSERT INTO LOCATION VALUES (3,'Office'); INSERT INTO LOCATION VALUES (4,'Home'); INSERT INTO CD VALUES ( 0, 'Rock and Roll Aint Noise Pollution', 'AC/DC', 1980, 0, 9); INSERT INTO CD VALUES ( 1, 'Shake a Leg', 'AC/DC', 1980, 2, 6); INSERT INTO CD VALUES ( 2, 'Have a Drink on Me', 'AC/DC', 1980, 2, 5); INSERT INTO CD VALUES ( 3, 'You Shook Me All Night Long', 'AC/DC', 1980, 1, 8); INSERT INTO CD VALUES ( 4, 'Back in Black', 'AC/DC', 1980, 4, 9); INSERT INTO CD VALUES ( 5, 'Let Me Put My Love into You', 'AC/DC', 1980, 4, 9); INSERT INTO CD VALUES ( 6, 'Givin the Dog a Bone', 'AC/DC', 1980, 2, 6); INSERT INTO CD VALUES ( 7, 'What Do You Do for Money Honey', 'AC/DC', 1980, 3, 5); INSERT INTO CD VALUES ( 8, 'Shoot to Thrill', 'AC/DC', 1980, 1, 8); INSERT INTO CD VALUES ( 9, 'Battery', 'Metalica', 1986, 0, 9); INSERT INTO CD VALUES ( 10, 'Master of Puppets', 'Metalica', 1986, 0, 9); INSERT INTO CD VALUES ( 11, 'The Thing That Should Not Be', 'Metalica', 1986, 4, 9); INSERT INTO CD VALUES ( 12, 'Welcome Home (Sanitarium)', 'Metalica', 1986, 3, 9); INSERT INTO CD VALUES ( 13, 'Disposable Heroes', 'Metalica', 1986, 1, 9); INSERT INTO CD VALUES ( 14, 'Leper Messiah', 'Metalica', 1986, 4, 9); INSERT INTO CD VALUES ( 15, 'Orion [Instrumental]', 'Metalica', 1986, 3, 9); INSERT INTO CD VALUES ( 16, 'Damage, Inc.', 'Metalica', 1986, 2, 9); |













| package mypackage; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; /** * Entity class Cd * * @author sang */ @Entity @Table(name = "CD") @NamedQueries( { @NamedQuery(name = "Cd.findByCdId", query = "SELECT c FROM Cd c WHERE c.cdId = :cdId"), @NamedQuery(name = "Cd.findByTitle", query = "SELECT c FROM Cd c WHERE c.title = :title"), @NamedQuery(name = "Cd.findByAuthor", query = "SELECT c FROM Cd c WHERE c.author = :author"), @NamedQuery(name = "Cd.findByYearCreated", query = "SELECT c FROM Cd c WHERE c.yearCreated = :yearCreated"), @NamedQuery(name = "Cd.findByRating", query = "SELECT c FROM Cd c WHERE c.rating = :rating") }) public class Cd implements Serializable { @Id @Column(name = "CD_ID", nullable = false) private Integer cdId; @Column(name = "TITLE") private String title; @Column(name = "AUTHOR") private String author; @Column(name = "YEAR_CREATED") private Integer yearCreated; @Column(name = "RATING") private Integer rating; @JoinColumn(name = "LOCATION", referencedColumnName = "LOCATION_ID") @ManyToOne private Location location; /** Creates a new instance of Cd */ public Cd() { } /** * Creates a new instance of Cd with the specified values. * @param cdId the cdId of the Cd */ public Cd(Integer cdId) { this.cdId = cdId; } /** * Gets the cdId of this Cd. * @return the cdId */ public Integer getCdId() { return this.cdId; } /** * Sets the cdId of this Cd to the specified value. * @param cdId the new cdId */ public void setCdId(Integer cdId) { this.cdId = cdId; } /** * Gets the title of this Cd. * @return the title */ public String getTitle() { return this.title; } /** * Sets the title of this Cd to the specified value. * @param title the new title */ public void setTitle(String title) { this.title = title; } /** * Gets the author of this Cd. * @return the author */ public String getAuthor() { return this.author; } /** * Sets the author of this Cd to the specified value. * @param author the new author */ public void setAuthor(String author) { this.author = author; } /** * Gets the yearCreated of this Cd. * @return the yearCreated */ public Integer getYearCreated() { return this.yearCreated; } /** * Sets the yearCreated of this Cd to the specified value. * @param yearCreated the new yearCreated */ public void setYearCreated(Integer yearCreated) { this.yearCreated = yearCreated; } /** * Gets the rating of this Cd. * @return the rating */ public Integer getRating() { return this.rating; } /** * Sets the rating of this Cd to the specified value. * @param rating the new rating */ public void setRating(Integer rating) { this.rating = rating; } /** * Gets the location of this Cd. * @return the location */ public Location getLocation() { return this.location; } /** * Sets the location of this Cd to the specified value. * @param location the new location */ public void setLocation(Location location) { this.location = location; } /** * Returns a hash code value for the object. This implementation computes * a hash code value based on the id fields in this object. * @return a hash code value for this object. */ @Override public int hashCode() { int hash = 0; hash += (this.cdId != null ? this.cdId.hashCode() : 0); return hash; } /** * Determines whether another object is equal to this Cd. The result is * <code>true</code> if and only if the argument is not null and is a Cd object that * has the same id field values as this object. * @param object the reference object with which to compare * @return <code>true</code> if this object is the same as the argument; * <code>false</code> otherwise. */ @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Cd)) { return false; } Cd other = (Cd)object; if (this.cdId != other.cdId && (this.cdId == null || !this.cdId.equals(other.cdId))) return false; return true; } /** * Returns a string representation of the object. This implementation constructs * that representation based on the id fields. * @return a string representation of the object. */ @Override public String toString() { return "mypackage.Cd[cdId=" + cdId + "]"; } } |
| package mypackage; import java.io.Serializable; import java.util.Collection; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Table; /** * Entity class Location * * @author sang */ @Entity @Table(name = "LOCATION") @NamedQueries( { @NamedQuery(name = "Location.findByLocationId", query = "SELECT l FROM Location l WHERE l.locationId = :locationId"), @NamedQuery(name = "Location.findByLocation", query = "SELECT l FROM Location l WHERE l.location = :location") }) public class Location implements Serializable { @Id @Column(name = "LOCATION_ID", nullable = false) private Integer locationId; @Column(name = "LOCATION") private String location; @OneToMany(mappedBy = "location") private Collection<Cd> cdCollection; /** Creates a new instance of Location */ public Location() { } /** * Creates a new instance of Location with the specified values. * @param locationId the locationId of the Location */ public Location(Integer locationId) { this.locationId = locationId; } /** * Gets the locationId of this Location. * @return the locationId */ public Integer getLocationId() { return this.locationId; } /** * Sets the locationId of this Location to the specified value. * @param locationId the new locationId */ public void setLocationId(Integer locationId) { this.locationId = locationId; } /** * Gets the location of this Location. * @return the location */ public String getLocation() { return this.location; } /** * Sets the location of this Location to the specified value. * @param location the new location */ public void setLocation(String location) { this.location = location; } /** * Gets the cdCollection of this Location. * @return the cdCollection */ public Collection<Cd> getCdCollection() { return this.cdCollection; } /** * Sets the cdCollection of this Location to the specified value. * @param cdCollection the new cdCollection */ public void setCdCollection(Collection<Cd> cdCollection) { this.cdCollection = cdCollection; } /** * Returns a hash code value for the object. This implementation computes * a hash code value based on the id fields in this object. * @return a hash code value for this object. */ @Override public int hashCode() { int hash = 0; hash += (this.locationId != null ? this.locationId.hashCode() : 0); return hash; } /** * Determines whether another object is equal to this Location. The result is * <code>true</code> if and only if the argument is not null and is a Location object that * has the same id field values as this object. * @param object the reference object with which to compare * @return <code>true</code> if this object is the same as the argument; * <code>false</code> otherwise. */ @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Location)) { return false; } Location other = (Location)object; if (this.locationId != other.locationId && (this.locationId == null || !this.locationId.equals(other.locationId))) return false; return true; } /** * Returns a string representation of the object. This implementation constructs * that representation based on the id fields. * @return a string representation of the object. */ @Override public String toString() { return "mypackage.Location[locationId=" + locationId + "]"; } } |




| <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>List Cd</title> </head> <body> <f:view> <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/> <h1>Listing Cds</h1> <h:form> <h:commandLink action="#{cd.createSetup}" value="New Cd"/> <br> <a href="/CDLibrary/index.jsp">Back to index</a> <br> <h:outputText value="Item #{cd.firstItem + 1}..#{cd.lastItem} of #{cd.itemCount}"/> <h:commandLink action="#{cd.prev}" value="Previous #{cd.batchSize}" rendered="#{cd.firstItem >= cd.batchSize}"/> <h:commandLink action="#{cd.next}" value="Next #{cd.batchSize}" rendered="#{cd.lastItem + cd.batchSize <= cd.itemCount}"/> <h:commandLink action="#{cd.next}" value="Remaining #{cd.itemCount - cd.lastItem}" rendered="#{cd.lastItem < cd.itemCount && cd.lastItem + cd.batchSize > cd.itemCount}"/> <h:dataTable value='#{cd.cds}' var='item' border="1" cellpadding="2" cellspacing="0"> <h:column> <f:facet name="header"> <h:outputText value="CdId"/> </f:facet> <h:commandLink action="#{cd.detailSetup}" value="#{item.cdId}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Title"/> </f:facet> <h:outputText value="#{item.title}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Author"/> </f:facet> <h:outputText value="#{item.author}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="YearCreated"/> </f:facet> <h:outputText value="#{item.yearCreated}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Rating"/> </f:facet> <h:outputText value="#{item.rating}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Location"/> </f:facet> <h:outputText value="#{item.location}"/> </h:column> <h:column> <h:commandLink value="Destroy" action="#{cd.destroy}"> <f:param name="cdId" value="#{item.cdId}"/> </h:commandLink> <h:outputText value=" "/> <h:commandLink value="Edit" action="#{cd.editSetup}"> <f:param name="cdId" value="#{item.cdId}"/> </h:commandLink> </h:column> </h:dataTable> </h:form> </f:view> </body> </html> |








| package mypackage; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * Entity class Person * * @author sang */ @Entity public class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; // Add a name field to the Person class private String name; /** Creates a new instance of Person */ public Person() { } /** * Gets the id of this Person. * @return the id */ public Long getId() { return this.id; } /** * Sets the id of this Person to the specified value. * @param id the new id */ public void setId(Long id) { this.id = id; } /** * Returns a hash code value for the object. This implementation computes * a hash code value based on the id fields in this object. * @return a hash code value for this object. */ @Override public int hashCode() { int hash = 0; hash += (this.id != null ? this.id.hashCode() : 0); return hash; } /** * Determines whether another object is equal to this Person. The result is * <code>true</code> if and only if the argument is not null and is a Person object that * has the same id field values as this object. * @param object the reference object with which to compare * @return <code>true</code> if this object is the same as the argument; * <code>false</code> otherwise. */ @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Person)) { return false; } Person other = (Person)object; if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false; return true; } /** * Returns a string representation of the object. This implementation constructs * that representation based on the id fields. * @return a string representation of the object. */ @Override public String toString() { return "mypackage.Person[id=" + id + "]"; } } |



| package mypackage; /** * * @author sang */ @Entity public class Student extends Person{ /** Creates a new instance of Student */ public Student() { } } |


| package mypackage; import javax.persistence.Entity; /** * * @author sang */ @Entity public class Student extends Person{ String school; double grade; /** Creates a new instance of Student */ public Student() { } } |
















| package mypackage; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * Entity class Person * * @author sang */ @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) public class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; // Add a name field to the Person class private String name; /** Creates a new instance of Person */ public Person() { } /** * Gets the id of this Person. * @return the id */ public Long getId() { return this.id; } /** * Sets the id of this Person to the specified value. * @param id the new id */ public void setId(Long id) { this.id = id; } /** * Returns a hash code value for the object. This implementation computes * a hash code value based on the id fields in this object. * @return a hash code value for this object. */ @Override public int hashCode() { int hash = 0; hash += (this.id != null ? this.id.hashCode() : 0); return hash; } /** * Determines whether another object is equal to this Person. The result is * <code>true</code> if and only if the argument is not null and is a Person object that * has the same id field values as this object. * @param object the reference object with which to compare * @return <code>true</code> if this object is the same as the argument; * <code>false</code> otherwise. */ @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Person)) { return false; } Person other = (Person)object; if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false; return true; } /** * Returns a string representation of the object. This implementation constructs * that representation based on the id fields. * @return a string representation of the object. */ @Override public String toString() { return "mypackage.Person[id=" + id + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |




| package mypacakge; /** * * @author sang */ @Entity public class Student2 extends Person2 { /** Creates a new instance of Student2 */ public Student2() { } } |
| package mypacakge; import javax.persistence.Entity; /** * * @author sang */ @Entity public class Student2 extends Person2 { String school; double grade; /** Creates a new instance of Student2 */ public Student2() { } } |
| package mypacakge; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * Entity class Person2 * * @author sang */ @Entity @Inheritance(strategy=InheritanceType.JOINED) public class Person2 implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; /** Creates a new instance of Person2 */ public Person2() { } /** * Gets the id of this Person2. * @return the id */ public Long getId() { return this.id; } /** * Sets the id of this Person2 to the specified value. * @param id the new id */ public void setId(Long id) { this.id = id; } /** * Returns a hash code value for the object. This implementation computes * a hash code value based on the id fields in this object. * @return a hash code value for this object. */ @Override public int hashCode() { int hash = 0; hash += (this.id != null ? this.id.hashCode() : 0); return hash; } /** * Determines whether another object is equal to this Person2. The result is * <code>true</code> if and only if the argument is not null and is a Person2 object that * has the same id field values as this object. * @param object the reference object with which to compare * @return <code>true</code> if this object is the same as the argument; * <code>false</code> otherwise. */ @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Person2)) { return false; } Person2 other = (Person2)object; if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false; return true; } /** * Returns a string representation of the object. This implementation constructs * that representation based on the id fields. * @return a string representation of the object. */ @Override public String toString() { return "mypacakge.Person2[id=" + id + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |

