Presentation is loading. Please wait.

Presentation is loading. Please wait.

A very brief introduction

Similar presentations


Presentation on theme: "A very brief introduction"— Presentation transcript:

1 A very brief introduction
Hibernate A very brief introduction

2 Topics to discuss Overview Database Basics & RDBMS JDBC ORM Hibernate

3 Overview: Where are we? Our web project consists of :
A bunch of static design files (such as HTML, CSS) A bunch of java classes that implement logical procedures One part of these logical procedures is to manage Users. Hence: We need some memory somewhere in our server computer to add Users as they sign up.

4 Database: our “somewhere”
A database has a set of tables Each table consists of a number of columns Databases are NOT necessarily within our project Databases even are NOT necessarily within our server computer So now that we have this database provided, how can we use it?

5 SQL: Structured Query Language
SQL is a special-purpose programming language Its “special purpose” is to make the communication with databases possible. It is a programming language; therefore it has a special syntax Now that we “made communication possible”, let’s get right down to business.

6 RDBMS: Relational Database Management System
An RDBMS is a tool to manage relational databases There are a bunch of RDBMSs out there: Oracle Database Microsoft SQL Server MySQL (Oracle Corporation) We will use MySQL since it is free and available (surprisingly!)

7 Let’s do a little SQL coding to learn:
MySQL Example Let’s do a little SQL coding to learn: Creating users Showing databases Adding tables Making a query

8 Back to our project a JAVA DATABASE CONNECTOR!
We saw how to make databases using SQL codes with the help of MySQL. But how can we set our project (java classes) to manage projects? We still don’t know. Seems like we need something to connect java to our database … Something like… a JAVA DATABASE CONNECTOR!

9 JDBC: Java Database Connector
A JDBC is just a bunch of code that knows what we still don’t know! We import it to our project And now we can make SQL statements and interact with our databases in our java code Let’s use JDBC in action. We are going to make a simple java class that connects to our database.

10 JDBC Problems We have been able to connect to database and make queries. But every time we want to update a table, we have to make same SQL statements. If a bad guy makes a bad move instead of signing up? We have to handle those guys too We are using OOP, while MySQL is based on relational tables. We have to worry about it too What if there was some kind of mapping between our java objects and our relational tables? Something like…

11 Object-Relational Mapping
The purpose of ORM is to interact with database as we write our code. ORM is between server and JDBC.

12 So how do we map each class to a table?
Hibernate Framework Hibernate is an ORM Framework for java. Like JDBC, Hibernate is a set of libraries To use Hibernate, we simply import it to our project classpath So how do we map each class to a table?

13 Configuring Hibernate
The initial configuration of hibernate is set in an xml file The default name for this xml file is hibernate.cfg.xml In hibernate.cfg.xml we specify the RDBMS that we intend to use, the url for the database we want to access, the user from which we want to access database, the password for that user –if required, and a couple of other <property>s

14 A sample hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM " <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">ezpass</property> <!-- List of XML mapping files --> <mapping class="ameghezi.User" /> </session-factory> </hibernate-configuration>

15 <mapping> tag <mapping> tag is exactly where we specify the mapping from the class to the table. This mapping can be managed in two ways.

16 First Way: Resource The first way to specify mapping is to use a resource The code for <mapping> looks like: <mapping resource=“user.hbm.xml" /> A resource is a .xml file (user.hbm.xml in our example) It looks something like:

17 User.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" " <hibernate-mapping> <class name=“User" table=“USER_DETAILS"> <id name=“userName" type=“string" column="id"> <generator class="native"/> </id> <property name=“password" column=“password" type="string"/> </class> </hibernate-mapping>

18 Second Way: Annotation
We use annotation on our entities to let hibernate know they are to be mapped to their corresponding table. <mapping> then looks like: <mapping class=“ameghezi.User" />

19 Using Hibernate to save an entity in our database
An entity is a “stand-alone” object. Our User example is an entity Using Hibernate to store an entity in our database requires: A Session Factory A Session A Transaction And now, Let’s DO IT

20 Thank you for learning 


Download ppt "A very brief introduction"

Similar presentations


Ads by Google