Collection Backed by a Join Table

In case you want to persist and query an Entity by its related Value Objects, you have the choice to persist the Value Objects as Entities. In terms of the Domain, those objects would still be Value Objects, but we'll need to give them an id and set them up with a one-to-many/one-to-one relation with the owner, a real Entity. To summarize, your ORM handles the collection of Value Objects as Entities, but in your Domain, they're still treated as Value Objects.

The main idea behind the Join Table strategy is to create a table that connects the owner Entity and its Value Objects. Let's see a database representation:

CREATE TABLE ` historical_products` (
`id` char( 36) COLLATE utf8mb4_unicode_ci NOT NULL,
`name` varchar( 255) COLLATE utf8mb4_unicode_ci NOT NULL,
`price_amount` int( 11 ) NOT NULL,
`price_currency` char( 3) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

The historical_products table will look the same as products. Remember that HistoricalProduct extends Product Entity in order to easily show how to deal with persisting a collection. A new prices table is now required in order to persist all the different Money Value Objects that a Product Entity can handle:

CREATE TABLE `prices`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` int(11) NOT NULL,
`currency` char(3) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Finally, a table that relates products and prices is needed:

CREATE TABLE `products_prices` (
`product_id` char( 36) COLLATE utf8mb4_unicode_ci NOT NULL,
`price_id` int( 11 ) NOT NULL,
PRIMARY KEY (`product_id`, `price_id`),
UNIQUE KEY `UNIQ_62F8E673D614C7E7` (`price_id`),
KEY `IDX_62F8E6734584665A` (`product_id`),
CONSTRAINT `FK_62F8E6734584665A` FOREIGN KEY (`product_id`)
REFERENCES `historical_products` (`id`),
CONSTRAINT `FK_62F8E673D614C7E7` FOREIGN KEY (`price_id`)
REFERENCES `prices`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset