- Published on
Handling Associations and relationship in Hibernate
- Authors
- Name
- Gary Huynh
- @huynhthienthach
Alright, buckle up, folks! We're about to delve into the tangled world of relationships
... but not the kind your therapist talks about. No, these are the associations
that live between our database entities
. We're talking one-to-one
, one-to-many
, many-to-one
, and many-to-many
. It's going to get complicated, and maybe a little steamy (if you get hot and bothered by database design
, that is).
Let's start with the first type of relationship: one-to-one. This is when each instance of Entity A can be associated with one and only one
instance of Entity B, and vice versa. Here's how you'd set this up in Java
with JPA/Hibernate
:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne
private Profile profile;
//getters and setters...
}
@Entity
public class Profile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String bio;
@OneToOne(mappedBy = "profile")
private User user;
//getters and setters...
}
In this example, each User
has one Profile
, and each Profile
belongs to one User
. It's like that couple that's so inseparable they might as well be one person.
Next up, we have the one-to-many/many-to-one relationship. This is where one instance of Entity A can be associated with multiple
instances of Entity B, but each instance of Entity B is associated with only one
instance of Entity A. It's like a parent with their children: each child has one parent (in this entity
), but a parent can have multiple children.
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "author")
private List<Book> books;
//getters and setters...
}
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToOne
private Author author;
//getters and setters...
}
Here, each Author
can write many Books
, but each Book
has one Author
. It's like the literary world's version of a polygamous relationship.
Finally, we have the many-to-many relationship. This is where multiple
instances of Entity A can be associated with multiple
instances of Entity B. This is the "friends with benefits" of entity relationships
. Everybody is associated with everybody else!
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany
private List<Course> courses;
//getters and setters...
}
@Entity
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToMany(mappedBy = "courses")
private List<Student> students;
//getters and setters...
}
In this example, a Student
can enroll in many Courses
, and a Course
can have many Students
. It's an academic free-for-all!
Well, there you have it folks, a whirlwind tour of entity relationships! Remember, it's not about who your entities
are underneath, but what relationships
they have that defines them. Happy mapping!