Controlling the Object-Relationship Mapping (ORM) process

Object-Relationship Mapping (ORM) is concerned with persisting an object to a database. There exist a number of annotations which provide information to guide the mapping process. The uses of these annotations are illustrated in this recipe.

Getting ready

When we persist an entity it is sometimes desirable to exercise more control over which table in a database should be used. In addition, we may want to specify the field names to use within the table. ORM allows us to control these factors by using the @Table and @Column annotations. The use of these annotations will be demonstrated by expanding upon the previous recipe and augmenting the EntityBeanApplication application.

How to do it...

The PartsBean developed so far provides little guidance in terms of which database table to use or how the fields of the object are mapped to columns of a table. The first thing we will look at is how to specify the database to be used for the PartsBean.

This database is specified in the persistence.xml file which we created using an IDE wizard in the Creating an Entity Facade recipe. This file is found in the EJB module and should appear as shown below. The presentence unit EntityBeanApplication-ejbPU is declared as and is associated with the database jdbc/__default.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="EntityBeanApplication-ejbPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider </provider>
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>

Having specified the database, we will probably want to specify the table to use. If we don't explicitly choose a table, one is automatically provided by the server. To specify the table we use the @Table annotation. We could have also modified the orm.xml file to achieve the same effect. Modify the PartsBean to add the @Table annotation. The name attribute of the annotation specifies the database table to use.

@Entity
@Table(name="PARTS")
public class PartsBean implements Serializable {

Next, add a series of @Column annotations to the entity as shown here:

@Column(name="NAME")
private String name;
@Column(name="PARTNUMBER")
private int partNumber;
@Column(name="WEIGHT")
private float weight;
@Column(name="QUANTITY")
private int quantity;

The persistence of the PartsBean is performed as illustrated in the Using the EntityManager recipe. The use of these annotations does not require modification of the PartsServlet.

How it works...

Notice for the EntityBeanApplication, the source was specified in the<jta-data-source> element. The value, jdbc/__default, specified the database to use. The available databases are a function of the Java EE server and its configuration. This application was developed using Glassfish. The jdbc/__default database was one of the available options. Using a different server or adding a different data store to a server will provide you with other options. Also, note that the database table's catalog and schema can also be specified using the catalog and schema elements.

Specific fields of an entity are mapped to columns of a table using the @Column annotation. The name element of the annotation determines the name to be used for the table column. In this example we used the same name as the field and in all caps. However, you can always use fields and naming conventions appropriate for your application.

There's more...

There are other elements of the @Column annotation which control the configuration of a column including but not limited to:

  • unique Specifies whether the column represents a unique key
  • nullable The column can or cannot hold a null value
  • length - The length of a string column
  • precision The precision of a decimal column

We will not discuss these options here.

So far the fields of the PartsBean have been simple Java primitives or the String class. If we need to use a collection, such as a Map or Set of basic types, then the @ElementCollection annotation can be used.

To illustrate this annotation, add an enumeration called ColorEnumeration to the class. Next, declare a Set of this type called colors. This field is intended to allow us to specify the available part's colors. Also add a get and set method for colors.

public enum ColorEnumeration {RED, GREEN, BLUE}
@ElementCollection
private Set<ColorEnumeration> colors;
public Set<ColorEnumeration> getColors() {
return colors;
}
public void setColors(Set<ColorEnumeration> colors) {
this.colors = colors; }

The @ElementCollection annotation has two attributes: targetClass used with collections of basic or embeddable classes; and fetch which specifies either a LAZY or EAGER retrieval of the field. The fetch attribute controls when the field is retrieved. With a lazy retrieval, the value for the field is not retrieved until it is needed. An eager fetch will retrieve the value immediately. If a generic class is used, as was the case with the set of colors above, the targetClass attribute is not required.

Modify the PartsServlet to create a set of objects and then populate the set with various colors.

parts = new PartsBean();
...
HashSet<PartsBean.ColorEnumeration> colors = new HashSet<PartsBean.ColorEnumeration>();
colors.add(PartsBean.ColorEnumeration.RED);
colors.add(PartsBean.ColorEnumeration.BLUE);
parts.setColors(colors);
partsFacade.create(parts);

See also

The Using the EntityManager recipe illustrates the complete process to persist a PartsBean.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.74.231