🧭 ORM

GunhoΒ·2024λ…„ 11μ›” 16일
1

Object Oriented Programming (OOP) & Java

λͺ©λ‘ 보기
19/29

🧭 ORM

🧭 ORM (Object Relational Mapping) is a mechanism that maps objects from the programming languages into the relational data from the RDBMS and vice versa.

ORM can be also considered as an interface between the OOP based programming languages and RDBMS. ORM provides features strictly relevant to the databases from object-table mapping, SQL query manipulation, transactions, and etc.

ORM was introduced, followed by the incompatible paradigms between OOP and RDBMS where the differences in their functioning logics incurred transition issues.

JPA (Java Persistence API) is a standardised ORM in Java Spring where it internally operates upon JDBC. Hibernate is a popular implementation of JPA in which Hibernate was introduced earlier and then became the standardisation.

μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ° - 기본편 (κΉ€μ˜ν•œ) Available at here

πŸ”‘ Key benefits of ORM are namely:

  • πŸͺ„ simplified database related logics

  • πŸ“Š no database dependent development

    • leading to an easier database transition?
  • πŸ”© increased code flexibility and maintenance

the above benefits, however, come at the πŸ’° cost of:

  • 🧚 potential low performance followed by automatic query creation

  • πŸ”¬ difficulties in database fine-tuning


πŸ‘‘ Paradigm Incompatibility

πŸ‘‘ Paramdigm incompatibilities often resulted in the old programmers to create an application that is either OOP or RDBMS centred, where overall neigher have been a good programming practices.

Paramdigms that specifically differ between OOP and RDBMS and its resulting incompatibilities are largely:

  • πŸ’· Inheritance
  • πŸ”— References
  • 🟰 Equaltiy

πŸ’· Inheritance

πŸ’· Inheritance refers to the identical inheritance from OOP.

RDBMS fundamentally lacks the native concept of inheritance and this often resulted in the application code with the objects under inheritance to have been shifted towards data-centric objects without inheritance. This led the applications to be database oriented and to be heavily dependent on databases.

μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ° - 기본편 (κΉ€μ˜ν•œ) Available at here

An alternative was to create manual mappers where the objects maintain inheritance and the databases maintain its table relations. This essentially incurred multiple SQL query statements calls to databases for the insertion and retrieval calls and as clearly illustrated underneath, developing a manual mapper was considered complicated so that data-centric objects were soon preferred:

Item & Album class

public abstract class Item {
	private Long id;
    private String name;
    private int price;
}

public class Album extends Item {
	private String artist;
}

INSERTION

INSERT ..... INTO item;
INSERT ..... INTO album;

RETRIEVAL

SELECT 
	i._____, ... 
    a.___, .... 
FROM item i
JOIN album a

|| 

SELECT 
	i._____, ... 
FROM item i

ORM (JPA) automates the overall above process and hence addresses paradigm incompatibilities in inheritance.ORM provides EntityManager and its public interfaces where it automates the mapping process and the developers can simply use such interfaces and apply inheritance in OOP perspectives if necessary.

JPA

em.persist(item);
em.find(Item.class, 1L);

πŸ”— References

πŸ”— References in objects and RDBMS essentially have varying implications wherein:

  • referred by types (objects)
  • referred by foreign keys (RDBMS)

This can be further clarified with the underneath code:

Member & Team class

public class Member {
	private Long id;
	private Team team;
}

public class Team {
	private Long id;
	private List<Member> members
}

where given the two objects referencing each other in OOP, the following code required extra manipulations as to get the team_id or member_id via the getId() method and relevant set methods thereby ultimately resulting in:

public class Member {
	private Long id;
	private Long teamId;
}

ORM, however, handles all the complicated manipulation processes at the code level where it maintains OOP references and enables automatic reference retrievals.

JPA

member.setTeam(team);
jpa.persist(member);
Member member = jpa.find(Member.class, memberId);
Team team = member.getTeam();

🟰 Equality

Equality is also a concept that has varied implications on OOP and RDMBS wherein:

  • OOP (==) checks for the memory address
  • RDBMS (id) checks for the id of a row within a table

This can be clearly represented underneath where the retrieved identical data from the database are considered as different in Java:

Member & MemberDAO class

public class Member {
	private int id;
    
    public Member(int id) {
    	this.id = id;
    }
}

class MemberDAO {
  public Member getMember(String memberId) {
  	    String sql = "SELECT * FROM MEMBER WHERE MEMBER_ID = ?";
        ...
        //JDBC API, SQL Execution
        return new Member(...);
  	}
}

public class Main() {
	public static void main(String[] args) {
    	Member member1 = MemberDao.getMember(1);
        Member member2 = MemberDao.getMember(1);
    	
        System.out.println(member1 == member2) // false;
    }
}

ORM, however, addresses the above paradigm incompatibility following its internal mechanisms. Developers then have to simply use publicly available interfaces or methods:

JPA

String memberId = "1L";
Member m1 = jpa.find(Member.class, memberId); 
Member m2 = jpa.find(Member.class, memberId); 
println(m1 == m2) //true


πŸ“š References

μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ° - 기본편 (κΉ€μ˜ν•œ)
μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ°
F-Lab

profile
Hello

0개의 λŒ“κΈ€