• No results found

Preparing the EJB 3.0 Project

4 Java Persistence

4.4 Development of an Example Application

4.5.2 Preparing the EJB 3.0 Project

If persistence is an issue, transactionality also comes into play, of course.

Entities are normally generated, changed, or deleted within transac-tions, that is, their status is reconciled with the database. The law of gravity still applies to entities: They are and remain ordinary Java objects, which do not suddenly change their values due to database pro-cesses.

JPA annotations Unlike EJB 2.1, the mapping of persistent objects, their attributes, and relations to database structures has now been standardized and is, as usual with Java EE 5, expressed using annotations. The principle of con-figuration by exception also applies here: If nothing is annotated, default values are used, which are sufficient for a quick start, meaning that the developer only needs to deal with the annotations when the default val-ues no longer meet the increasing requirements and complexity of the application. As neat as this concept seems, its difficulty is also obvious.

Unfortunately, the application developer does not see the default values, which does not release her or him from knowing them or the behavior achieved with them.

In this example application, the most important annotations (for rela-tionships) are presented, partly to make them explicit for explanatory reasons, and also because the naming of the persistent objects and attributes should not be linked to the names of the database tables and their columns by naming convention.

4.5.2 Preparing the EJB 3.0 Project

Persistence unit Before programming the entities of the project management scenario, you still need to define the persistence unit. The persistence unit defines the set of all entities that can be addressed using the entity manager at runtime in an application. It must also be mapped in the same database.

All of this is stored in a file called persistence.xml. However, the file ulti-mately remains largely empty.

Declaration of the runtime data source

In addition to the name of the persistence unit, you declare the Java Transaction API (JTA) data source, which defines, among other things, the database schema in which the entities of the persistence unit are per-sisted. The set of all entities is declared here using configuration by exception, that is, all classes of this project annotated as entities are part

Programming with Enterprise JavaBeans 3.0/Java Persistence API 4.5

of the persistence unit (as long as all the entities that are not listed in the file persistence.xml are not explicitly excluded).

You also need to enable the EJB 3.0 project to see the existing database tables, so that it can elegantly browse them when mapping the entities to the table structures at design time.

Declaration of the design time data-base connection

As explained in Section 4.4.2, Implementing the Example Scenario in EJB 3.0 and JPA, it will only be possible to use the table declarations stored in the Dictionary project in a future version of the SAP NetWeaver Com-position Environment. However, you can easily bypass this weakness with a small trick. You have already deployed the archive of the Dictio-nary project, that is, physically created the tables. When deploying the Dictionary archive that contains the table declarations, Open JDBC uses the system data source of the server to physically create the described tables in its database schema. All table description metadata of the Java Dictionary is also stored in a separate table there. We will now excep-tionally bypass the Open JDBC layer for read-only purposes and config-ure the EJB 3.0 project to work with the tables already created by Open JDBC:

1. Switch to the Java Persistence Perspective, and use the Database Explorer to create a new database connection. This is to point to the server’s database schema (system database), in which you previously set up the tables created with the Java Dictionary through deployment (see Definition and Deployment of the Tables in Section 4.4.2).

2. In the DatabaseExplorer view, choose Connections, right-click, and choose NewConnections….

3. You can enter any name for the database connection. The database must be the system database of the server itself. Choose the JDBC driver class and its path and URL appropriate for your installation.

You also need to enter the database user ID and its password. Figure 4.6 shows this dialog step.

Note

This database connection only exists in the development environment. This is not a data source that is available to the application at runtime.

Java Persistence

4

Table 4.1 shows the respective values for a default installation with the default instance name (CE1) and SAP MaxDB as the server’s database.

You do not need to define the filter offered in the second dialog step.5

Figure 4.6 Including the Server Database Schema at Design Time

5 This parameter is important if tables are not created with the Java Dictionary, but with Dali using database-specific scripts to create the tables from the entities.

Connection Property Value

Connection Name CE1

Database Manager5 MAXDB 7.6

Database CE1

JDBC Driver Class com.sap.dbtech.jdbc.DriverSapDB Class location C:\sapdb\programs\runtime\jar\sapdbc.jar Connection URL jdbc:sapdb://localhost/CE1?timeout=0

Table 4.1 Parameters of the Design Time Database Connection

Programming with Enterprise JavaBeans 3.0/Java Persistence API 4.5

For EJB 3.0 projects, the declaration of the database connection that you have just created for mapping the entities at design time and the gener-ation of persistence.xml with the persistence informgener-ation for runtime are combined into a single step in the SAP NetWeaver Developer Studio:

1. In the EJB 3.0 project, right-click and choose JavaPersistence폷Add JavaPersistence….

2. You now need to select the CE1 database connection and the SAPCE1DB schema, and to assign the name for the persistence unit (PersistenceSamplesPU) (Figure 4.7).

3. You can find the generated persistence.xml in the META INF directory of the EJB 3.0 project, and you need to add the declaration of the JTA data source (PROJECTMANAGEMENT_DS) used at runtime to it (Figure 4.8).

User ID SAPCE1DB

Password <yourpassword>

Figure 4.7 Java Persistence Properties for the EJB 3.0 Project Connection Property Value

Table 4.1 Parameters of the Design Time Database Connection (cont.)

Java Persistence