2. Understanding Java Platform, Enterprise Edition
3. Creating Your First Java EE Application
4. Creating Your Second Web Application
Create the Web Application Project
Creating the Java Persistence API Entity
Create the FirstcupUser Entity Class
Add Properties to the FirstcupUser Entity
Create the DukesBirthdayBean Enterprise Bean Class
Add a Logger Instance to DukesBirthdayBean.java
Add a Business Method to DukesBirthdayBean that Gets the Average Age Difference of firstcup Users
Add a Business Method for Calculating the Age Difference Between Duke and the User
Configuring the Resource Bundle in the Configuration File
Creating the DukesBDay Managed Bean Class
Add an Enterprise Bean Reference
Get the Age Difference from the DukesBirthdayBean Enterprise Bean
Resource Libraries in firstcup
The inputDate Composite Component
Create the inputDate Composite Component
Set the Welcome File in the web.xml Deployment Descriptor
Add the Form to greeting.xhtml
Building, Packaging, Deploying, and Running the firstcup Web Application
Build, Package, and Deploy the firstcup Web Application
The Java Persistence API allows you to create and use Java programming language classes that represent data in a database table. A Java Persistence API entity is a lightweight, persistent Java programming language object that represents data in a data store. Entities can be created, modified, and removed from the data store by calling the operations of the Java Persistence API entity manager. Entities, or the data encapsulated by the persistent fields or properties of a entity, can be queried using the Java Persistence Query Language (JPQL), a language similar to SQL that operates on entities.
In firstcup, there is a single entity that defines one query.
The FirstcupUser Java Persistence API entity represents a particular firstcup user, and stores the user's birthday and the difference in age between the user and Duke. FirstcupUser also defines a Java Persistence API query used to calculate the average age difference of all users.
You should now see the FirstcupUser.java file inside the firstcup.entity package in the Projects tab. The FirstcupUser.java file should also be open in the editor pane.
Create the FirstcupUser entity's two properties: birthday, of type java.util.Calendar; and ageDifference, of type int.
The birthday property must be annotated with the javax.persistence.Temporal annotation to mark the property as a date field in the underlying database table. All persistent fields or properties of type java.util.Calendar or java.util.Date must be annotated with @Temporal.
@Temporal(javax.persistence.TemporalType.DATE) protected Calendar birthday; protected int ageDifference;
public Calendar getBirthday() {
return birthday;
}
public void setBirthday(Calendar birthday) {
this.birthday = birthday;
}
public int getAgeDifference() {
return ageDifference;
}
public void setAgeDifference(int ageDifference) {
this.ageDifference = ageDifference;
}Create two constructors for FirstcupUser: one that takes no arguments, and another that takes two arguments.
public FirstcupUser() {
}
public FirstcupUser(Date date, int ageDifference) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
this.setBirthday(cal);
this.setAgeDifference(ageDifference);
}Add a JPQL named query to the FirstcupUser entity that returns the average age difference of all firstcup users.
This query uses the AVG aggregate function to return the average of all the values of the ageDifference property of the FirstcupUser entities.
@NamedQuery(name="findAverageAgeDifferenceOfAllFirstcupUsers", query="SELECT AVG(u.ageDifference) FROM FirstcupUser u")
The @NamedQuery annotation appears just before the class definition of the entity, and has two required attributes: name, with the unique name for this query; and query, the JPQL query definition.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Legal Notices
Scripting on this page tracks web page traffic, but does not change the content in any way.