• +91 9723535972
  • info@interviewmaterial.com

Hibernate Interview Questions and Answers

Hibernate Interview Questions and Answers

Question - 31 : - How can I convert the type of a property to/from the database column type?

Answer - 31 : - Use a UserType.

Question - 32 : - How can I get access to O/R mapping information such as table and column names at runtime?

Answer - 32 : - This information is available via the Configuration object. For example, entity mappings may be obtained using Configuration.getClassMapping(). It is even possible to manipulate this metamodel at runtime and then build a new SessionFactory.

Question - 33 : - How can I create an association to an entity without fetching that entity from the database (if I know the identifier)?

Answer - 33 : - If the entity is proxyable (lazy="true"), simply use load(). The following code does not result in any SELECT statement: Item itemProxy = (Item) session.load(Item.class, itemId);Bid bid = new Bid(user, amount, itemProxy);session.save(bid);

Question - 34 : - How can I manipulate mappings at runtime?

Answer - 34 : - You can access (and modify) the Hibernate metamodel via the Configuration object, using getClassMapping(), getCollectionMapping(), etc. Note that the SessionFactory is immutable and does not retain any reference to the Configuration instance, so you must re-build it if you wish to activate the modified mappings.

Question - 35 : - How can I retrieve the identifier of an associated object, without fetching the association?

Answer - 35 : - Just do it. The following code does not result in any SELECT statement, even if the item association is lazy. Long itemId = bid.getItem().getId(); This works if getItem() returns a proxy and if you mapped the identifier property with regular accessor methods. If you enabled direct field access for the id of an Item, the Item proxy will be initialized if you call getId(). This method is then treated like any other business method of the proxy, initialization is required if it is called.

Question - 36 : - How can I insert XML data into Oracle using the xmltype() function?

Answer - 36 : - Specify custom SQL INSERT (and UPDATE) statements using and in Hibernate3, or using a custom persister in Hibernate 2.1. You will also need to write a UserType to perform binding to/from the PreparedStatement.  

Question - 37 : - How can I execute arbitrary SQL using Hibernate?

Answer - 37 : - PreparedStatement ps = session.connection().prepareStatement(sqlString); Or, if you wish to retrieve managed entity objects, use session.createSQLQuery(). Or, in Hibernate3, override generated SQL using , , and in the mapping document. I want to call an SQL function from HQL, but the HQL parser does not recognize it! Subclass your Dialect, and call registerFunction() from the constructor.

Question - 38 : - What are the major advantages of Hibernate Framework?

Answer - 38 : -

  • It is open-sourced and lightweight.
  • Performance of Hibernate is very fast.
  • Helps in generating database independant queries.
  • Provides facilities to automatically create a table.
  • It provides query statistics and database status.

Question - 39 : - What are the advantages of using Hibernate over JDBC?

Answer - 39 : -

Major advantages of using Hibernate over JDBC are:

  • Hibernate eliminates a lot of boiler-plate code that comes with JDBC API, the code looks cleaner and readable.
  • This Java framework supports inheritance, associations, and collections. These features are actually not present in JDBC.
  • HQL (Hibernate Query Language) is more object-oriented and close to Java. But for JDBC, you need to write native SQL queries.
  • Hibernate implicitly provides transaction management whereas, in JDBC API, you need to write code for transaction management using commit and rollback.
  • JDBC throws SQLException that is a checked exception, so you have to write a lot of try-catch block code. Hibernate wraps JDBC exceptions and throw JDBCException or HibernateException which are the unchecked exceptions, so you don’t have to write code to handle it has built-in transaction management which helps in removing the usage of try-catch blocks.

Question - 40 : - What is an ORM tool?

Answer - 40 : -

It is basically a technique that maps the object that is stored in the database. An ORM tool helps in simplifying data creation, manipulation, and access. It internally uses the Java API to interact with the databases.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners