Hibernate Flashcards

Category sponsor

Hibernate is a powerful and widely-used Object-Relational Mapping (ORM) framework for Java. It simplifies database operations by mapping Java objects to database tables, eliminating much of the boilerplate code required for JDBC operations. Hibernate implements the Java Persistence API (JPA) specification and provides additional features beyond the standard, including caching, lazy loading, and advanced querying capabilities through HQL (Hibernate Query Language) and Criteria API. Hibernate is designed to be database-independent, allowing developers to switch between different database systems with minimal code changes. It's an essential tool for enterprise Java applications, providing efficient data persistence and retrieval mechanisms.

Our flashcard app contains carefully selected Hibernate interview questions, complete with comprehensive answers, to effectively prepare you for any interview requiring Hibernate knowledge. IT Flashcards is not only a valuable tool for job seekers but also a great way to strengthen and test your understanding of Hibernate ORM framework. Regular practice with the app will keep you updated with the latest trends in Java persistence and enhance your expertise in object-relational mapping.

Example Hibernate flashcards from our app

Download our app from the App Store or Google Play to get more free flashcards or subscribe for access to all flashcards.

Hibernate

What is Hibernate?

**Hibernate** is a popular object-relational mapping (ORM) tool in Java that provides a framework for mapping Java domain objects to relational database tables. Hibernate implements the JPA (Java Persistence API) specification and adds its own methods and annotations, significantly simplifying working with databases.

By using Hibernate, a developer can focus on the business logic of the application without worrying about data access details. Hibernate automatically handles CRUD operations (create, read, update, delete), data type conversions, and database connection management.

Hibernate is independent of specific database management systems (DBMS), which means it can work with most popular relational databases, such as MySQL, Oracle, PostgreSQL, and many others.

Hibernate

What annotations are used in Hibernate for mapping classes to database tables?

In Hibernate, various annotations are used to map classes to tables in the database. Here are the most important ones:

1. **@Entity**: This is the main annotation that we need to add to the class to map it to a table in the database. A class annotated as @Entity is recognized by Hibernate as an entity.

2. **@Table**: The @Table annotation is used to define the name of the table to which we want to map the class. If we do not use this annotation, the default table name will be the same as the class name.

3. **@Column**: This annotation is used to map a class property to a column in the database. We can define the column name, its length, whether it is unique, etc.

4. **@Id**: This annotation is used to define the primary key in the table.

5. **@GeneratedValue**: Typically used with @Id, the @GeneratedValue annotation is used to specify the strategy for generating the primary key value.

6. **@OneToMany, @ManyToOne, @ManyToMany, @OneToOne**: These annotations are used to define the type of relationship between tables.

7. **@JoinColumn**: This annotation is used to specify the name of the column used for joining tables in a relationship.

8. **@Transient**: This annotation is used when we want to specify which field of the class should not be mapped to a column in the table.

Hibernate

What is the difference between @OneToOne and @ManyToOne in Hibernate?

Annotations **@OneToOne** and **@ManyToOne** in Hibernate are used for mapping relationships between tables in a database. They are used to represent one-to-one and many-to-one relationships.

The **@OneToOne** annotation is used when one row in table A can be associated with only one row in table B, and one row in table B can be associated with only one row in table A. This is a relatively rare case in real-world applications, as one-to-one relationships are usually represented as a single table.

The **@ManyToOne** annotation is used when multiple rows in table A can be associated with one row in table B, but one row in table B can be associated with many rows in table A. This is a typical case when we have a "many-to-one" relationship, for example, many orders can be assigned to one customer.

In summary, the main difference between @OneToOne and @ManyToOne is that @OneToOne represents a one-to-one relationship, while @ManyToOne represents a many-to-one relationship.

Hibernate

What are the main differences between @Embeddable and @Embedded?

**@Embeddable** and **@Embedded** are Hibernate annotations used for mapping embedded objects.

**@Embeddable** is applied to a class that is embedded by another entity object. On the other hand, **@Embedded** is used on a field that is of another class type, which is marked as **@Embeddable**.

The difference between them is that **@Embeddable** is used in a class whose instances we want to embed in another class, whereas **@Embedded** is used in a class that embeds instances of another class.

For example:
@Embeddable
public class Address {
    private String street;
    private String city; 
    //.. other fields
}

In the example above, the **Address** class is embeddable – it is marked with **@Embeddable**.

@Entity
public class User {
    @Id
    private Long id;

    //...

    @Embedded
    private Address shippingAddress;
}

In the **User** class, the **shippingAddress** field is embedded – it is marked with **@Embedded**.

In summary, @Embeddable is used to indicate that a class can be embedded in other entities, while @Embedded is used to specify which elements in an entity should be embedded.

Download IT Flashcards App Now

Empower your IT learning journey with the ultimate flashcard system. From basic programming principles to mastering advanced technologies, IT Flashcards is your passport to IT excellence. Download now and unlock your potential in today's competitive tech landscape.

Home Blog Sponsors Contact Privacy Policy Terms of Service

Copyright © 2025 IT Flashcards