The association relationship mapping in the Spring ORM framework
The association relationship mapping in the Spring ORM framework
Overview:
The Spring ORM (object relationship mapping) framework is a powerful framework that is used to achieve data persistence and associated relationship mapping between Java applications and relational databases.In the Spring ORM framework, we can use annotations or XML configurations to define the relationship between the physical class, and use the characteristics provided by the framework to simplify the database operation.
One -to -one association relationship mapping:
One -to -one relationship refers to the association between the two tables. One record of one of the tables can only correspond to one record in another table.In the Spring ORM framework, we can use @Onetoone annotations to define a one -to -one relationship.
Example code:
@Entity
public class Employee {
@Id
private Long id;
...
@OneToOne
@JoinColumn(name = "address_id")
private Address address;
...
}
@Entity
public class Address {
@Id
private Long id;
...
@OneToOne(mappedBy = "address")
private Employee employee;
...
}
In the above code, there is a one -to -one relationship between the Employee class and the Address class.The Employee class uses the @onToone annotation to specify the relationship between the address attribute and the address entity class, and the address class uses the MappedBY attribute to specify the relationship with the Employee physical class.@Joincolumn annotation is used to specify the outer key to store one -to -one relationship in the database.
One -to -multiple associated relationship mapping:
One pair of multi -associations refers to the association between the two tables. One of the records of one table can correspond to multiple records in the other table.In the Spring ORM framework, we can use @Onetomany annotation to define a pair of multi -associated relationships.
Example code:
@Entity
public class Department {
@Id
private Long id;
...
@OneToMany(mappedBy = "department")
private List<Employee> employees = new ArrayList<>();
...
}
@Entity
public class Employee {
@Id
private Long id;
...
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
...
}
In the above code, there is a pair of multi -associated relationships between the Department class and the Employee class.The department class uses the @onetomany annotation to specify the relationship between the EMPLOYEES property and the Employee physical class, and the Employee class uses @Manytoone annotations to specify the relationship with the department physical class.@Joincolumn annotation is used to specify a pair of multi -associated external keys in the database.
Multiple -to -multiple relationship mapping:
Multi -associated relationships refer to the association between the two tables. One record of one of the tables can correspond to multiple records in the other table, and correspondingly, one record of the other table can also correspond to the first table in the first table.Multiple records.In the Spring ORM framework, we can use the @Manytomany annotation to define more to multi -associated relationships.
Example code:
@Entity
public class Student {
@Id
private Long id;
...
@ManyToMany
@JoinTable(name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id"))
private List<Course> courses = new ArrayList<>();
...
}
@Entity
public class Course {
@Id
private Long id;
...
@ManyToMany(mappedBy = "courses")
private List<Student> students = new ArrayList<>();
...
}
In the above code, there are multiple relationships between the Student class and the Course class.The Student class uses the @Manyanany annotation to specify the relationship between the Courses attribute and the Course entity class, and the Course class uses the MappedBY attribute to specify the relationship with the Student physical class.@JOINTABLE annotations are used to specify the intermediate table that stores more to multiple associated relationships in the database. JoinColumns attribute specifies the external key name of the current physical class, and the INVERSEJOINCOLUMNS attribute specifies the external key name of the associated entity class.
Summarize:
The Spring ORM framework provides rich annotations and characteristics, which is convenient for us to map and manage the relationship between the physical class in Java applications.Through one -to -one, one -to -one, and multiple -to -multiple -to -many -related relationships, we can easily store and retrieve relevant data in the database.This provides an efficient and reliable data persistence solution for our applications.