Hibernate

[vc_row full_width=”stretch_row” css=”.vc_custom_1559286923229{background-color: #f6f6f7 !important;}”][vc_column width=”1/2″][vc_tta_accordion color=”peacoc” active_section=”1″][vc_tta_section title=”What is JDBC?” tab_id=”1559286383409-ab730398-6c03″][vc_column_text]

JDBC stands for Java Database Connectivity and provides a set of Java API for accessing the relational databases from Java program. These Java APIs enables Java programs to execute SQL statements and interact with any SQL compliant database.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is ORM?” tab_id=”1559286522681-3bf94e12-e7b7″][vc_column_text]

ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C# etc.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the advantages of ORM over JDBC?” tab_id=”1561382593569-b1979b66-b066″][vc_column_text]An ORM system has following advantages over plain JDBC[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is Hibernate?” tab_id=”1561382595833-dd54d407-26c0″][vc_column_text]Hibernate is an Object-Relational Mapping(ORM) solution for JAVA and it raised as an open source persistent framework created by Gavin King in 2001. It is a powerful, high performance Object-Relational Persistence and Query service for any Java Application.

Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieve the developer from 95% of common data persistence related programming tasks.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the advantages of using Hibernate?” tab_id=”1561382597303-5168678c-55b9″][vc_column_text]Following are the advantages of using Hibernate.

  • Hibernate takes care of mapping Java classes to database tables using XML files and without writing any line of code.
  • Provides simple APIs for storing and retrieving Java objects directly to and from the database.
  • If there is change in Database or in any table then the only need to change XML file properties.
  • Abstract away the unfamiliar SQL types and provide us to work around familiar Java Objects.
  • Hibernate does not require an application server to operate.
  • Manipulates Complex associations of objects of your database.
  • Minimize database access with smart fetching strategies.
  • Provides Simple querying of data.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Name some of the databases that hibernate supports.” tab_id=”1561382598718-1fee5a6b-29dd”][vc_column_text]Hibernate supports almost all the major RDBMS. Following is list of few of the database engines supported by Hibernate.

  • HSQL Database Engine
  • DB2/NT
  • MySQL
  • PostgreSQL
  • FrontBase
  • Oracle
  • Microsoft SQL Server Database
  • Sybase SQL Server
  • Informix Dynamic Server

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Name some of the java based tools/frameworks that supports hibernate integration.” tab_id=”1561382602352-48d936eb-64df”][vc_column_text]Hibernate supports a variety of other technologies, including the following −

  • XDoclet Spring
  • J2EE
  • Eclipse plug-ins
  • Maven

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the key components/objects of hibernate?” tab_id=”1561382603416-a2e0c7df-e6f8″][vc_column_text]Following are the key components/objects of Hibernate −

  • Configuration − Represents a configuration or properties file required by the Hibernate.
  • SessionFactory − Configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated.
  • Session − Used to get a physical connection with a database.
  • Transaction − Represents a unit of work with the database and most of the RDBMS supports transaction functionality.
  • Query − Uses SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.
  • Criteria − Used to create and execute object oriented criteria queries to retrieve objects.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the two key components of a hibernate configuration object?” tab_id=”1561382604362-41fc1dd4-d143″][vc_column_text]The Configuration object provides two keys components −

    • Database Connection − This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.
    • Class Mapping Setup

This component creates the connection between the Java classes and database tables.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is a configuration object in hibernate?” tab_id=”1561382605426-bedbe54f-bb01″][vc_column_text]

The Configuration object is the first Hibernate object you create in any Hibernate application and usually created only once during application initialization. It represents a configuration or properties file required by the Hibernate.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is a SessionFactory in hibernate?” tab_id=”1561382606689-6de5471f-2224″][vc_column_text]Configuration object is used to create a SessionFactory object which inturn configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated. The SessionFactory is a thread safe object and used by all the threads of an application.

The SessionFactory is heavyweight object so usually it is created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file. So if you are using multiple databases then you would have to create multiple SessionFactory objects.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is Session in hibernate?” tab_id=”1561382593038-8e7f1218-a7ac”][vc_column_text]A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.

The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is Transaction in hibernate?” tab_id=”1561382592598-bfa5a635-55f4″][vc_column_text]A Transaction represents a unit of work with the database and most of the RDBMS supports transaction functionality. Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA).

This is an optional object and Hibernate applications may choose not to use this interface, instead managing transactions in their own application code.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is Query in hibernate?” tab_id=”1561382592102-02f6742f-af9d”][vc_column_text]

Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is Criteria in hibernate?” tab_id=”1561382591605-a59a34b3-da74″][vc_column_text]

Criteria object are used to create and execute object oriented criteria queries to retrieve objects.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Name some of the properties you would require to configure for a databases in a standalone situation.” tab_id=”1561382591221-5a07bad8-cf1f”][vc_column_text]

Sr.No. Properties & Description
1 hibernate.dialect

This property makes Hibernate generate the appropriate SQL for the chosen database.

2 hibernate.connection.driver_class

The JDBC driver class.

3 hibernate.connection.url

The JDBC URL to the database instance.

4 hibernate.connection.username

The database username.

5 hibernate.connection.password

The database password.

6 hibernate.connection.pool_size

Limits the number of connections waiting in the Hibernate database connection pool.

7 hibernate.connection.autocommit

Allows autocommit mode to be used for the JDBC connection.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the three states of a persistent entity at a given point in time?” tab_id=”1561382590677-cc47cbcf-ee26″][vc_column_text]Instances may exist in one of the following three states at a given point in time −

  • transient − A new instance of a a persistent class which is not associated with a Session and has no representation in the database and no identifier value is considered transient by Hibernate.
  • persistent − You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value and is associated with a Session.
  • detached − Once we close the Hibernate Session, the persistent instance will become a detached instance.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml is used to map a Java class property to a column in the database table?” tab_id=”1561382590095-0dd9d5a4-e5bf”][vc_column_text]

The <property> element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml is used to map a java.util.Set property in hibernate?” tab_id=”1561382589718-2e1a6ceb-a239″][vc_column_text]

This is mapped with a <set> element and initialized with java.util.HashSet.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml is used to map a java.util.SortedSet property in hibernate?” tab_id=”1561382589333-47659694-5e1f”][vc_column_text]

This is mapped with a <set> element and initialized with java.util.TreeSet. The sort attribute can be set to either a comparator or natural ordering.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml is used to map a java.util.Map property in hibernate?” tab_id=”1561382588926-dd3c13fa-09be”][vc_column_text]

This is mapped with a <map> element and initialized with java.util.HashMap.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml is used to map a java.util.SortedMap property in hibernate?” tab_id=”1561382588517-7fc83786-ee5a”][vc_column_text]

This is mapped with a <map> element and initialized with java.util.TreeMap. The sort attribute can be set to either a comparator or natural ordering.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is one-to-one association?” tab_id=”1561382588060-9c018b10-4231″][vc_column_text]A one-to-one association is similar to many-to-one association with a difference that the column will be set as unique. For example an address object can be associated with a single employee object.

<many-to-one> element is used to define one-to-one association. The name attribute is set to the defined variable in the parent class. The column attribute is used to set the column name in the parent table which is set to unique so that only one object can be associated with an other object.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is one-to-many association?” tab_id=”1561382587557-eee3186e-5475″][vc_column_text]In One-to-Many mapping association, an object can be can be associated with multiple objects. For example Employee object relates to many Certificate objects.

A One-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element.

<one-to-many> element of set element indicates that one object relates to many other objects.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is many-to-many association?” tab_id=”1561382583069-d03ea374-27b7″][vc_column_text]A Many-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element.

<many-to-many> element indicates that one object relates to many other objects and column attributes are used to link intermediate column.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Section” tab_id=”1562663806431-8c7a083c-6e0b”][/vc_tta_section][/vc_tta_accordion][/vc_column][vc_column width=”1/2″][vc_tta_accordion color=”peacoc” active_section=”1″][vc_tta_section title=”What is the purpose of Session.beginTransaction() method?” tab_id=”1561382561432-7f73ef2a-cc67″][vc_column_text]

Session.beginTransaction method begins a unit of work and returns the associated Transaction object.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to add a criteria to a query?” tab_id=”1561382561455-654071d3-eb53″][vc_column_text]

Session.createCriteria creates a new Criteria instance, for the given entity class, or a superclass of an entity class.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to create a HQL query?” tab_id=”1561382611424-56181e07-6453″][vc_column_text]

Session.createQuery creates a new instance of Query for the given HQL query string.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to create a SQL query?” tab_id=”1561382613753-7c9c9136-4ca1″][vc_column_text]

Session.createSQLQuery creates a new instance of SQLQuery for the given SQL query string.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to remove a persistent instance from the datastore?” tab_id=”1561382614729-6b63842b-62b1″][vc_column_text]

Session.delete removes a persistent instance from the datastore.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to get a persistent instance from the datastore?” tab_id=”1561382615672-42dd66a8-6425″][vc_column_text]

Session.get returns the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to save the state of the given instance from the underlying database?” tab_id=”1561382616984-e392adb7-34cd”][vc_column_text]

Session.save saves the state of the given instance from the underlying database.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to re-read the state of the given instance from the underlying database?” tab_id=”1561382618152-4fb6fc3a-9883″][vc_column_text]

Session.refresh re-reads the state of the given instance from the underlying database.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to update the state of the given instance from the underlying database?” tab_id=”1561382619930-3767e1f0-f3d6″][vc_column_text]

Session.update updates the state of the given instance from the underlying database.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which method is used to save or update the state of the given instance from the underlying database?” tab_id=”1561382620762-261798c4-b2da”][vc_column_text]

Session.saveOrUpdate either saves(Object) or updates(Object) the given instance.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are persistent classes in hibernate?” tab_id=”1561382621738-44e10b8a-ca7d”][vc_column_text]

Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the best practices that hibernate recommends for persistent classes.” tab_id=”1561382622978-99f18b2d-fe4d”][vc_column_text]There are following main rules of persistent classes, however, none of these rules are hard requirements.

  • All Java classes that will be persisted need a default constructor.
  • All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database. This property maps to the primary key column of a database table.
  • All attributes that will be persisted should be declared private and have getXXX and setXXX methods defined in the JavaBean style.
  • A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.
  • All classes that do not extend or implement some specialized classes and interfaces required by the EJB framework.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Where Object/relational mappings are defined in hibernate?” tab_id=”1561382623945-e109c4d3-ece0″][vc_column_text]

An Object/relational mappings are usually defined in an XML document. This mapping file instructs Hibernate how to map the defined class or classes to the database tables. We should save the mapping document in a file with the format <classname>.hbm.xml.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is root node of hbm.xml?” tab_id=”1561382625642-843354b1-1f56″][vc_column_text]

The mapping document is an XML document having <hibernate-mapping>as the root element which contains all the <class> elements.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml defines a specific mappings from a Java classes to the database tables?” tab_id=”1561382626563-0fa3f678-a5bf”][vc_column_text]

The <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml defines maps the unique ID attribute in class to the primary key of the database table?” tab_id=”1561382627658-bb4ddb6b-277e”][vc_column_text]

The <id> element maps the unique ID attribute in class to the primary key of the database table. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml is used to automatically generate the primary key values?” tab_id=”1561382628592-dd61fe2e-19d3″][vc_column_text]

The <generator> element within the id element is used to automatically generate the primary key values. Set the class attribute of the generator element is set to native to let hibernate pick up either identity, sequence or hilo algorithm to create primary key depending upon the capabilities of the underlying database.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml is used to map a java.util.List property in hibernate?” tab_id=”1561382630393-e350687e-823e”][vc_column_text]

This is mapped with a <list> element and initialized with java.util.ArrayList.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which element of hbm.xml is used to map a java.util.Collection property in hibernate?” tab_id=”1561382632977-3b296953-06eb”][vc_column_text]

This is mapped with a <bag> or <ibag> element and initialized with java.util.ArrayList.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is many-to-one association?” tab_id=”1561382634764-ad5070ba-db90″][vc_column_text]A many-to-one association is the most common kind of association where an Object can be associated with multiple objects. For example a same address object can be associated with multiple employee objects.

<many-to-one> element is used to define many-to-one association. The name attribute is set to the defined variable in the parent class. The column attribute is used to set the column name in the parent table.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Is SessionFactory a thread-safe object?” tab_id=”1561382636355-5dfe038c-83df”][vc_column_text]

Yes, SessionFactory is a thread-safe and can be accessed by multiple threads simultaneously.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Is Session a thread-safe object?” tab_id=”1561382638762-3bfa136c-84a1″][vc_column_text]

No, Session is not thread-safe.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is the difference between save() and persist() methods of session object?” tab_id=”1561382649403-b75ce50f-0f5c”][vc_column_text]

session.save saves the object and returns the id of the instance whereas persist do not return anything after saving the instance.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is the difference between get() and load() methods of session object?” tab_id=”1561382651188-733a0ad7-4f7c”][vc_column_text]There are following differences between get() and load() methods.

  • get() returns null if no data is present where as load throws ObjectNotFoundException exception in such case.
  • get() always hits the database whereas load() method doesn’t hit the database.
  • get() returns actual object whereas load() returns proxy object.
  • A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.
  • All classes that do not extend or implement some specialized classes and interfaces required by the EJB framework.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is lazy loading?” tab_id=”1561382674843-1f0b68aa-358c”][vc_column_text]

Lazy loading is a technique in which objects are loaded on demand basis. Since Hibernate 3, lazy loading is by default, enabled so that child objects are not loaded when parent is loaded.

[/vc_column_text][/vc_tta_section][/vc_tta_accordion][/vc_column][/vc_row][vc_row full_width=”stretch_row” overlay_color=”rgba(12,12,12,0.58)” css=”.vc_custom_1528340415800{padding-bottom: 104px !important;background-image: url(https://wordpresslms.thimpress.com/wp-content/uploads/sites/4/2017/06/layer-532.jpg?id=231) !important;background-position: center !important;background-repeat: no-repeat !important;background-size: cover !important;}” el_class=”overflow-visible become-teacher”][vc_column width=”1/2″][vc_column_text el_class=”align-right”]

Please login to send your request!
[/vc_column_text][/vc_column][vc_column width=”1/2″ el_class=”talk”][vc_column_text el_class=”thim-content-talk”]I’m a Copywriter in a Digital Agency, I was searching for courses that’ll help me broaden my skill set. Before signing up for Rob’s.[/vc_column_text][/vc_column][/vc_row]

WhatsApp us