β οΈ This blog post was created with AI assistance to help explain Uniface concepts in simple terms.
π What Are Unrelated Entities?
In Uniface, entities are considered unrelated when there's no relationship defined between them in your application model. Think of it like two separate database tables that don't have a foreign key connecting them β they exist independently. π
π« Why You Shouldn't Nest Them
While Uniface allows you to nest unrelated entities in components, it's not recommended. When you do this anyway, Uniface creates what's called an ad hoc relationship β basically a temporary connection that's not formally defined in your model. The compiler will give you a warning when this happens. β οΈ
π‘ Example Scenario
Imagine you have two entities:
-
CUSTOMER
β contains customer information -
PRODUCT
β contains product information
If these entities don't have a relationship defined (like through an ORDER entity), they are unrelated. Nesting PRODUCT inside CUSTOMER would be unusual and not recommended. π€
βοΈ What Happens When You Nest Them Anyway?
1. Data Retrieval Issues π₯
When you retrieve data from the outer entity (the parent), Uniface will load all occurrences of the inner entity (the child). This can lead to performance problems and incorrect data display.
Solution: Use the read u_where
statement in the read trigger of the inner entity to filter which records should be shown. This forces a relationship between the entities.
2. Multiple Outer Entities Problem π
If you have multiple outer entities with unrelated inner entities, Uniface only retrieves data for the first outer entity (reading from left to right, top to bottom). For the others, you need to use the retrieve/e
statement explicitly.
3. Delete Operation Confusion β
Here's a tricky part: when you try to delete an occurrence of an outer entity that contains inner entity occurrences, Uniface makes two assumptions:
- The entities are actually related π
- The referential integrity constraint is set to restricted
This means Uniface will return an error message preventing the delete operation! π
β Best Practice
Instead of nesting unrelated entities, keep them separate in your component structure. This makes your code clearer and avoids unexpected behavior. If entities truly need to be connected, define a proper relationship in your application model first. π―
π§ Managing Ad Hoc Relationships
If you must use nested unrelated entities, you can maintain the ad hoc relationship through triggers:
- Read Trigger: Control which inner entity records are loaded
- Write Trigger: Handle how data is saved
- Delete Trigger: Manage deletion logic
π Special Note About Record Components
For components with Component Behavior set to record, the rules are slightly different. The top left-most entity is automatically expanded to include other painted entities, so they're not treated as unrelated in the same way. π
π Key Takeaways
- Unrelated entities = no defined relationship in the application model π
- Nesting them is allowed but not recommended β οΈ
- Use
read u_where
to control data retrieval π― - Be aware of delete operation restrictions π
- Always prefer defining proper relationships in your model β¨
Understanding how Uniface handles unrelated entities helps you build more reliable and maintainable applications. When in doubt, define your relationships properly in the application model β your future self will thank you! π
Top comments (0)