Hibernate: Files_and_Classes

As mentioned in the 'About' section, hibernate handles the communication with the database; by transforming the queries and instructions set by the user, in an object oriented approach, to its equivalent SQL queries

For example,

  • Assume having the table "Lecturer" with columns: ID, FName, LName.

  • The user would like to add a new Lecturer this way:

    Lecturer L1 = new Lecturer(); L1.setFirstName ("Fatma"); L1.setLastName("Meawad"); save(L1);

So, first off all, we need to have a Class representing the entity table Lecturer, that contains

  • Attributes representing the columns of the table | FirstName, LastName
  • A getter and a setter method for each attribute | getFirstName, setFirstName, getLastName, setLastName

After the user prepares the object and asks Hibernate to save it to the database; Hibernate has to connect to the DB and execute this query:

INSERT into Lecturer (FName, LName) VALUES ('Fatma', 'Meawad');

So, how Hibernate will know that the FirstName attribute, in the Lecturer object, corresponds to the FName column, in the Lecturer Table ?

Here comes the role of the "Mapping files". Hibernate uses these mapping files to point out which classes are representing which tables, which attributes are representing which columns , and other aspects that helps in the construction of the generated query.

It seems that every thing is now clear for Hibernate to be able to perform its job; but actually, Hibernate still needs to know how to manage the connections with the Database.

It needs to know the DataSource name, username and password if any, the driver class, information about managing transactions, caching, and connection pooling ... etc; These are called "Hibernate configurations".

Don't panic of all these configurations since most of them have default values; they can be overridden for flexibility.

Hibernate configurations can be set either

  • Programatically or
  • in a .properties file or
  • in a .xml file

For example, you can tell hibernate about your DataSource name, username and password in one of these ways:

  • Programmatically

    Configuration config = new Configuration(); config .setProperty("hibernate.connection.url", "jdbc_URL"); config .setProperty("hibernate.connection.username","DB_Username"); config .setProperty("hibernate.connection.password","DB_Password");

  • in a .properties file containing these commands

    hibernate.connection.url = jdbc_URL hibernate.connection.username = DB_Username hibernate.connection.password = DB_Password

  • in an XML file

    <property name="connection.url">jdbc _URL</property> <property name="connection.url">DB_Username</property> <property name="connection.password">DB_Password</property>

To Recap ...

To build a Hibernate application, we need:

  1. Persistent classes; those are classes representing the Database entity tables.
  2. Mapping files; defining persistent classes and their attributes, and their corresponding database and columns.
  3. Hibernate Configurations settings; that shows hibernate how to obtain and manage the connections with the database.

Read more about Persistent Classes , Mapping files, and Hibernate Configurations from JBoss Community Documentation.

Now its time to start coding... Check the next section to code a simple Hibernate application by following simple steps.