Register   Login   About   Study   Enterprise   Share
Internet / AI Technology University (ITU/AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 2. Java and Databases >> 2.5. Data Handling Frameworks
Current Topic: 2.5.1. Hibernate Introduction
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Hibernate is one of the first and one of the best Object-Relational Mapping (ORM) frameworks.
The main features:
-Uses Object-Oriented (OO) query language called Hibernate Query Language (HQL)
-Uses objects instead of tables and fields instead of columns
-Provides object-to-relational mapping for most DBs
-Separates data layer from business logics
-Uses DB connection info to retrieve DB schema
-Generates Data Access Objects (DAO) beans with data fields mapping table columns
-Generates Insert/Update/Delete/Select statements for DB tables

For example, using Hibernate we would create the class Accounts to map this class to the Accounts table.

public class Accounts {
// data
private String email;
private String password;
private String enabled;
// get and set methods for each variable
// usually done with Eclipse – right mouse click on the Eclipse source page – SOURCE – Generate Getters and Setters
}




Then, we would create the mapping file:













We can save this file in the resources folder of the project as Accounts.hbm.xml and provide a reference to this file in Hibernate configuration, which illustrated below as hibernate.cfg.xml file.


"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">






oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:its
its
its


10


org.hibernate.dialect.OracleDialect


thread






Was it clear so far?



Note that this configuration file includes all details related to the database in the SessionFactory scope. SessionFactory is responsible for a specific database. For several databases, we would use several configurations.
Property elements include connection data, size of the connection pool (10 simultaneous connections), and point to a specific dialect, in this case, Oracle, to properly address data types and more database
There is the line that enables Hibernate's automatic session management. And the last line points to the Accounts mapping file. We would also save this file in the project - resources directory hibernate.cfg.xml.
In our Java source we will be able to handle data with Java code similar to the example below. Hibernate provides necessary SQL for us behind the scene.


Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

Accounts anAccount = new Accounts();
anAccount.setEmail(email);
anAccount.setPassword(password);
anAccount.setEnabled(enabled);
anAccount.setLastUpdate(dateAsString);
session.save(anAccount);

session.getTransaction().commit();


To exercise Hibernate we need its library ... and we need the skills of working with Java libraries. We are not there yet, but it is coming soon…
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>public class Accounts {
<br/>   // data
<br/>   private String email;
<br/>   private String password;
<br/>   private String enabled;
<br/>   // get and set methods for each variable
<br/>   // usually done with Eclipse – right mouse click on the Eclipse source page – SOURCE – Generate Getters and Setters
<br/>}
<br/>




Then, we would create the mapping file:
<br/><hibernate-mapping package="org.hibernate.tutorial.domain">
<br/>
<br/>    <class name="Accounts" table="Accounts">
<br/>        <property name="email"/>
<br/>        <property name="password"/>
<br/>        <property name="enabled"/>
<br/>        <property name="lastUpdate"/>
<br/>    </class>
<br/>
<br/></hibernate-mapping>
<br/>


We can save this file in the resources folder of the project as Accounts.hbm.xml and provide a reference to this file in Hibernate configuration, which illustrated below as hibernate.cfg.xml file.
<br/><?xml version='1.0' encoding='utf-8'?>
<br/><!DOCTYPE hibernate-configuration PUBLIC
<br/>        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<br/>        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<br/>
<br/><hibernate-configuration>
<br/>
<br/>    <session-factory>
<br/>
<br/>        <!-- Database connection settings -->
<br/>        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<br/>        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:its</property>
<br/>        <property name="connection.username">its</property>
<br/>        <property name="connection.password">its</property>
<br/>
<br/>        <!-- JDBC connection pool (use the built-in) -->
<br/>        <property name="connection.pool_size">10</property>
<br/>
<br/>        <!-- SQL dialect -->
<br/>        <property name="dialect">org.hibernate.dialect.OracleDialect</property>
<br/>
<br/>        <!-- Enable Hibernate's automatic session context management -->
<br/>        <property name="current_session_context_class">thread</property>
<br/>
<br/>        <mapping resource="resources/Accounts.hbm.xml"/>
<br/>
<br/>    </session-factory>
<br/>
<br/></hibernate-configuration>
<br/>






Was it clear so far?




Note that this configuration file includes all details related to the database in the SessionFactory scope. SessionFactory is responsible for a specific database. For several databases, we would use several configurations.
Property elements include connection data, size of the connection pool (10 simultaneous connections), and point to a specific dialect, in this case, Oracle, to properly address data types and more database
There is the line that enables Hibernate's automatic session management. And the last line points to the Accounts mapping file. We would also save this file in the project - resources directory hibernate.cfg.xml.
In our Java source we will be able to handle data with Java code similar to the example below. Hibernate provides necessary SQL for us behind the scene.

<br/>        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
<br/>
<br/>        session.beginTransaction();
<br/>
<br/>        Accounts anAccount = new Accounts();
<br/>        anAccount.setEmail(email);
<br/>        anAccount.setPassword(password);
<br/>        anAccount.setEnabled(enabled);
<br/>        anAccount.setLastUpdate(dateAsString);
<br/>        session.save(anAccount);
<br/>
<br/>        session.getTransaction().commit();
<br/>


To exercise Hibernate we need its library ... and we need the skills of working with Java libraries. We are not there yet, but it is coming soon…

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy