- ORM or Object Relational Mapping is a system that implements the responsibility of mapping the Object to Relational Model. That means it is responsible to store Object Model data into Relational Model and further read the data from Relational Model into Object Model.
-
- Unidirectional relationship: In this relationship, only one entity can refer the properties to another. It contains only one owing side that specifies how an update can be made in the database.
- Bidirectional relationship: This relationship contains an owning side as well as an inverse side. So here every entity has a relationship field or refer the property to other entity.
-
Following are the various ORM mappings: -
- One-to-one : This association is represented by @OneToOne annotation. Here, instance of each entity is related to a single instance of another entity.
- One-to-many : This association is represented by @OneToMany annotation. In this relationship, an instance of one entity can be related to more than one instance of another entity.
- Many-to-one : This mapping is defined by @ManyToOne annotation. In this relationship, multiple instances of an entity can be related to single instance of another entity.
- Many-to-many : This association is represented by @ManyToMany annotation. Here, multiple instances of an entity can be related to multiple instances of another entity. In this mapping, any side can be the owing side.
@Entity
@Table(name = "tbl_employee")
@Data
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "address"}) //It makes the entity non-owning side lazy
public class Employee{
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
}
{
"id": 3,
"name": "SUSHANT KUMAR ROUT"
}
- @JsonManagedReference
- @JsonBackReference
- @JsonIgnore
@Entity
@Table(name = "tbl_employee")
@Data
public class Employee {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_name")
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
}
@Entity
@Table(name = "tbl_address")
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "country")
private String country;
@OneToOne(mappedBy = "address")
private Employee employee;
}
@Entity
@Table(name = "tbl_employee")
@Data
public class Employee {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_name")
private String name;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "employee", optional = true)
@PrimaryKeyJoinColumn
private Address address;
}
@Entity
@Table(name = "tbl_address")
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "country")
private String country;
@OneToOne(optional = true)
@JoinColumn(name = "employee_id")
@MapsId
private Employee employee;
}
@Entity
@Table(name = "tbl_employee")
@Data
public class Employee {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_name")
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "employee_address",
joinColumns = { @JoinColumn(referencedColumnName = "id", name = "employee_id") },
inverseJoinColumns = { @JoinColumn(referencedColumnName = "id", name = "address_id", unique = true) })
private Address address;
}
@Entity
@Table(name = "tbl_address")
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "country")
private String country;
@OneToOne(mappedBy = "address")
private Employee employee;
}
one-to-many mapping means that one row in a table is mapped to multiple rows in another table. It's a good practice to mark the many-to-one side as the owning side.
@Entity
@Table(name = "tbl_employee")
@Data
public class Employee {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_name")
private String name;
@OneToMany(mappedBy = "employee")
private List<Address> address = new ArrayList<>();
}
@Entity
@Table(name = "tbl_address")
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "country")
private String country;
@ManyToOne
@JoinColumn(name = "address_id")
private Employee employee;
}



