



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of enterprise java beans (ejb) entity beans, discussing the problems with the old model and introducing the new pojo-based model and object-relational mapping (orm). It covers the benefits and limitations of orm, the role of entitymanager, managed vs. Unmanaged entities, and persistence context.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Ye Wu
http://www.ise.gmu.edu/~wuye
SWE 645
Component-based Software Development
2007-2-
© Dr. Ye Wu
2
Old EJB follows component model•Performance•Compatibility•Complexity
-^
New EJB Entity bean is a plain Java-basedmodel (POJO – plain old java object- persistence)Entity Manager – create, query, remove, update
2007-2-
© Dr. Ye Wu
3
New EJB Entity bean utilized the O/R mapping idea
Table
Object
-^
Attr
-^
Attr
-^
Attr
Col
Col
Col
2007-2-
© Dr. Ye Wu
4
There is a natural mapping between objects anddatabase tables;
-^
Most create/update/delete oprations work withindividual objects
-^
Domain object can be cached easily, and may are“read only”
2007-2-
© Dr. Ye Wu
5
There is no easy way to map objects to databasetables;
-^
Data access is prodomainant through large datasets, batch operation, or aggregation
-^
Cached domain object is not easy, mostly “write”
2007-2-
© Dr. Ye Wu
6
import
javax.persistence.*;
@Entity public class
Customer implements Serializable{
private long
id;
private
String fristName;
private
String lastName;
public
String getFristName() {
return
fristName;}
public void
setFristName(String fristName) {
this
.fristName = fristName;}
@Id public long
getId() {
return
id; }
public void
setId(
long
id) {
this
.id = id; }
public
String getLastName() {
return
lastName;}
public void
setLastName(String lastName) {
this
.lastName = lastName;}
2007-2-
© Dr. Ye Wu
7
@Entity @Table(name = "Customer") public class
Customer implements Serializable{
private long
id;
private
String fristName;
private
String lastName;
@Column(name="firstName", length=255, nullable=
false
)
public
String getFristName() {
return
fristName;}
public void
setFristName(String fristName) {
this
.fristName = fristName;}
@Id @Column(name="firstName", nullable=
false
, columnDefinition="long")
public long
getId() {
return
id; }
public void
setId(
long
id) {
this
.id = id; }
public
String getLastName() {
return
lastName;}
public void
setLastName(String lastName) {
this
.lastName = lastName;}
2007-2-
© Dr. Ye Wu
8
Persist POJO into database
-^
Manage O/R mapping
-^
Creating queries,
-^
Finding objects
-^
Synchronizing objects
-^
Inserting objects into the database
-^
Caching
-^
Transaction support
2007-2-
© Dr. Ye Wu
13
-^
A basic DataSource implementation
Standard connection object that are not pooled or used in adistributed transaction.
-^
A DataSource class that supports connection poolingConnection objects that participate in connection pooling and will be
recycled.
-^
A DataSource class that supports distributed transactionConnections objects that can be used in a distributed transaction.
2007-2-
© Dr. Ye Wu
14
<local-tx-datasource>
<jndi-name>swe645</jndi-name><connection- url>jdbc:oracle:thin:@apollo.ite.gmu.edu:1521:ite10g</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class><user-name>class_account</user-name>
2007-2-
© Dr. Ye Wu
15
<local-tx-datasource>
<jndi-name>swe645</jndi-name><connection-url>
jdbc:mysql://localhost/swe645</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class><user-name>swe645</user-name>
2007-2-
© Dr. Ye Wu
16
<persistence-unit name="swe645"><jta-data-source>java:/swe645</jta-data-source>
2007-2-
© Dr. Ye Wu
17
@Stateless public class
StatelessEJB
implements
StatelessRemote { @PersistenceContext(unitName="swe645") private
EntityManager entityManger;
2007-2-
© Dr. Ye Wu
18
Persisting EntitiesCustomer cust = new Customer();entityManager.persist(cust)
Finding Entities•^
Find()
-^
getReference()
-^
Query()
2007-2-
© Dr. Ye Wu
19
Update EntitiesCustomer cust = entityManager.find(….)cust.setXXXX()
Merging EntitiesentityManager.merge(obj)
2007-2-
© Dr. Ye Wu
20
Remove EntitiesCustomer cust = entityManager.find(….)cust.remove(cust)
refresh()entityManager.merge(obj)