2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier Entity Relationships in theorder Application Self-Referential Relationships One-to-Many Relationship Mapped to Overlapping Primary and Foreign Keys Primary Keys in theorder Application Entity Mapped to More Than One Database Table Cascade Operations in theorder Application BLOB and CLOB Database Types in theorder Application Temporal Types in theorder Application Managing theorder Application's Entities Building and Running theorder Application Creating the Database Tables in NetBeans IDE Creating the Database Tables Using Ant Building, Packaging, Deploying, and Runningorder In NetBeans IDE Building, Packaging, Deploying, and Runningorder Using Ant Relationships in theroster Application The Many-To-Many Relationship inroster Entity Inheritance in theroster Application Automatic Table Generation in theroster Application Building and Running theroster Application Building, Packaging, Deploying, and Runningroster in NetBeans IDE 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | Theroster ApplicationTheroster application maintains the team rosters for players in recreational sports leagues.The application has four components: Java Persistence API entities (Player,Team, andLeague), astateful session bean (RequestBean), an application client (RosterClient), and three helper classes (PlayerDetails,TeamDetails, andLeagueDetails). Functionally,roster is similar to theorder application described earlier in this chapterwith three new features thatorder does not have: many-to-many relationships, entity inheritance,and automatic table creation at deploytime. Relationships in theroster ApplicationA recreational sports system has the following relationships:
Inroster this is reflected by the following relationships between thePlayer,Team,andLeague entities:
The Many-To-Many Relationship inrosterThe many-to-many relationship betweenPlayer andTeam is specified by using the@ManyToMany annotation. InTeam.java, the@ManyToMany annotation decorates thegetPlayers method: @ManyToMany@JoinTable( name="EJB_ROSTER_TEAM_PLAYER", joinColumns= @JoinColumn(name="TEAM_ID", referencedColumnName="ID"), inverseJoinColumns= @JoinColumn(name="PLAYER_ID", referencedColumnName="ID"))public Collection<Player> getPlayers() { return players;}The@JoinTable annotation is used to specify a table in the database thatwill associate player IDs with team IDs. The entity that specifies the@JoinTableis the owner of the relationship, so in this case theTeam entityis the owner of the relationship with thePlayer entity. Becauseroster uses automatictable creation at deploytime, the container will create a join table in thedatabase namedEJB_ROSTER_TEAM_PLAYER. Player is the inverse, or non-owning side of the relationship withTeam. Asone-to-one and many-to-one relationships, the non-owning side is marked by themappedBy element inthe relationship annotation. Because the relationship betweenPlayer andTeam is bidirectional,the choice of which entity is the owner of the relationship is arbitrary. InPlayer.java, the@ManyToMany annotation decorates thegetTeams method: @ManyToMany(mappedBy="players")public Collection<Team> getTeams() { return teams;}Entity Inheritance in theroster ApplicationTheroster application demonstrates how to use entity inheritance, as described inEntity Inheritance. TheLeague entity inroster is an abstract entity with two concrete subclasses:SummerLeague andWinterLeague. BecauseLeague is an abstract class it cannot be instantiated: ...@Entity@Table(name = "EJB_ROSTER_LEAGUE")public abstract class League implements java.io.Serializable {...}Instead,SummerLeague orWinterLeague are used by clients when creating a league.SummerLeagueandWinterLeague inherit the persistent properties defined inLeague, and only adda constructor that verifies that the sport parameter matches the type of sportallowed in that seasonal league. For example, here is theSummerLeague entity: ...@Entitypublic class SummerLeague extends League implements java.io.Serializable { /** Creates a new instance of SummerLeague */ public SummerLeague() { } public SummerLeague(String id, String name, String sport) throws IncorrectSportException { this.id = id; this.name = name; if (sport.equalsIgnoreCase("swimming") || sport.equalsIgnoreCase("soccer") || sport.equalsIgnoreCase("basketball") || sport.equalsIgnoreCase("baseball")) { this.sport = sport; } else { throw new IncorrectSportException( "Sport is not a summer sport."); } }}The roster application uses the default mapping strategy ofInheritanceType.SINGLE_TABLE, so the@Inheritance annotation is not required. If you wanted to use a different mappingstrategy, decorateLeague with@Inheritance and specify the mapping strategy in thestrategy element: @Entity@Inheritance(strategy=JOINED)@Table(name="EJB_ROSTER_LEAGUE")public abstract class League implements java.io.Serializable { ...}roster uses the default discriminator column name, so the@DiscriminatorColumn annotation is notrequired. Because you are using automatic table generation inroster the Persistence provider willcreate a discriminator column in theEJB_ROSTER_LEAGUE table calledDTYPE, which willstore the name of the inherited entity used to create the league. Ifyou want to use a different name for the discriminator column, decorateLeaguewith@DiscriminatorColumn and set thename element: @Entity@DiscriminatorColumn(name="DISCRIMINATOR")@Table(name="EJB_ROSTER_LEAGUE")public abstract class League implements java.io.Serializable { ...}Automatic Table Generation in theroster ApplicationAt deploytime the Application Server will automatically drop and create the database tablesused byroster. This is done by setting thetoplink.ddl-generation property todrop-and-create-tablesinpersistence.xml. <?xml version="1.0" encoding="UTF-8"?><persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"version="1.0"> <persistence-unit name="em" transaction-type="JTA"> <jta-data-source>jdbc/__default</jta-data-source> <properties> <property name="toplink.ddl-generation" value="drop-and-create-tables"/> </properties> </persistence-unit></persistence> This feature is specific to the Java Persistence API provider used by theApplication Server, and is non-portable across Java EE servers. Automatic table creation isuseful for development purposes, however, and thetoplink.ddl-generation property may be removedfrompersistence.xml when preparing the application for production use, or when deploying toother Java EE servers. Building and Running theroster ApplicationThis section describes how to build, package, deploy, and run therosterapplication. You can do this using either NetBeans IDE or Ant. Building, Packaging, Deploying, and Runningroster in NetBeans IDEFollow these instructions to build, package, deploy, and run theroster exampleto your Application Server instance using NetBeans IDE.
You will see the following partial output from the application client in theOutput tab: List all players in team T2:P6 Ian Carlyle goalkeeper 555.0P7 Rebecca Struthers midfielder 777.0P8 Anne Anderson forward 65.0P9 Jan Wesley defender 100.0P10 Terry Smithson midfielder 100.0List all teams in league L1:T1 Honey Bees VisaliaT2 Gophers MantecaT5 Crows OrlandList all defenders:P2 Alice Smith defender 505.0P5 Barney Bold defender 100.0P9 Jan Wesley defender 100.0P22 Janice Walker defender 857.0P25 Frank Fletcher defender 399.0... Building, Packaging, Deploying, and Runningroster Using AntTo build the application components ofroster, enter the following command: ant This runs thedefault task, which compiles the source files and packages theapplication into an EAR file located attut-install/examples/ejb/roster/dist/roster.ear. To deploy the EAR, make sure the Application Server is started, thenenter the following command: ant deploy The build system will check to see if the Java DB databaseserver is running and start it if it is not running, then deployroster.ear. The Application Server will then drop and create the database tables duringdeployment, as specified inpersistence.xml. Afterroster.ear is deployed, a client JAR,rosterClient.jar, is retrieved. This containsthe application client. To run the application client, enter the following command: ant run You will see the output, which begins: [echo] running application client container.[exec] List all players in team T2:[exec] P6 Ian Carlyle goalkeeper 555.0[exec] P7 Rebecca Struthers midfielder 777.0[exec] P8 Anne Anderson forward 65.0[exec] P9 Jan Wesley defender 100.0[exec] P10 Terry Smithson midfielder 100.0[exec] List all teams in league L1:[exec] T1 Honey Bees Visalia[exec] T2 Gophers Manteca[exec] T5 Crows Orland[exec] List all defenders:[exec] P2 Alice Smith defender 505.0[exec] P5 Barney Bold defender 100.0[exec] P9 Jan Wesley defender 100.0[exec] P22 Janice Walker defender 857.0[exec] P25 Frank Fletcher defender 399.0... Theall TaskAs a convenience, theall task will build, package, deploy, and run theapplication. To do this, enter the following command: ant all UndeployingorderTo undeployroster.ear, enter the following command: ant undeploy Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |