The @Entity annotation marks a plain Java class as a persistent entity, telling Hibernate (and JPA) that instances of this class map to rows in a database table and can be saved, loaded, and queried through the persistence context.
Key Points: • A class annotated @Entity must have a no-argument constructor and a field marked @Id identifying its primary key. • By default, the table name matches the class name unless overridden with @Table(name = "..."). • Without @Entity, Hibernate has no way of knowing the class should be managed and mapped to a table. • It's typically combined with @Id, @GeneratedValue, and @Column to fully describe the mapping. • @Entity classes cannot be final, since Hibernate may need to create runtime proxy subclasses for lazy loading.
Example: Annotating a plain Employee class with @Entity and giving it an @Id field turns it from an ordinary POJO into something session.save(employee) can persist directly to an employee table.
Code Example:
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}Interview Tip: A concise interview answer is:
"@Entity marks a POJO as a Hibernate-managed persistent class mapped to a database table. It has to be paired with an @Id field for the primary key, and it enables the class to be saved, loaded, and queried through the Session or JPA EntityManager."