Skip to content

sushantrout/complete-spring-boot

Repository files navigation

complete-spring-boot

Object Relational Mapping

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.

Mapping Directions

  1. 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.
  2. 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.

Types of Mapping

Following are the various ORM mappings: -
  1. One-to-one : This association is represented by @OneToOne annotation. Here, instance of each entity is related to a single instance of another entity.
  2. 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.
  3. 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.
  4. 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.

Important points at the time of fetching the data.


	@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"
	}
	

And also we have some annotations like :

  1. @JsonManagedReference
  2. @JsonBackReference
  3. @JsonIgnore

One-To-One

With forgain key

(https://github.com/sushantrout/complete-spring-boot/tree/main/one-to-one-with-forgain-key)

one-to-one-forgain-key


@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;
}

With shared primary key

(https://github.com/sushantrout/complete-spring-boot/tree/main/one-to-one-with-shared-primary-key)

one-to-one-shared-primarykey


@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;
}

Using join table

(https://github.com/sushantrout/complete-spring-boot/tree/main/one-to-one-with-third-table)

one-to-one-jointable

@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

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.

one-to-many-join-column

@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;
}


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages