How to Create Entity in Spring Boot: Simple Guide
To create an entity in Spring Boot, define a Java class and annotate it with
@Entity. Add a primary key field annotated with @Id and optionally @GeneratedValue for auto-generation.Syntax
Use the @Entity annotation on a class to mark it as a database entity. Use @Id to mark the primary key field. Optionally, @GeneratedValue can auto-generate the primary key value.
@Entity: Marks the class as a database table.@Id: Marks the primary key field.@GeneratedValue: Configures automatic key generation.- Class fields represent table columns.
java
import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // getters and setters }
Example
This example shows a simple User entity with an auto-generated ID, name, and email fields. It demonstrates how to define the entity class and its primary key.
java
package com.example.demo.entity; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; public User() {} public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Output
No console output; this class defines a database entity used by Spring Data JPA.
Common Pitfalls
Common mistakes when creating entities include:
- Forgetting the
@Entityannotation, so Spring Boot does not recognize the class as a table. - Not marking any field with
@Id, causing errors because every entity needs a primary key. - Using primitive types for the ID without
@GeneratedValue, which can cause manual ID management issues. - Not providing a no-argument constructor, which is required by JPA.
java
/* Wrong: Missing @Entity annotation */ public class Product { @Id private Long id; } /* Right: Add @Entity annotation */ import jakarta.persistence.Entity; import jakarta.persistence.Id; @Entity public class Product { @Id private Long id; }
Quick Reference
| Annotation | Purpose |
|---|---|
| @Entity | Marks the class as a database entity |
| @Id | Defines the primary key field |
| @GeneratedValue | Configures automatic primary key generation |
| @Column | Customizes column mapping (optional) |
| @Table | Specifies table name and schema (optional) |
Key Takeaways
Always annotate your entity class with @Entity to map it to a database table.
Mark one field as the primary key using @Id; use @GeneratedValue for auto-generated keys.
Provide a no-argument constructor for JPA to instantiate the entity.
Ensure your entity fields have getters and setters for proper data access.
Avoid missing @Entity or @Id annotations to prevent runtime errors.