|
My use case is to enforce integrity.
Example e-commerce:
Products can optional be grouped by a variant group (drop down menu for variant filters).
Instead of using two tables such as Product and ProductVariantGroup a simple approach is to use a single table.
Entity
Product
{
boolean isVarGroup;
Product varGroup; }
<many-to-one name="varGroup" class="Product" lazy="false" fetch="select" />
<property name="isVarGroup"/>
Database
In case a product is also a variant group its reference must point to itself. If not it must point to a valid variant group.
-- check isVarGroup
IF (NEW.isVarGroup IS TRUE AND NEW.varGroup != NEW.id) THEN
-- set to itself
SET NEW.varGroup = NEW.id; -- SIGNAL SQLSTATE '23000' ...
-- cannot reference itself if not a variant group
ELSEIF (NEW.varGroup = NEW.id) THEN
SET NEW.varGroup = NULL; -- SIGNAL SQLSTATE '23000' ...
END IF;
There are maybe other use cases.
|