# BaseEntity

## Purpose

`BaseEntity` appears to be the shared persistence base class for the PetClinic domain model. It centralizes the identifier field and the common “new vs. persisted” check so that subclasses do not need to repeat JPA identity mapping or lifecycle logic.

In practice, this class gives every entity a consistent primary-key contract while keeping the entity hierarchy small and focused on domain-specific state.

## Design

This is a mapped superclass in a JPA entity hierarchy. It is not itself a concrete domain object; instead, it provides reusable infrastructure for entities that need a generated database identifier and a simple way to determine whether the instance has been persisted yet.

The design is intentionally minimal:

- `@MappedSuperclass` makes the fields and mapping available to subclasses.
- `@Id` and `@GeneratedValue(strategy = GenerationType.IDENTITY)` define the persistence identity strategy.
- `Serializable` suggests the entity base may be transferred across persistence or web boundaries.

## Key Methods

### `Integer getId()`
Returns the current identifier value for the entity.

- **Parameters:** none
- **Returns:** the `Integer` primary key value, or `null` if the entity has not been assigned an ID yet
- **Side effects:** none

This is the standard accessor used by frameworks, repositories, and calling code that need to inspect the database identity of an entity instance.

### `void setId(Integer id)`
Assigns an identifier value to the entity.

- **Parameters:** `id` - the identifier to store
- **Returns:** nothing
- **Side effects:** mutates the entity’s `id` field

This method is primarily used by JPA and related infrastructure. The `IDENTITY` generation strategy usually means application code should not set this manually for new entities, but the setter remains available for persistence frameworks and object mapping tools.

### `boolean isNew()`
Reports whether the entity appears to be unsaved.

- **Parameters:** none
- **Returns:** `true` when `this.id == null`, otherwise `false`
- **Side effects:** none

This is the most semantically important method in the class. It appears to be the canonical “new entity” check used by repository or UI logic that needs to distinguish between a freshly constructed instance and one that has already been persisted.

## Relationships

`BaseEntity` sits at the root of the PetClinic domain inheritance tree and is extended by several important model classes.

- **`NamedEntity` extends `BaseEntity`**
  - This appears to add shared name-related behavior while inheriting the identifier contract.
- **`Person` extends `BaseEntity`**
  - This appears to model person-like records that need a generated primary key.
- **`Visit` extends `BaseEntity`**
  - This appears to represent a persisted visit record with the same base identity behavior.

The class has no outbound references beyond standard JPA and Java serialization annotations/interfaces, so it acts as a reusable foundation rather than a consumer of other application types.

```mermaid
flowchart TD
BaseEntity["BaseEntity"]
NamedEntity["NamedEntity"]
Person["Person"]
Visit["Visit"]
PersistenceLayer["JPA and repository layer"]
NamedEntity --> BaseEntity
Person --> BaseEntity
Visit --> BaseEntity
BaseEntity --> PersistenceLayer
```

## Usage Example

A typical usage pattern is to create a subclass, populate its domain fields, and let persistence assign the identifier:

```java
Person person = new Person();
boolean fresh = person.isNew(); // true before persistence

// repository.save(person) or entityManager.persist(person)
// after persistence, the generated ID becomes available
Integer id = person.getId();
```

For code that works generically with entities, `isNew()` provides a simple branching point:

```java
if (entity.isNew()) {
    // create path
} else {
    // update path
}
```

## Notes for Developers

- The `id` field is generated with `GenerationType.IDENTITY`, so the database is responsible for assigning the value.
- `isNew()` depends solely on `id == null`. If an entity instance has a manually assigned identifier, `isNew()` will treat it as existing.
- The class is mutable because of `setId(Integer)`, but in normal application code the identifier should usually be considered framework-managed.
- Because this is a superclass used by multiple entities, any change to the identity contract can affect a large portion of the domain model.
- The class is extremely small, but it is foundational: its behavior influences persistence semantics for every subclass.
