|
Working with Entity bean using JPA |
|
to persist the data in the database. The
Database Model is called the Domain Model
that represents the persistence objects or
entities in the database. An entity may be a
person, place or a thing about which you want
to store the data in the database. A rich domain
model includes the characteristics of all the
object-oriented behavior like inheritance,
polymorphism and many more.
Lets read about the some common annotations
that are used to develop a JPA application.
Primary Key Generation:
In EJB 3.0, a primary key is used with @Id
annotation. Depending upon the application
requirement, Id annotation can be used with
different primary key generation strategies
defined by GeneratorType enum. The
GeneratorTypes are TABLE, SEQUENCE,
IDENTITY, AUTO, and NONE.
Declaring an entity bean:
Every bound persistent POJO class is an
entity bean and is declared using the @Entity
annotation (at the class level). @Entity declares
the class as an entity bean (i.e. a persistent
POJO class), which tells the EJB3 container that
this class needs to be mapped to a relational
database table.
Defining the table:
@Table is set at the class level; it allows you
to define the table, catalog, and schema
names for your entity bean mapping. If no
@Table is defined the default values are used,
that is the unqualified class name of the
entity.
Here is the sample code using of these
annotations:
@Entity
@Table(name=”book”)
@SequenceGenerator(name =
“book_sequence”, sequenceName =
“book_id_seq”)
public class Book implements Serializable {
Long empid; |
|
@Id
@GeneratedValue
public Long getId()
{ return id; }
public void setId(Long id)
{ this.id = id; }
}
The @Table defines the table name. Each
instance of the entity bean represents a row
of data in the table. Each column in the table
corresponds to a data attribute in the entity
bean. The @SequenceGenerator defines a
sequence generator. A sequence is a database
feature. It returns the next Integer or Long
value each time it is called.
@Id declares the identifier property of this
entity bean. @GeneratedValue annotation
indicates that the server automatically
generates the primary key value.
Managing Entities:
The entity manager manages entities. The
entity manager is represented by
javax.persistence.EntityManager instances.
Each EntityManager instance is associated with
a persistence context.
A persistence context defines the scope under
which particular entity instances are created,
persisted, and removed. It is a set of managed
entity instances that exist in a particular data
store. The EntityManager interface defines the
methods that are used to interact with the
persistence context.
The EntityManager Interface:
The EntityManager API creates and removes
persistent entity instances, finds entities by
the entity’s primary key, and allows queries to
be run on entities. To obtain an EntityManager
instance, inject the entity manager into the
application component:
@PersistenceContext
EntityManager em; |
|
Feb 2008 | Java Jazz Up | 8 |
|
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
Download PDF |
|
|
|
|
|
|
|
|
|