🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we will learn everything about JPA @JoinTable annotation with an example.
@JoinTable Annotation Overview
The @JoinTable annotation in JPA is used to customize the association table that holds the relationships between two entities in a many-to-many relationship. This annotation is often used in conjunction with the @ManyToMany annotation to define the structure of the join table.
It provides essential attributes to tailor the join table's properties to your application's requirements. Here are the most commonly used attributes of the @JoinTable annotation:
name:
Specifies the name of the join table that will be created in the database to manage the many-to-many relationship.
Example:
@JoinTable(
name = "student_course"
)joinColumns:
Specifies the foreign key column(s) from the owning entity to the join table. These columns represent the relationship from the owning entity to the join table. Use the @JoinColumn annotation within joinColumns to define the foreign key column(s).
Example:
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id")
)inverseJoinColumns:
Specifies the foreign key column(s) from the inverse entity to the join table. These columns represent the relationship from the inverse entity to the join table. Use the @JoinColumn annotation within inverseJoinColumns to define the foreign key column(s).
Example:
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id")
)uniqueConstraints:
Specifies the unique constraints for the join table, ensuring that certain combinations of columns have unique values. Use the @UniqueConstraint annotation within uniqueConstraints to define the unique constraints.
Example:
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id"),
uniqueConstraints = @UniqueConstraint(columnNames = {"student_id", "course_id"})
)indexes:
Specifies indexes for the join table columns, which can improve query performance. Use the @Index annotation within indexes to define the indexes for specific columns.
Example:
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id"),
indexes = @Index(columnList = "course_id")
)schema:
Specifies the database schema for the join table. Example: @JoinTable(name = "student_course", schema = "public")
Example:
@JoinTable(name = "student_course", schema = "public")catalog:
The catalog of the table.
foreignKey:
Used to specify or control the generation of a foreign key constraint for the columns corresponding to the joinColumns element when table generation is in effect.
@JoinTable Annotation Example
Student JPA Entity
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id")
)
private List<Course> courses;
// Constructors, getters, setters, etc.
}Course JPA Entity
@Entity
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany(mappedBy = "courses")
private List<Student> students;
// Constructors, getters, setters, etc.
}Customizing the Join Table:
Conclusion
Complete Many-to-Many Mapping Example
References
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment