Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I was wondering, what is the best way to create relationships between entities in a project that is being written from scratch, or specify what relationships to have between them?

For example, as we can directly understand the relationship between the Student and the Teacher, or the relationship between the person and the address, how can we identify the entities that have a difficult relationship in a short time?

I also know that in most projects OneToMany or ManyToMany relationships are happened, but I'm having trouble figuring out which is which. I would appreciate it if you could suggest an article or anything on this topic.

What I have tried:

I've tried to try this in many ways but the thing is that I'm having trouble understanding it because some entities are named nonsense in the software.
Posted
Updated 7-Dec-21 9:27am

1 solution

Quote:
create relationships between entities in a project that is being written from scratch, or specify what relationships to have between them?
That's the best part about an ORM. You don't need to think about it yourself (apart from some index, FK-related constraints), an ORM handles the relationship itself. For example, see the following Java code:

Java
public class Teacher {
   int id;
   String name;
}

public class Subject {
   int id;
   String name;
   String code;
}
Then, you can build a relationship between these as (many-to-many):
Java
public class Lecture {
   int id;
   int teacherId;
   int subjectId;
}
Here, a teacher is linked to a subject. What's important is that your Java code "relates" a teacher to multiple subjects and a single subject can be taught by multiple teachers. This is a many-to-many relationship. Hibernate (an other ORMs) automatically translate this relationship to the native SQL queries that generate the tables for you.

java - Code First Apprach with Hibernate - Stack Overflow[^]
Tutorial: Hibernate, JPA - Part 1 - Java Code Geeks - 2021[^]
Quote:
how can we identify the entities that have a difficult relationship in a short time?
You cannot do that automatically, in your first project. Once you have built a few projects and deployed applications — that crashed and caused some late night debugging — you will get a sense of what to do. For example, as soon as you mentioned "relationship between the person and the address", I thought of the address type as being an embedded relationship. Continue to develop a project, see how its performance can be improved and use the documentation of the framework to learn best practices.

https://www.baeldung.com/jpa-embedded-embeddable[^]

Check out the documentation about the relational database (not only) from Oracle: IBM Docs - Relational Database[^].
 
Share this answer
 
v2
Comments
Richard MacCutchan 7-Dec-21 15:39pm    
Should those two ids in the Lecture class really be Strings?
Afzaal Ahmad Zeeshan 7-Dec-21 15:40pm    
Good catch, I normally prefer using Strings as IDs and forgot what I just wrote in the answer. Thanks. :)
Richard MacCutchan 7-Dec-21 15:44pm    
It's the sort of thing we all do from time to time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900