In EJB 3.x, an entity is a POJO (Plain Old Java Object) persistent domain object that represents a database table row. As an entity is a Java class, create a Java class in the jboss-ejb3-ejb
subproject of the jboss-ejb3 project. Select File | New. In the New window, select Java | Class and click on Next:
Select/specify jboss-ejb3/jboss-ejb3-ejb/src/main/java
as the Java Source folder, org.jboss.ejb3.model
as the Package, and Catalog
as the class Name. Click on Finish:
Similarly, add Java classes for the Edition.java
, Section.java
, and Article.java
entities, as shown in the following Project Explorer:
Next, we develop the EJB 3.x entities. A JPA persistence provider is required for the EJB entities, and we will use the Hibernate persistence provider. The Hibernate persistence provider has some peculiarities that need to be mentioned, as follows:
java.util.List, java.util.Collection
properties annotated with @org.hibernate.annotations.CollectionOfElements
@OneToMany
or @ManyToMany
associations not annotated with @org.hibernate.annotations.IndexColumn
We will develop the Catalog, Edition, Section, and Article class with one-to-many relationship between the Catalog and Edition class, the Edition and Section class, and the Section and Article class, as shown in the following UML class diagram:
Annotate the Catalog entity class with the @Entity annotation and the @Table annotation. If the @Table annotation is not used, then the entity name is used as the table name by default. In the @Table annotation, specify the table name as CATALOG and uniqueConstraints, using the @UniqueConstraint annotation for the id
column. Specify the named queries as findCatalogAll, which selects all Catalog and findCatalogByJournal entities. This selects a Catalog entity by Journal, using the @NamedQueries and @NamedQuery annotations:
@Entity @Table(name = "CATALOG", uniqueConstraints = @UniqueConstraint(columnNames = "ID")) @NamedQueries({ @NamedQuery(name="findCatalogAll", query="SELECT c FROM Catalog c"), @NamedQuery(name="findCatalogByJournal", query="SELECT c FROM Catalog c WHERE c.journal = :journal") }) public class Catalog implements Serializable { }
Specify the no-argument
constructor, which is required in an entity class. The Catalog entity class implements the Serializable interface to serialize a cache-enabled entity to a cache when persisted to a database. To associate a version number with a serializable class for a serialization runtime, specify a serialVersionUID variable. Declare String
variables for id and journal bean properties and for a collection of Set<Edition> type, as the Catalog entity has a bi-directional one-to-many association to Edition. The collection is chosen as Set for the reason mentioned earlier. Hibernate does not support more than one EAGER association of the java.util.List type. Add get
/set
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 sequence
type, 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 as MySQL database supports auto increment of primary key column values by generating a sequence, we have set the generation strategy to GenerationType.AUTO
.
Specify the bi-directional one-to-many association to Edition
using the @OneToMany
annotation. The mappedBy
element is specified on the non-owning side of the relationship, which is the Catalog
entity. The cascade
element is set to ALL
. Cascading is used to cascade database table operations to associated tables. The fetch
element is set to EAGER
. With EAGER
fetching the associated entity, collection is immediately fetched when an entity is retrieved:
// bi-directional many-to-one association to Edition @OneToMany(mappedBy = "catalog", targetEntity=org.jboss.ejb3.model.Edition.class, cascade = { CascadeType.ALL }, fetch = FetchType.EAGER) public Set<Edition> getEditions() { return this.editions; } }
As mentioned earlier, associations marked with mappedBy
must not specify @JoinTable
or @JoinColumn
. The get and set methods for the Edition
collection are also specified. The Catalog.java
entity class is available in the code download for the chapter at http://www.packtpub.com/support.
Next, develop the entity class for the EDITION
database table: Edition.java
. Specify the @Entity
, @Table
, @Id
, @Column
, and @GeneratedValue
annotations, as discussed for the Catalog
entity. Specify the findEditionAll
and findEditionByEdition
named queries to find Edition collections. Specify the bean properties and associated get/set methods for id
and edition
. Also, specify the one-to-many association to the Section
entity using a collection of the Set
type. The bi-directional many-to-one association to the Catalog
relationship is specified using the @ManyToOne
annotation, and with cascade of type PERSIST
, MERGE
, and REFRESH
. The Edition
entity is the owning side of the relationship. Using the @JoinTable
annotation, a join table is included on the owning side to initiate cascade operations. The join columns are specified using the @JoinColumn
annotation. The Edition.java
entity class is available in the code download for the chapter.
Develop the entity class for the SECTION
table: Section.java
. Specify the findSectionAll
and findSectionBySectionName
named queries to find Section
entities. Specify the id
and sectionname
bean properties. Specify the bi-directional many-to-one association to Edition
using the @ManyToOne
annotation and the bi-directional one-to-many association to Article
using @OneToMany
. The @JoinTable
and @JoinColumn
are specified only for the @ManyToOne
association for which Section
is the owning side. The Section.java
entity class is available in the code download for the chapter.
Specify the entity class for the ARTICLE
table: Article.java
. The Article
entity is mapped to the ARTICLE
database table using the @TABLE
annotation. Add the findArticleAll
and findArticleByTitle
named queries to find Article
entities. Specify id
and sectionname
bean properties and the associated get
/set
methods. The Article
entity is the owning side of the bi-directional many-to-one association to Section
. Therefore, the @JoinTable
and @JoinColumn
are specified. The Article.java
class is available in the code downloaded for the chapter.