WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Boot & Hibernate · Basic · question 16 of 100

What are some common Hibernate annotations for mapping Java objects to database tables?

📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

Hibernate provides a variety of annotations that can be used to map Java objects to database tables. Here are some of the most commonly used Hibernate annotations for mapping Java objects to database tables:

@Entity: The @Entity annotation is used to mark a Java class as an entity, which means that it will be mapped to a database table. Here is an example:

    @Entity
    @Table(name = "users")
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        
        @Column(name = "first\_name")
        private String firstName;
        
        @Column(name = "last\_name")
        private String lastName;
        
        // ...
    }

@Table: The @Table annotation is used to specify the name of the database table that the entity will be mapped to. Here is an example:

    @Entity
    @Table(name = "users")
    public class User {
        // ...
    }

@Id: The @Id annotation is used to mark a field as the primary key of the entity. Here is an example:

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

@GeneratedValue: The @GeneratedValue annotation is used to specify the generation strategy for the primary key. Here is an example:

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

@Column: The @Column annotation is used to specify the mapping of a field to a column in the database table. Here is an example:

    @Entity
    @Table(name = "users")
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        
        @Column(name = "first\_name")
        private String firstName;
        
        @Column(name = "last\_name")
        private String lastName;
        
        // ...
    }

@OneToMany and @ManyToOne: The @OneToMany and @ManyToOne annotations are used to specify the mapping of one-to-many and many-to-one relationships between entities. Here is an example:

    @Entity
    @Table(name = "orders")
    public class Order {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        
        @ManyToOne
        @JoinColumn(name = "customer\_id")
        private Customer customer;
        
        // ...
    }
    
    @Entity
    @Table(name = "customers")
    public class Customer {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        
        @OneToMany(mappedBy = "customer")
        private List<Order> orders;
        
        // ...
    }

In the example above, we have defined a one-to-many relationship between Customer and Order entities using the @ManyToOne and @OneToMany annotations. The @JoinColumn annotation is used to specify the name of the foreign key column in the orders table that references the primary key column in the customers table.

Overall, Hibernate provides a rich set of annotations that can be used to map Java objects to database tables and define relationships between entities. By using these annotations, developers can easily define the mapping between Java objects and database tables without having to write boilerplate SQL code.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Boot & Hibernate interview — then scores it.
📞 Practice Spring Boot & Hibernate — free 15 min
📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

All 100 Spring Boot & Hibernate questions · All topics