• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Objects live in RAM—when the program stops, they’re gone. To keep data permanently (across restarts, for other applications to use), objects must be saved to persistent storage (database, filesystem). What are the ways to persist Java objects?

Core Idea

There are two main ways to persist Java objects:

  1. Serialization: Convert the entire object to a byte stream (blob) and save it. Simple but not queryable.
  2. Object-Relational Mapping (ORM): Decompose the object into fields and store them as rows/columns in a relational database. Queryable and debuggable.

Entity Beans use ORM (not serialization) for persistence.

How It Works

MechanismHowProsCons
SerializationObjectOutputStream → byte blobSimple, built-inNot queryable, hard to debug
ORMMap fields → table columnsQueryable, standard SQLMore complex setup
Object DBStore as native objectNo mapping neededNot widely adopted

Visual Explanation

Persistence cluster_ser Serialization cluster_orm Object-Relational Mapping Obj BankAccount Object (accountID, owner, balance) Blob Byte Blob (unreadable) Obj->Blob ObjectOutputStream Table BankAccount Table | ID | Owner | Balance | Obj->Table JDBC/ORM (accountID→col1, owner→col2, etc.)

Key Properties

  • Entity Beans use ORM: They map to relational database tables (not serialization)
  • EJB doesn’t dictate ORM tool: You can use JDBC (BMP) or container-managed (CMP)
  • Modern tools: Hibernate, TopLink, JDO—automate ORM (popular in EJB 3.x+)
  • Queryable: Unlike serialization, ORM lets you run SQL queries like “find all accounts with balance > $1000”

Connections

Edge Cases & Gotchas

  • Serialization is easier but limiting: Good for simple caching, bad for business data
  • ORM has a learning curve: Mapping complex object relationships to tables requires skill
  • JDO (Java Data Objects): Alternative to EJB entity beans for persistence (portable across databases)