Understanding EJB Entity Beans: Old vs. New Model and ORM - Prof. Ye Wu, Study notes of Engineering

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

Pre 2010

Uploaded on 02/12/2009

koofers-user-51e
koofers-user-51e 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
EJB Entity Beans
Ye Wu
http://www.ise.gmu.edu/~wuye
SWE 645
Component-based Software Development
2007-2-27 © Dr. Ye Wu 2
Problems in Old EJB Entity Bean Model
Old EJB follows component model
•Performance
•Compatibility
•Complexity
New EJB Entity bean is a plain Java-based
model (POJO – plain old java object- persistence)
Entity Manager – create, query, remove, update
2007-2-27 © Dr. Ye Wu 3
New Entity Bean Model – O/R Mapping
New EJB Entity bean utilized the O/R mapping idea
Table
Object
Attr1
Attr2
Attr3
Col1 Col2 Col3
2007-2-27 © Dr. Ye Wu 4
When to Use ORM
There is a natural mapping between objects and
database tables;
Most create/update/delete oprations work with
individual objects
Domain object can be cached easily, and may are
“read only”
pf3
pf4
pf5

Partial preview of the text

Download Understanding EJB Entity Beans: Old vs. New Model and ORM - Prof. Ye Wu and more Study notes Engineering in PDF only on Docsity!

EJB Entity Beans

Ye Wu

http://www.ise.gmu.edu/~wuye

SWE 645

Component-based Software Development

2007-2-

© Dr. Ye Wu

2

Problems in Old EJB Entity Bean Model

•^

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 Entity Bean Model – O/R Mapping

New EJB Entity bean utilized the O/R mapping idea

Table

Object

-^

Attr

-^

Attr

-^

Attr

Col

Col

Col

2007-2-

© Dr. Ye Wu

4

When to Use ORM

•^

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

When NOT to Use ORM

•^

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

Bean Class

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

Bean Class

@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

EntityManager

•^

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

JDBC Data Source

-^

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

Define Oracle Data Source

<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>C1a55ac7 <min-pool-size>10</min-pool-size><max-pool-size>20</max-pool-size><blocking-timeout-millis>5000</blocking-timeout-millis><idle-timeout-minutes>15</idle-timeout-minutes> </local-tx-datasource>

2007-2-

© Dr. Ye Wu

15

Define MySQL data source

<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>swe645 </local-tx-datasource>

2007-2-

© Dr. Ye Wu

16

Persistence.xml for J2EE Applications

<persistence-unit name="swe645"><jta-data-source>java:/swe645</jta-data-source>

</persistence-unit>

2007-2-

© Dr. Ye Wu

17

Obtain Persistence Context

@Stateless public class

StatelessEJB

implements

StatelessRemote { @PersistenceContext(unitName="swe645") private

EntityManager entityManger;

2007-2-

© Dr. Ye Wu

18

EntityManger Services

Persisting EntitiesCustomer cust = new Customer();entityManager.persist(cust)

Finding Entities•^

Find()

-^

getReference()

-^

Query()

2007-2-

© Dr. Ye Wu

19

EntityManger Services

Update EntitiesCustomer cust = entityManager.find(….)cust.setXXXX()

Merging EntitiesentityManager.merge(obj)

2007-2-

© Dr. Ye Wu

20

EntityManger Services

Remove EntitiesCustomer cust = entityManager.find(….)cust.remove(cust)

refresh()entityManager.merge(obj)