csm15: component-based software development · 2007. 3. 6. · the class must not be declared...

21
CSM15: Component-Based Software development Lecture 8 Java Persistence - Entity Classes

Upload: others

Post on 24-Mar-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

CSM15: Component-Based Software development

Lecture 8Java Persistence - Entity Classes

Page 2: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

What is an entity bean/class?

JEE 5 simplifies the handling of “Persistence” in Java

But what do we mean by persistence, and what is an entity?

Page 3: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Entities are things

ID LastName FirstName888 Krause Paul999 Schneider Steve777 Brush Basil007 Bond James

Customer Table

Page 4: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Entities are things

ID LastName FirstName888 Krause Paul999 Schneider Steve777 Brush Basil007 Bond James

Customer Table

ID: 888LastName:

KrauseFirstName:

Paul

ID: 999LastName: Schneider

FirstName: Steve

ID: 777LastName: Brush

FirstName: BasilID: 007

LastName: Bond

FirstName: James

Customer Beans

Page 5: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

persistence and entities

An Entity is an object-oriented representation of an underlying “persistent store”

The Persistent Store may be a:

relational database

object database

clumsy storing serialized objects to files...

Page 6: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Nouns and verbs

Entity beans are data objects - things - Nouns -

customer, booking, animal, order, ...

Session beans are processes - verbs -

membership registration, checkout, order submission, ...

Entity beans (data) will interact with Session Beans (processes)

Page 7: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Managing persistence

In JEE 5, we no longer need to use deployment descriptors to manage persistence. Instead, we:

Create a persistence unit (see next slide)

specify the data source

specify the entity manager

The Container does the rest

Page 8: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

What’s a Persistence unit?

A persistence unit defines the entity classes that are managed by the entity manager

Persistence units are defined by the persistence.xml configuration file.

Each persistence unit must be identified with a name that is unique to the persistence unit's scope.

Page 9: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Start a new project

Choose File > New Project (Ctrl-Shift-N).

Select Web Application from the Web category.

Name the project CustomerBook

Set the server to Sun Java System Application Server

Set the Java EE Version to Java EE 5, click Finish

Page 10: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

create a persistence unit

Using New/File, create a new Persistence Unit using the wizard

You can use the default name for the persistence unit

Use the default, Top Link, for the Persistence Provider

the entity manager is in TopLink Essential.jar

Select jdbc/sample (this will connect to the Java DB)

Page 11: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

What do you see?

Page 12: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

The Entity manager

The Entity Manager:

Creates and removes persistent entity instances

Finds entities by the entity’s primary key

Allows queries to be run on entities

Will manage the storage of an entity’s data to its associated database

Page 13: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

entities

An entity is a lightweight persistence domain object. Typically an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.

The persistent state of an entity is represented either through persistent fields or persistent properties. These fields or properties use object/relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store.

Sun Java EE 5 Tutorial

Page 14: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

requirements for entity classes

The class must be annotated with the javax.persistence.Entity annotation.

The class must have a public or protected, no-argument constructor. The class may have other constructors.

The class must not be declared final. No methods or persistent instance variables must be declared final.

Persistent instance variables must be declared private, protected, or package-private, and can only be accessed directly by the entity class's methods. Clients should access the entity's state through accessor or business methods.

Page 15: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

entity classes from a DB

Tools > Java DB Database > Start Java DB Server

New/File & select Entity Class from the Persistence category

Select the CUSTOMER table and click Add

Note DISCOUNT_CODE is also added

Click Next, then specify ejb as the package and Finish

Page 16: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Customer.java

Page 17: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Customer.java

Page 18: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Customer.java

Page 19: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Customer.java

Page 20: CSM15: Component-Based Software development · 2007. 3. 6. · The class must not be declared final. No methods or persistent instance variables must be declared final. Persistent

Customer.java