7.7.4. Entities

Entity beans for database entities. Looks like a class designated as an Entity class (EJB 3.0 and above) or an object with a remote interface and a factory with a home interface (EJB 2.1 and below).

public interface AccountHome extends javax.ejb.EJBHome
{
  public Account create (String firstName, String lastName, double initialBalance)
    throws RemoteException, CreateException;
  public Account create (String accountNumber, double initialBalance)
    throws RemoteException, CreateException, LowInitialBalanceException;
  public Account createLargeAccount (String firstname, String lastname, double initialBalance)
    throws RemoteException, CreateException;
  ...
  public Account findByPrimaryKey (String AccountNumber)
    throws RemoteException, FinderException;
  ...
}

public interface EJBHome extends Remote
{
  public void remove (Object primaryKey) throws RemoteException, RemoveException;
}
@Entity public class AnEntity
{
  // With field based access fields are persistent by default.
  private int someField;
  private String someOtherField;

  // Relationships among entities must be annotated.
  @OneToMany private Collection<AnotherEntity> relatedEntities;

  // Every entity must have a primary key.
  @Id private long aKeyField;

  // Field that is not persistent
  @Transient private String aTransientString;

  // Obligatory constructor with no arguments
  public AnEntity () { ... }

  // Additional business methods ...
  public void myMethodOne (int iArgument) { ... }
  public int myMethodTwo (Object oArgument) { ... }
}
@Entity public class AnEntity
{
  // With property based access fields are not persistent themselves.
  private int someTransientField;
  private String someOtherTransientField;

  // Relationships among entities must be annotated.
  private Collection<AnotherEntity> relatedEntities;
  @OneToMany public Collection<AnotherEntity> getRelatedEntities ()
  {
    return (relatedEntities);
  }
  public void setRelatedEntities (Collection<AnotherEntity> entityCollection)
  {
    relatedEntities = entityCollection;
  }

  // Getter and setter methods for primary key.
  private long aKeyField;
  @Id Long getAKeyField () { return (aKeyField); }
  public void setAKeyField (Long aKeyField) { this.aKeyField = aKeyField; }

  // Obligatory constructor with no arguments
  public AnEntity () { ... }

  // Additional business methods ...
  public void myMethodOne (int iArgument) { ... }
  public int myMethodTwo (Object oArgument) { ... }
}
// Home interface naming service lookup
Context initialContext = new InitialContext ();
AccountHome accountHome =
  (AccountHome) initialContext.lookup
    (“java:comp/env/ejb/accounts”);

// Creation
accountHome.createLargeAccount (...);

// Location
accountHome.findByPrimaryKey (...);
public interface EntityManager
{
  void persist (Object entity);
  void refresh (Object entity);
  void remove (Object entity);

  void detach (Object entity);
  <T> T merge (T entity);

  void lock (Object entity, LockModeType lockMode);

  // Find by primary key
  <T> T find (Class<T> entityClass, Object primaryKey);

  // Find by primary key and return lazy reference
  <T> T getReference (Class<T> entityClass, Object primaryKey);

  // Clear persistence context and detach all entities
  void clear ();

  // Check whether persistence context contains managed entity
  boolean contains (Object entity);

  // Synchronize persistence context with database
  // Flush mode governs automatic synchronization
  // upon query execution or upon commit
  void flush ();
  FlushModeType getFlushMode ();
  void setFlushMode (FlushModeType flushMode);

  Query createQuery (String ejbqlString);
  Query createNamedQuery (String name);
  Query createNativeQuery (String sqlString);
  ...
}
public interface Query
{
  // Execute a query that returns a result list
  List getResultList ();
  // Execute a query that returns a single result
  Object getSingleResult();
  // Execute an update query
  int executeUpdate ();

  // Methods used to fetch results step by step
  Query setMaxResults (int maxResult);
  Query setFirstResult (int startPosition);

  // Bind a parameter in a query
  Query setParameter (String name, Object value);
  Query setParameter (String name, Date value, TemporalType temporalType);
  Query setParameter (String name, Calendar value, TemporalType temporalType);
  Query setParameter (int position, Object value);
  Query setParameter (int position, Date value, TemporalType temporalType);
  Query setParameter (int position, Calendar value, TemporalType temporalType);
}

Persistence of an entity bean. (EJB 3.0 and above) Instance variables are made persistent, can be fields or properties, types are limited roughly to primitive types, serializable types, collections. Primary key variable annotated as an Id. Entity manager provides finder methods. (EJB 2.1 and below) Container managed persistence generates accessor methods for fields described by abstract persistence schema in the deployment descriptor. Bean managed persistence requires implementation of manual database access and ejbLoad and ejbStore methods. Home interface provides finder methods.