Sure, let's take a look at some concrete examples.
- @ManyToOne:
Consider the following example of a blog post and a user who wrote it. A blog post can be written by a single user, but a user can write multiple blog posts.
@Entity
@Table(name = "blog_post")
public class BlogPost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
// getters and setters
}
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "user")
private List<BlogPost> posts;
// getters and setters
}
In the above example, the BlogPost is the source entity and the User is the target entity. The @ManyToOne annotation on the BlogPost entity indicates that many BlogPosts can be associated with a single User, and the @JoinColumn annotation specifies the Foreign Key column to link the two entities. The mappedBy attribute on the User entity tells JPA to use the "user" field on the BlogPost entity as the mappedBy attribute to link the two entities.
- @OneToMany:
Consider the following example of a customer and their orders. A customer can have many orders, but an order can only belong to one customer.
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private List<Order> orders;
// getters and setters
}
@Entity
@Table(name = "order")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String status;
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer;
// getters and setters
}
In the above example, the Customer is the source entity and the Order is the target entity. The @OneToMany annotation on the Customer entity indicates that a single Customer can be associated with many Orders, and the mappedBy attribute specifies the Foreign Key column to link the two entities. The @ManyToOne annotation on the Order entity indicates that each Order belongs to a single Customer, and the @JoinColumn annotation specifies the Foreign Key column to link the two entities.
- @OneToOne:
Consider the following example of a student and their address. A student can have only one address, and an address can only belong to one student.
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(mappedBy = "student")
private Address address;
// getters and setters
}
@Entity
@Table(name = "address")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String street;
private String city;
@OneToOne
@JoinColumn(name = "student_id")
private Student student;
// getters and setters
}
In the above example, both the Student and Address entities are both the source and target entities. The @OneToOne annotation on the Student entity indicates that each Student has only one Address, and the mappedBy attribute specifies the Foreign Key column to link the two entities. The @OneToOne annotation on the Address entity indicates that each Address belongs to a single Student, and the @JoinColumn annotation specifies the Foreign Key column to link the two entities.
- @ManyToMany:
Consider the following example of a book and its authors. A book can have multiple authors, and an author can write multiple books.
@Entity
@Table(name = "book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToMany
@JoinTable(
name = "book_author",
joinColumns = @JoinColumn(name = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id"))
private List<Author> authors;
// getters and setters
}
@Entity
@Table(name = "author")
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany(mappedBy = "authors")
private List<Book> books;
// getters and setters
}
In the above example, both the Book and Author entities are both the source and target entities. The @ManyToMany annotation on the Book entity indicates that each Book can have multiple Authors, and the @JoinTable annotation specifies the join table to link the two entities. The @ManyToMany annotation on the Author entity indicates that each Author can write multiple Books, and the mappedBy attribute specifies the Foreign Key column to link the two entities.