Question | Answer | Example Code |
---|---|---|
What is the purpose of the @Entity annotation in Hibernate? | Marks a class as a Hibernate entity, mapping it to a database table. | java @Entity public class Employee { @Id @GeneratedValue private int id; private String name; } |
What is the purpose of the @Table annotation in Hibernate? | Specifies the table name and schema. | java @Entity @Table(name = "employees") public class Employee { @Id @GeneratedValue private int id; } |
What is the difference between @Id and @GeneratedValue? | @Id marks a field as the primary key, while @GeneratedValue defines how the key is generated. | java @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; |
What is the purpose of @Column annotation? | Defines column properties such as name, length, and uniqueness. | java @Column(name = "emp_name", length = 50, unique = true) private String name; |
What is the difference between @OneToOne and @OneToMany relationships? | @OneToOne: One entity related to one entity. @OneToMany: One entity related to multiple entities. | java @OneToOne private Address address; @OneToMany(mappedBy = "department") private List<Employee> employees; |
What is the difference between @ManyToOne and @ManyToMany relationships? | @ManyToOne: Multiple entities linked to a single entity. @ManyToMany: Many-to-many relationship between entities. | java @ManyToOne private Department department; @ManyToMany private List<Course> courses; |
What is the use of @JoinColumn in Hibernate? | Defines the foreign key column in a relationship. | java @OneToOne @JoinColumn(name = "address_id") private Address address; |
What is optimistic and pessimistic locking in Hibernate? | Optimistic Locking: Uses versioning (@Version) to prevent conflicts. Pessimistic Locking: Locks a record to prevent concurrent modifications. | java @Version private int version; |
What is the use of @Embedded and @Embeddable annotations? | Enables embedding one class into another without creating a separate table. | java @Embeddable public class Address { private String city; private String state; } @Entity public class Employee { @Embedded private Address address; } |
What is a Named Query in Hibernate? | A predefined query that can be reused. | java @NamedQuery(name = "Employee.findAll", query = "FROM Employee") public class Employee { } Query query = session.getNamedQuery("Employee.findAll"); List<Employee> employees = query.list(); |
What is a Native SQL query in Hibernate? | Executes raw SQL queries. | java Query query = session.createSQLQuery("SELECT * FROM employees").addEntity(Employee.class); List<Employee> employees = query.list(); |
What are the different fetching strategies in Hibernate? | Lazy Fetching: Loads data when needed. Eager Fetching: Loads related data immediately. | java @OneToMany(fetch = FetchType.LAZY) @OneToMany(fetch = FetchType.EAGER) |
What is Hibernate Interceptor? | Allows custom logic before or after database operations. | java class MyInterceptor extends EmptyInterceptor { @Override public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { System.out.println("Entity deleted: " + entity); } } Session session = sessionFactory.withOptions().interceptor(new MyInterceptor()).openSession(); |
What is the difference between clear(), evict(), and close() in Hibernate? | clear(): Removes all objects from session cache. evict(Object obj): Removes a specific object from cache. close(): Closes session completely. | java session.evict(employee); session.clear(); session.close(); |
What is the difference between Hibernate and JPA? | JPA is a specification for ORM, while Hibernate is an implementation of JPA with extra features. | java @Entity public class Employee { } |
No comments