Mapping an entity
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
@Entitymarks a class as persistent;@Idmarks its primary key.- JPA needs a no-argument constructor (protected is fine); entities are normal classes, not records.
@Tableand@Columnset 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
@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.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.