Sobes.tech
Junior — Middle

What are the main annotations to use when defining an entity in Hibernate?

sobes.tech AI

Answer from AI

Main annotations for defining an entity in Hibernate:

  • @Entity — marks the class as an entity that will be mapped to a database table.
  • @Table — (optional) specifies the table name and additional parameters if the table name differs from the class name.
  • @Id — indicates the field that is the primary key.
  • @GeneratedValue — sets the strategy for generating the primary key value.

Example:

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    // getters and setters
}