Sobes.tech
Junior — Middle

How to determine the properties of a class so that Hibernate can correctly map it to a database?

sobes.tech AI

Answer from AI

In order for Hibernate to correctly map a class to a database, it is necessary to:

  • Define the class properties as fields with corresponding data types.
  • Use Hibernate/JPA annotations to specify the mapping, for example, @Entity above the class, @Id for the primary key.
  • Properties should have getters and setters (JavaBeans convention).
  • For complex types or relationships, use annotations like @OneToMany, @ManyToOne, etc.

Example:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private int age;

    // getters and setters
    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 int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
}
How to determine the properties of a class so that… - sobes.tech