April 17, 2007, 6:18 a.m.
posted by hashspark
Exercise 6.1: Basic Property MappingsThis exercise shows you an example of the @Temporal, @Lob, and @Enumerated mapping types discussed in Chapter 6, as well as an example of an autogenerated primary key. Start Up JBossIf you already have JBoss running, there is no reason to restart it. Otherwise, start it up as instructed in Workbook 1. Initialize the DatabaseThe database tables will be created when Exercise 6.1 is deployed to JBoss. If you have problems running this example, shut down JBoss and run the clean.db Ant task. Build and Deploy the Example ProgramsPerform the following steps:
As in the last exercise, titan.jar is rebuilt, copied to the JBoss deploy directory, and redeployed by the application server. Examine the Customer EntityThis exercise maps one Customer entity with some of the basic mapping types shown in Chapter 6 of the EJB book. Customer.java
package com.titan.domain;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name="CUSTOMER_TABLE")
public class Customer implements java.io.Serializable
{
The @javax.persistence.Table annotation is used to set the relational table to which the Customer entity maps.
private int id;
private String lastName;
private String firstName;
private CustomerType customerType;
private Date timeCreated = new Date( );
private JPEG picture;
@Id
@GeneratedValue
@Column(name="CUST_ID")
public int getId( )
{
return id;
}
public void setId(int pk)
{
id = pk;
}
The @javax.persistence.GeneratedValue annotation tells the persistence provider that we want the id property of the Customer entity to be autogenerated when EntityManager.persist( ) is executed. The @javax.persistence.Column annotation is used to specify the name of the column to which the id property maps in CUSTOMER_TABLE .
public String getLastName( ) { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public String getFirstName( ) { return firstName; }
public void setFirstName(String lastName) { this.firstName = firstName; }
Although the lastName and firstName properties are not annotated, the persistence provider will assume that they are persistent. Use the @javax.persistence.Transient annotation if you want to mark a property as nonpersistent.
@Enumerated(EnumType.STRING)
public CustomerType getCustomerType( ) { return customerType; }
public void setCustomerType(CustomerType type) { customerType = type; }
com.titan.domain.CustomerType is a Java enum . The @javax.persistence.Enumerated annotation is used here to tell the persistence provider that we want to store it as a string in the database.
@Temporal(TemporalType.TIME)
public Date getTimeCreated( ) { return timeCreated; }
A java.util.Date can be stored in different ways in the database, so here we specify that we want the database to store it as a time SQL type, using the @javax.persistence.Temporal annotation to do this.
@Lob @Basic(fetch=FetchType.LAZY)
public JPEG getPicture( ) { return picture; }
public void setPicture(JPEG jpeg) { picture = jpeg; }
}
Our last property is of type com.titan.domain.JPEG . Let's pretend that this class is actually an in-memory representation of a .jpeg image. If you open up the JPEG.java implementation, you'll see that it is just a dummy class. By marking the Customer's picture property as an @Lob , the persistence provider will see that com.titan.domain.JPEG is a serializable Java object and will map it to an SQL Blob type in the database. When you have this exercise built and deployed, bring up the Hypersonic SQL Database Manager program, as discussed in Workbook 2. Figure W-11 shows how the annotation mappings have affected the schema of our autogenerated database. Figure W-11. CUSTOMER_TABLE![]() Examine TravelAgentBeantravelAgentBean acts as a façade around the persistence unit that holds the Customer entity. Let's take a look at it. TravelAgentBean.java
package com.titan.travelagent;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.titan.domain.Customer;
@Stateless
public class TravelAgentBean implements TravelAgentRemote
{
@PersistenceContext(unitName="titan") private EntityManager manager;
public int createCustomer(Customer cust)
{
manager.persist(cust);
return cust.getId( );
}
public Customer findCustomer(int pKey)
{
return manager.find(Customer.class, pKey);
}
}
The Customer entity is allocated on the client and is sent via the createCustomer( ) method to the server. After the manager.persist( ) method has executed, the primary key for the Customer entity has been generated and is returned by the method invocation. The findCustomer( ) method simply finds and returns the entity based on its primary key. Examine the ClientThe client's only role is to allocate a Customer instance and invoke the createCustomer( ) and findCustomer( ) methods of travelAgentBean. There's not much else to add here. Run the ClientRun the Client application by invoking ant run.client at the command prompt. Remember to set your JBOSS_HOME and PATH environment variables. This is the output:
run.client:
[java] Bill
[java] Burke
[java] BIG_SPENDAH
|
- Comment
