Junior — Middle
How do you denote a class as an entity for ORM systems like Hibernate in Java?
sobes.tech AI
Answer from AI
In Java, to designate a class as an entity that will be used by an ORM system (such as Hibernate), the @Entity annotation from the javax.persistence package is used.
This annotation informs the ORM that this class corresponds to a table in the database, and its instances are records in that table.
Example:
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
// getters and setters
}
Besides @Entity, annotations like @Table are often used to specify the table name, @Id to denote the primary key, as well as other annotations for configuring columns and relationships.