Prev Next

Hibernate / Hibernate Interview Questions III

1. How to configure application server JNDI DataSource with Hibernate framework?

For web applications, it is ideal to have the servlet container manage the connection pool. This is achieved by defining JNDI resource for DataSource and use it in the web application....

Read full answer

2. How to implement Joins in Hibernate?

There are different ways to implement joins in Hibernate. Using associations such as one-to-one, one-to-many, Using JOIN in the HQL query, and by using native sql query with join keyword.

Read full answer

3. Explain the general flow of hibernate.

Load configuration file and create instance of configuration class. Using configuration object, create SessionFactory object.

Read full answer

4. What is cascade in Hibernate?

Cascading consists in propagating the Parent entity state transition to one or more Child entities, and it can be used for both unidirectional and bidirectional associations.

Read full answer

5. How to include query hint in Hibernate criteria?

Using Criteria addQueryHint method, we can specify database specific hint. Criteria addQueryHint(String hint)

Read full answer

6. Difference in positional parameter in JDBC create Query and Hibernate.

In JDBC positional parameter index starts at 1, while it starts at 0 in Hibernate create sql query.

Read full answer

7. Explain Formula annotation in Hibernate.

@Formula annotation is used to calculate a given entity attribute using an SQL query expression. It defines a formula (derived value) which is a SQL fragment that acts as a @Column alternative in most cases....

Read full answer

8. How to limit query results in HQL?

Use Query.setMaxResults() to limit the result.

Read full answer

9. How do I order the selected entity when using CriteriaQuery?

You can define an ORDER BY clause with the orderBy method of the CriteriaQuery interface and the asc or desc method of the CriteriaBuilder interface. CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> cq = cb.createQuery(Employee.class); Root<Book> root = cq.from(Employee.class

Read full answer

10. Explain JPA Entity Object Life Cycle.

The life cycle of entity objects consists of four states: New, Managed, Removed and Detached . When an entity object is initially created its state is New ....

Read full answer

11. Explain the role of JMX in hibernate.

Java Applications and components are managed in hibernate by a standard API called JMX API. JMX provides tools for development of efficient and robust distributed, web based solutions.

Read full answer

12. Does hibernate support polymorphism?

Yes, hibernate fully supports polymorphism. Polymorphism queries and polymorphism associations are supported in all mapping strategies of hibernate.

Read full answer

13. Mention some of the Hibernate Best Practices.

Identify natural keys for all entities, and map them. Place each class mapping in its own file....

Read full answer

14. Difference between transient and detached objects in hibernate.

Transient objects do not have association with the database and session objects. They are simple objects and not persisted to the database....

Read full answer

15. What happens when a transient mapped object is passed onto a Sessions save?

It has no effect and the transient object retains its state.

Read full answer

16. Explain cascade and inverse properties in one-many mapping.

There is no relationship between cascade and inverse, both serves different purpose.

Read full answer

17. What is inverse = true in hibernate mapping?

It defines which side is the parent or the relationship owner for the two entities. Hence, inverse="true" in a Hibernate mapping shows that this designated class is the relationship owner; while the other class is the child.

Read full answer

18. What is the default fetch type in Hibernate?

The JPA @ManyToOne and @OneToOne annotations are fetched EAGERly by default, while the @OneToMany and @ManyToMany relationships are LAZY by default.

Read full answer

19. What is mappedBy attribute in hibernate?

The attribute mappedBy indicates that the entity in this side is the inverse of the relationship, and the owner resides in the "other" entity. This also means that you can access the other table from the class which you've annotated...

Read full answer

20. Difference between mappedBy and joinColumn In hibernate.

mappedBy. joinColumn....

Read full answer

«
»

Comments & Discussions