Fundemental questions about associations

1
Hi,   I'm new to Mendix and I have some difficulties to understand the meaning of associations in Mendix. I hope someone of you can explain them to me. (To simplify the reference I added some numbering to my questions.)   Let's start with the example of two entities Customer and Address. Each Customer can have several Addresses (shipping and billing address), but each Address is associated with exactly one Customer. So we have a 1 to many association: Customer (name: String) 1 - * Address (type: Enum, address: String) Let's assume we have a two Customers and each of them has 2 Addresses stored. In any classical relational database, I would use the Customer-ID and store it as an attribute in the Address table, to know exactly which Address belongs to which Customer. But I understood from the rapid developer learning path, it's not the intended way of modeling in Mendix.   1) How does Mendix know which address belongs to which customer, when there isn't any attribute containing this mapping?   In the Crash Course Chapter 9.7.1 there are the entities Department and Employee with a 1 - * association. I.e., Each Employee is assigned to exactly one Department, but each Department may have several Employees assigned. Then a second * - * associations is defined between Employee and Department. Before each Employee was assigned to exactly one Department. But now there is a second association telling each Employee can be assigned to several Departments. This is a conflict to me.   2) How can it be allowed to have two contradicting associations defined?   The chapter says the second association would be required for the XPath, that is defined later in the chapter. My best guess is, I must read the XPath backwards to understand it's meaning. [UserManager.Employee_Department/UserManager.Department/UserManager.Employee_Department_Security/UserManager.Employee/UserManager.Employee_Account='[%CurrentUser%]'] I read it the following: The user account of the currently logged in user is resolved to exactly one Employee object via the 1 - 1 association UserManager.Employee_Account. Via the * - * association UserManager.Employee_Department_Security I get all Departments the selected Employee is assigned to. From each of these departments I get all the Employees assigned to each department by the 1 - * association UserManager.Employee_Department.   3) If this interpretation is correct, why can't we do the same then without the recently added association UserManager.Employee_Department_Security, by using the Employee_Department association twice in the XPath? [UserManager.Employee_Department/UserManager.Department/UserManager.Employee_Department/UserManager.Employee/UserManager.Employee_Account='[%CurrentUser%]'] We would get the Employee object of the current user; from there we would get the one department this user is assigned to and from there all the employees assigned to this one department.   In the "Expand Your Domain Modeling Skills" learning path in chapter 2.2 "Multiple Associations Between Entities" are supposed to be explained.   It says: "The company wants to be able to see a data grid in their app with all customers, and in that data grid show both addresses immediately. This is impossible when you use a One-to-Many association from Customer to Address, using an enumeration to see what type of address it is. With multiple One-to-One associations, it is possible to directly output both addresses in the data grid."   Unfortunately, it is just claimed it would be impossible, but it is not explained, why this isn't possible? At least to me it is not obvious.   4) Can someone explain, why it's not possible to get all addresses from a 1 - * association?   In chapter 2.3 of the "Expand Your Domain Modeling Skills" it says that if we add a second 1 - 1 association between Team and Player entities, Mendix knows that there can be only one Captain per Team.   5) How can Mendix know that the 1 - 1 association refers to the Captain attribute and not to any other attribute in the Player entity?   Please help me to understand it. Thanks, Arne
asked
3 answers
0

Hi Arne

 

Firstly, associations are tables in the database. This is Thorsten said.

 

As for you 3 and 4 question. My effort to make it more transparent, hope it helps:

Question 3:

When you look at your domain model your reference of employee_department is a 1 * reference. This means that multiple employees can be set to 1 department. This also implies that these same employees can't be set to a different department. Your other reference Employee_Department_Security is a multiple multiple reference * * and this means that you can set employees to department a, but also to department b or c etc.

 

Question 4:

The problem here is not the reference (or association) but the problem is the data grid. A data grid can only provide 1 -1 information, otherwise the column value can have way to much information. Therefore, when you have 1 -1 references, this can be accessed directly from a data grid.

 

Hope it helps, otherwise just keep asking

answered
2

Arne, can only provide an answer for the first two questions at the moment:

  1. Mendix creates association tables where it stores the IDs of the associated objects. This is hidden from the domain model, but you can inspect this with PGAdmin or DBearver (if using a PostgreSQL db).
  2. Same as before: Mendix creates two association tables, one for each association.
answered
0

Ok, the answer from Thorsten helped me to figure out the answer of question 5, too.
(Mendix doesn’t know what attribute the association belongs to. The connection is made in the user interface by using the association, when the team’s captain is chosen. So Mendix uses the 1 – 1 association table to store the team’s captain, since the captain selection widget refers to this association.)

But still, I can’t figure out the answers to the XPath related questions 3 & 4.

answered