Object-relational mapping (ORM)
What an ORM maps: class and table, attribute and column, association and foreign key, inheritance strategies; lazy or eager loading and the N+1 query problem.
- 29 cards
- 6 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- The numbers
- Scenarios
- Understand
A few cards from the deck
Object-relational mapping (ORM): what is it?
Show the answer
A technique and tool that map a program's classes and objects to the tables and rows of a relational database, and generate the SQL in the programmer's place.
Hibernate (which implements the JPA specification), SQLAlchemy or Django's ORM are examples. The SQL is still there: the ORM writes it, and you need to know which SQL it writes.
Which JPA annotation, placed on the
auteurattribute, puts the foreign keyauteur_idin thelivretable?
```java
@Entity
class Livre {
@Id Long id;
String titre;
Auteur auteur;
}`@ManyToOne@OneToMany@ManyToMany@Column(name = "auteur_id")
Show the answer
@ManyToOneMany books for one author: the "many" side holds the foreign key, with
@ManyToOne, and its default name isauteur_id(@JoinColumnpicks another one).@ManyToManywould create a join table, and@Columndescribes a plain column, not an association.In an ORM, a class corresponds to a …, an attribute to a … and an object to a ….
Show the answer
In an ORM, a class corresponds to a table, an attribute to a column and an object to a row.
This is the basic correspondence. The rest (associations, inheritance) follows from it.
Entity: what is it?
Show the answer
A class whose instances correspond to rows of a table, each identified by a primary key that the ORM tracks.
In JPA, it is marked with
@Entityand its identifier with@Id. A class without the mark is not stored in the database.