Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
sakshamjain
Advisor
Advisor

Introduction


In SAP CAP (Cloud Application Programming Model), establishing effective relationships between entities is crucial for designing and implementing robust and efficient applications. These relationships define how data is connected and provide the foundation for data integrity and application behavior. In this blog post, we will explore and understand the different relationship types available in SAP CAP: one-to-one, one-to-many, and many-to-many. Let's dive in!

One-to-One


A one-to-one relationship represents a connection between two entities, where each record in one entity is associated with only one record in another entity, and vice versa. This type of relationship is typically used when there is a unique and singular association between two entities.

Example Scenario: Consider a scenario where we have two entities, "Employee" and "Address." Each employee has only one residential address, and each address is assigned to only one employee. In this case, we can define a one-to-one relationship between the "Employee" and "Address" entities.
using { managed } from '@sap/cds/common';

namespace employee.catalog;

entity Employee : managed {
key id: UUID;
employeeName: String;
employeeAddress: Association to Address;
}

entity Address : managed {
key id: UUID;
addressLineOne: String;
addressLineTwo: String;
pincode: String;
city: String;
state: String;
employee: Association to Employee;
}

One-to-Many


A one-to-many relationship defines a connection between two entities where each record in one entity can be associated with multiple records in another entity. However, each record in the second entity is associated with only one record in the first entity. This relationship type is widely used when there is a hierarchical or parent-child relationship between entities.

Example Scenario: Let's take an example where we have two entities, "Department" and "Employee." Each department can have multiple employees, but each employee is assigned to only one department. In this case, we can establish a one-to-many relationship between the "Department" and "Employee" entities.
using { managed } from '@sap/cds/common';

namespace employee.catalog;

entity Department : managed {
key id: UUID;
departmentName: String;
employees: Association to many Employee on employees.departmentID = $self.id;
}

entity Employee : managed {
key id: UUID;
employeeName: String;
departmentID: String;
department: Association to Department on department.id = departmentID;
}

Many-to-Many


A many-to-many relationship represents a connection between two entities where each record in one entity can be associated with multiple records in another entity, and vice versa. This relationship type is used when there is a complex and non-unique association between entities.

Example Scenario: Consider a scenario where we have two entities, "Student" and "Course." Each student can enroll in multiple courses, and each course can have multiple students. In this case, we need to establish a many-to-many relationship between the "Student" and "Course" entities. To achieve this, we introduce a third entity, known as a junction or association entity, which contains the necessary foreign keys to connect the two entities.
using { managed } from '@sap/cds/common';

namespace employee.catalog;

entity Student {
key studentID: UUID;
studentName: String;
courses: Composition of many StudentToCourse on courses.student = $self;
}

entity Course {
key courseID: UUID;
courseName: String;
students: Composition of many StudentToCourse on students.course = $self;
}

entity StudentToCourse {
student: Association to Student;
course: Association to Course;
}

Conclusion


Understanding and correctly defining the relationship types is essential for designing robust data models in SAP CAP applications. The one-to-one, one-to-many, and many-to-many relationships provide the flexibility to represent various associations between entities accurately. By choosing the appropriate relationship type, developers can ensure data integrity, optimize application performance, and enable complex data retrieval and manipulation operations.

Remember, the choice of relationship type depends on the specific business requirements and the nature of data associations in your application. With a clear understanding of these relationship types, you can design and implement efficient and scalable SAP CAP applications.
2 Comments
phidoe
Explorer
0 Kudos
Hello sakshamjain ,

thank you for this great post.

Do you know if there is a way to create fiori elements UIs for Many-To-Many relationships? I am thinking of a a list report displaying students and an object page that allows to assign courses to students.

Thanks in advance,

Br,

Philipp
CesarIllera
Explorer
0 Kudos

Hello @sakshamjain 

Thanks for sharing your knowledge, It was really help for me.

Regards