Technical Interview Handbook

Technical Interview Handbook

  • Notes
  • About Me

›ORM

Introduction

  • Getting Started

Best Practices

  • Spring Project

Messaging

    Basics

    • Messaging Guide

    Rabbit MQ

    • Rabbit MQ Guide

Logging

  • ELK Guide

Concepts

  • GraphQL vs Rest

ORM

  • Hibernate Mapping Guide

Database

  • Mongo DB Guide

Enterprise Search

  • Apache Solr Guide

UI

  • ReactJS Guide

Build Tool

  • Maven Guide

Testing

  • Junit5 & Mockito Guide

CI/CD

  • Docker Desktop Installation
  • Docker Guide

About Me

  • About Me

Hibernate Mapping Guide

Types of Mapping

@OneToOne - One Product is owned by one member

  • It's always a best practice to put mapping in PK class who owns and has FK mapping of owned entity.
@OneToOne
private Members members;

@OneToMany(One Member can own multiple Products)

  • This mapping loads lazily by default hence leading to optimizing the performance. How? Say we have 50 members, with 100000 products. Each time, we need 1 member, entire product will be loaded for this operation. So, better load products only when required. Thats's lazy loading.
  • Optional relationship type

@ManyToOne(Not used here but eg. Many Products owned by One Member)

  • Eager by default. So, don't go for this unless required.
  • @ManyToOne(fetch = FetchType.LAZY) - This again makes it lazy. But with same example as above, would you want to load entire 100000 products and work in the reverse way? No? Right, choice is yours.
  • This relationship type is also optional by default but can be made mandatory - @ManyToOne(optional = false)
  • If relationship type is mandatory, we can't save Member without a Product.

@ManyToMany(One member can read multiple books. And one book can be read by multiple members)

  • Configurations for optionality, fetch type, cascading effect can be done as in others

Common Tips

  • referencedColumnName isn't required for PK mapping. For non-PK mapping, it is required
← GraphQL vs RestMongo DB Guide →
  • Types of Mapping
    • @OneToOne - One Product is owned by one member
    • @OneToMany(One Member can own multiple Products)
    • @ManyToOne(Not used here but eg. Many Products owned by One Member)
    • @ManyToMany(One member can read multiple books. And one book can be read by multiple members)
  • Common Tips
Technical Interview Handbook
Docs
Getting Started (or other categories)Guides (or other categories)API Reference (or other categories)
Community
User ShowcaseStack OverflowProject ChatTwitter
More
BlogGitHubStar
Copyright © 2020 anupama-sinha