π§
ORM
(Object Relational Mapping
) is a mechanism that mapsobjects
from theprogramming languages
into therelational data
from theRDBMS
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.
π Key benefits of ORM
are namely:
πͺ simplified database related logics
π no database dependent development
π© 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
π 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
refers to the identicalinheritance
fromOOP
.
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
.
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
in objects
and RDBMS
essentially have varying implications wherein:
types
(objects
)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
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
μλ° ORM νμ€ JPA νλ‘κ·Έλλ° - κΈ°λ³ΈνΈ (κΉμν)
μλ° ORM νμ€ JPA νλ‘κ·Έλλ°
F-Lab