In this section, we will create an EJB 3.0 entity bean for a domain model. Create the org.jboss.springmvc.model.Catalog
entity bean class, which is just a plain old Java object (POJO). To create a Java class, select File | New | Other, and in New, select Java | Class, as shown in the following screenshot:
In the New Java Class wizard, select Source folder as jboss-springmvc/src/main/java
, specify Package as org.jboss.springmvc.model
, and specify the class Name as Catalog
, as shown in the following screenshot:
The org.jboss.springmvc.model.Catalog.java
class gets added to the project, as shown in Project Explorer in the following screenshot:
Remove the Member.java
class and the other Member*
classes from the project to get the directory structure, as shown in the following screenshot:
Annotate the Catalog
entity class with the @Entity
annotation and the @Table
annotation. In the @Table
annotation, specify the table name as CATALOG
and uniqueConstraints
using the @UniqueConstraint
annotation for the ID
column. Specify the no-argument constructor, which is required in an entity class. The entity class implements the Serializable
interface to serialize a cache-enabled entity bean to a cache when persisted to a database. To associate a version number with a serializable class by serialization runtime, specify a serialVersionUID
variable. Declare variables for the bean properties: id
, journal
, publisher
, edition
, title
, and author
. Add getter/setter methods for the bean properties. The @Id
annotation specifies the identifier property. The @Column
annotation specifies the column name associated with the property. The nullable
element is set to false
as the primary key cannot be null
.
If we were using the Oracle database, we would have specified the primary key generator to be of the type sequence using the @SequenceGenerator
annotation. The generation strategy is specified with the @GeneratedValue
annotation. For the Oracle database, the generation strategy would be strategy=GenerationType.SEQUENCE
, but because MySQL database supports auto-increment of primary key column values by generating a sequence, we have set the generation strategy to GenerationType.AUTO
. The Catalog.java
entity class is listed in the following code snippet:
package org.jboss.springmvc.model; import java.io.Serializable; import javax.persistence.*; /** * The persistent class for the CATALOG database table. * */ @Entity @Table(name = "CATALOG", uniqueConstraints = @UniqueConstraint(columnNames = "ID")) public class Catalog implements Serializable { private static final long serialVersionUID = 1L; private int id; private String journal; private String publisher; private String edition; private String title; private String author; public Catalog() { } @Id @Column(name = "ID", nullable = false) @GeneratedValue(strategy = GenerationType.AUTO) public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getJournal() { return this.journal; } public void setJournal(String journal) { this.journal = journal; } public String getPublisher() { return this.publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public String getEdition() { return this.edition; } public void setEdition(String edition) { this.edition = edition; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return this.author; } public void setAuthor(String author) { this.author = author; } }