ORM: Specifying a value equality constraint against two referenced entities
I am trying to model a concept using object-role-modelling, and I can't find the necessary constraint type. I'm wondering if it exists.
Here are three facts:
- Commodity must be of one CommodityCategory
- EntityDescriptor must be of of CommodityCategory
- EntityDescripto开发者_JAVA技巧r may be for one Commodity
This is straightforward to model:
But here's the constraint:
- If an EntityDescriptor is for a Commodity, the CommodityCategory referenced by the Commodity must equal the CommodityCategory referenced by the EntityDescriptor
For example, suppose we had these commodities.
*--------------------*------------* | CommodityCategory | Commodity | *--------------------*------------* | Fuel | Gas | | Fuel | Petrol | | Food | Sugar | *--------------------*------------*
These are legal
*------------------*-------------------*-----------* | EntityDescriptor | CommodityCategory | Commodity | *------------------*-------------------*-----------* | 1 | Fuel | | | 2 | Fuel | Gas | | 3 | Food | | | 4 | Food | Sugar | *------------------*-------------------*-----------*
But this is illegal
*------------------*-------------------*-----------* | EntityDescriptor | CommodityCategory | Commodity | *------------------*-------------------*-----------* | 5 | Food | Petrol | *------------------*-------------------*-----------*
I looked at the Equality constraint, but that is about the existence of the relationship, not the actual values in the relationship.
Is there something I can use to model this constraint?
Written in CQL see the ActiveFacts home page, you need a subset constraint like this:
some EntityDescriptor references some Commodity
only if that EntityDescriptor is for some CommodityCategory and that Commodity is of that CommodityCategory;
Note that this becomes more fluent if you include a reading in each direction.
In NORMA, you need a subset constraint that has two role pairs:
The subset pair is the two roles of "EntityDescriptor references Commodity". The superset pair is the role of EntityDescriptor in "EntityDescriptor is for CommodityCategory, and the role of Commodity in "Commodity is of CommodityCategory".
Note that the first role of each pair is played by the same type (EntityDescriptor), and likewise with the second role of each pair (Commodity). It's also possible to use compatible subtype/supertypes, but the types must be compatible in this way.
An equality constraint is like two subset constraints, one running in each direction. It always requires that at least one reference exists whenever an EntityDescriptor is for some CommodityCategory and that Commodity is of that CommodityCategory, as well as vice versa.
can't we add a subset constraint between roles of is of and is for, so every commodity to category is a subset of entity descriptor to category
the table go like this : ED (EntityDescriptor), CC (CommodityCategory), CM (Commodity)
ED CC <---> CC CM ED <---> CM CC
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 5 5 // error, cause CC doesn't have 5,5 to ED
4 4 4 4 4 4 4
5 4 4 5 4 5 4 // ok, cause CC have 4 to 5 on CC-ED
6 4 6 3 // error, cause ED-CC doesn't have 6,3
so can just we see that CC has two roles that to ED (r1) and to CM (r2), that r2 is subset of r1. so i think the commodity doesn't have directly constraint role to ED but the constraint applied through CC.
If you want to enforce using a database I'd recommend before insert/update triggers to prevent associating an EntityDescription and Commodity that do not match.
If you are thinking of using code I'd recommend studying the Specification Pattern. Assume a Commodity, EntityDescriptor and CommodityDescriptor classes. CommodityDescriptor would be part of the composition of the other two classes. Commodity would including a Specification, say MatchingCommidityDescriptionSpecification (yes it's verbose) as part of its composition. Then when Commodity.setEntityDescription(EntityDescription entityDescriptor) is called it validates against the Specification by comparing the Commodity and EntityDescriptor's CommodityDescriptor values.
精彩评论