Skip to content
Ameen Abozaid
Planned lessonJNR-028-L01

Mapping an entity

  • Lesson 1 of 8
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

turn a class into a table mapping.

Planned topics

  • @Entity marks a class as persistent; @Id marks its primary key.
  • JPA needs a no-argument constructor (protected is fine); entities are normal classes, not records.
  • @Table and @Column set names, length, and nullability; they should match the real schema.
  • Store enums with @Enumerated(EnumType.STRING), never the default ordinal (numbers break when you reorder).
  • Entities model the database, not the API: do not return them from controllers.
Draft outline details

Demonstration

java
@Entity
@Table(name = "product")
public class Product {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(nullable = false, length = 120)
    private String name;
    @Enumerated(EnumType.STRING)
    private ProductStatus status;
    protected Product() {}
}

Check your understanding

Which enum mapping is safe?

Show answer

EnumType.STRING.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.