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.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The numbers
  5. Scenarios
  6. 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 auteur attribute, puts the foreign key auteur_id in the livre table?

    ```java
    @Entity
    class Livre {
    @Id Long id;
    String titre;
    Auteur auteur;
    }
    `

    • @ManyToOne
    • @OneToMany
    • @ManyToMany
    • @Column(name = "auteur_id")
    Show the answer

    @ManyToOne

    Many books for one author: the "many" side holds the foreign key, with @ManyToOne, and its default name is auteur_id (@JoinColumn picks another one). @ManyToMany would create a join table, and @Column describes 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 @Entity and its identifier with @Id. A class without the mark is not stored in the database.