Sunday, 14 April 2019

Hibernate Interview Questions For Experienced

Question # 11 What is a polymorphic association?

Answer # Polymorphic association is a term used in discussions of Object-Relational Mapping with respect to the problem of representing in the relational database domain, a relationship from one class to multiple classes. In statically typed languages such as Java these multiple classes are subclasses of the same superclass.

Question # 12 What is the difference between session and Sessionfactory in hibernate?

Answer # SessionFactory is Hibernate’s concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. Sessions are opened by a SessionFactory and then are closed when all work is complete.

Question # 13 What is the difference between load and get method in hibernate?

Answer # Session.load(): It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. If no row found , it will throws an ObjectNotFoundException.

Question # 14 Is hibernate Sessionfactory Singleton?

Answer # SessionFactory is also thread safe so only one thread can execute at a time its code. The instance of sessionFactory is heavyweighted because it contains connection, hibernate configuration, mapping files, location path so if you create number of instance of sessionFactory then your code becomes very heavy.

Question # 15 What are the configuration files in hibernate?

Answer # Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate.properties, or as an XML file named hibernate.cfg.xml.

Questions # 16 What is the use of dialect in hibernate?

Answer # Dialect means “the variant of a language”. Hibernate, as we know, is database agnostic. It can work with different databases. However, databases have proprietary extensions/native SQL variations, and set/sub-set of SQL standard implementations. Therefore at some point hibernate has to use database specific SQL.

Question # 17 What is the use of Show_sql in hibernate?

Answer # Hibernate has build-in a function to enable the logging of all the generated SQL statements to the console. You can enable it by add a “show_sql” property in the Hibernate configuration file “ hibernate.cfg.xml “. This function is good for basic troubleshooting, and to see what’s Hibernate is doing behind.

Question # 18 What is hibernate proxy and how it helps in lazy loading?

Answer # A proxy is a subclass implemented at runtime. Hibernate creates a proxy (a subclass of the class being fetched) instead of querying the database directly, and this proxy will load the “real” object from the database whenever one of its methods is called.

Question # 19 Is session is thread safe in hibernate?

Answer # SessionFactory is Hibernates concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. Why to make session object thread safe if we already have a SessionFactory(immutable) object.

Question # 20 What is the use of configuration in hibernate?

Answer # The org.hibernate.cfg.Configuration is used to build an immutable org.hibernate.SessionFactory . The mappings are compiled from various XML mapping files. A org.hibernate.cfg.Configuration also allows you to specify configuration properties.

Question # 21 What is criteria in hibernate?

Answer # In Hibernate, the Criteria API helps us build criteria query objects dynamically. Criteria is a another technique of data retrieval apart from HQL and native SQL queries. The primary advantage of the Criteria API is that it is intuitively designed to manipulate data without using any hard-coded SQL statements.

Question # 22 What is the difference between lazy and eager loading in hibernate?

Answer # All data is fetched when eager marked data in the object when session is connected. However, in case of lazy loading strategy, lazy loading marked object does not retrieve data if session is disconnected (after session.close() statement). All that can be made by hibernate proxy.

Question # 23 Is Sessionfactory immutable?

Answer # The internal state of a SessionFactory is immutable. Most problems with concurrency occur due to sharing of objects with mutable state. Once the object is immutable, its internal state is setted on creation and cannot be changed. So many threads can access it concurrently and request for sessions.

Question # 24 Is Hibernate configuration file mandatory?

Answer # Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate.cfg.xml file which is exactly what the configure() method does. No, it’s not mandatory to use hibernate.cfg.xml. Just don’t use .configure().

Question # 25 What is meant by annotation in hibernate?

Answer # Hibernate annotations are the newest way to define mappings without the use of XML file. You can use annotations in addition to or as a replacement of XML mapping metadata. Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping.

Question # 26 What does hibernate.hbm2ddl.auto create means?

Answer # hibernate.hbm2ddl.auto. Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop , the database schema will be dropped when the SessionFactory is closed explicitly.

Question # 27 What is the meaning of persistence in hibernate?

Answer # When a POJO instance is in session scope, it is said to be persistent i.e hibernate detects any changes made to that object and synchronizes it with database when we close or flush the session. And about hibernate.properties and XML Mapping.

Question # 28 How does Hibernate proxy work?

Answer # When a method is invoked on the object, Hibernate will fetch the data from the column and populate the object. This is the proxy mechanism. To add this new behavior (the loading of the data when a method is invoked), Hibernate will create a dynamic subclass of Person using CGLib and add the desired functionality.

Question # 29 What is first level cache in hibernate?

Answer # First level cache is associated with “session” object. The scope of cache objects is of session. First level cache is enabled by default and you can not disable it. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.

Question # 30 What is the use of Session in hibernate?

Answer # The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service. The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)

No comments:

Post a Comment

Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...