Association Classes
You normally don't create an entity class for a join table.
- The
film_actortable, for example, is made of keys which are essential for the@ManyToManyrelationship, but building an entity for this table would not add any additional value to our codebase.
However, sometimes the join table contains additional data that does not belong with either entity that participates in the many-to-many relationship, but instead belongs to the association itself.
-
The
inventory_itemtable, for example, contains a column calledmedia_conditionthat wouldn't make sense to place in Film or Store. -
If this is the case, you can create an entity for the join table to model the additional data.
-
This new class is sometimes called an association class.
Drill¶
Create an
enumcalled MediaCondition with values ofNew,Used,Damaged,Lost, andNA.Create a new entity called InventoryItem which is mapped to the
inventory_itemtable. Add fields for
- id
- mediaCondition
+-----------------+------------------------------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+------------------------------------------+------+-----+-------------------+-----------------------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | film_id | int(10) unsigned | NO | MUL | NULL | | | store_id | smallint(5) unsigned | NO | MUL | NULL | | | media_condition | enum('New','Used','Damaged','Lost','NA') | YES | | NULL | | | last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-----------------+------------------------------------------+------+-----+-------------------+-----------------------------+Build a JUnit test for the InventoryItem entity to ensure it is mapped correctly.