2. Hibernate
Hibernate is an Object-Relational Mapping (ORM) framework for Java. It
provides a way to map Java objects to relational database tables, and vice versa.
It also provides a powerful query language (HQL) for retrieving and
manipulating data, as well as a number of other features for managing the
persistence of Java objects. Hibernate is commonly used in enterprise
applications to abstract away the complexity of working with a relational
database and to provide a more object-oriented way of working with data.
3. How to create Hibernate Project?
1. Create a maven project.
2. In pom.xml add jdbc and hibernate dependencies using below link:
a. https://mvnrepository.com/
3. Create hibernate configuration xml in src/main/java for database
configuration.
4. Create your required classes in you package.
4. What is Catalog,Group id and Artifact id
In a Maven project, a "catalog" refers to a collection of related projects. The "groupId" and
"artifactId" are two important pieces of information used to identify a project within a catalog.
The "groupId" is a unique identifier for a project's group or organization. It typically follows a
reverse-domain-name convention, such as com.example.myproject. The "groupId" is used to
group related projects together, and it forms part of the project's fully-qualified name.
The "artifactId" is a unique identifier for a specific project within a group. The "artifactId" is used
to distinguish different versions of the same project, and it forms part of the project's
fully-qualified name.
Together, the "groupId" and "artifactId" form the "coordinates" of a project in a Maven catalog.
The coordinates are used to specify a project's dependencies, and they are also used to locate the
project's artifacts (e.g. JAR files) in a Maven repository.
5. Use of pom.xml
The pom.xml (short for "Project Object Model") is an XML file that contains information about a Maven
project. It is used to configure the project's build, dependencies, plugins, and other settings. The pom.xml
is typically located in the root directory of a Maven project.
The main use of the pom.xml is to define the project's dependencies. A dependency is a library or module
that a project needs in order to build and run correctly. In the pom.xml, dependencies are defined in the
<dependencies> section. Each dependency is defined by its groupId, artifactId, and version. Maven uses
this information to locate the dependency in a Maven repository and to download the dependency to the
local machine.
The pom.xml also contains information about the project's build settings, such as the project's name,
description, and version. Plugins are used for tasks such as compiling code, running tests, and creating a
JAR or WAR file.
In short, the pom.xml serves as a central place to configure all aspects of a Maven project, and it is
essential for the proper functioning of a Maven build.
6. Use of
hibernate.cfg.xml
The hibernate.cfg.xml (also known as the Hibernate
configuration file) is an XML file that contains
configuration settings for a Hibernate-based
application. It is typically located in the classpath of
the application and is used to configure the Hibernate
SessionFactory, which is the main entry point to the
Hibernate API.
The main use of the hibernate.cfg.xml is to configure
the connection to the database. It contains
information such as the JDBC driver class, the
database URL, the username, and the password. It
also contains information about the dialect of SQL
that the database uses, which allows Hibernate to
generate appropriate SQL statements for the
database.
The hibernate.cfg.xml also contains other settings
related to Hibernate's behavior, such as the cache
strategy, the current session context, and the
transaction factory. It also contains the mapping of
Java classes to database tables, through the use of
<mapping> element or by specifying the package
containing the entity classes.
7. hibernate.cfg.xml
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
<!-- 'show_sql' set true to check sql statements on console else set to false
-->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- use 'create' to Create tables on application startup else use 'update'-->
<property name="hbm2ddl.auto">update</property>
</session-factory>
8. Hibernate classes overview
The Configuration object is used to configure the SessionFactory.
The SessionFactory is used to create and manage sessions.
A session represents a single unit of work with the database and is used to perform operations on
the database and on persistent objects.
The SessionFactory is a thread-safe object that is responsible for creating and managing sessions
and other objects used by Hibernate.
The Configuration is used to create the SessionFactory and it is used to specify the properties of
the SessionFactory, such as the database connection properties and Hibernate properties.