Step 1. Grab the tools needed to run the example in this tutorial [the version used for this tutorial is put between brackets].
- Hibernate [4.2.5]: http://sourceforge.net/projects/hibernate/files/hibernate4/4.2.5.Final/
- Eclipse[3] [4.3 Kepler]: http://www.eclipse.org/downloads/
- PostgreSQL[4][5] [9.1.9]: http://www.postgresql.org/download/
- PostgreSQL JDBC driver[6] [9.2-1003 JDBC4 driver]: http://jdbc.postgresql.org/download/postgresql-9.2-1003.jdbc4.jar
Step 2. Create a new Java project.
Step 3. Create a new folder directly under your project root folder and name it lib (right-click on the project's root folder and New -> Folder), then make a copy of the JAR files under hibernate-release-4.2.5.Final/lib/required and the JDBC driver JAR file (postgresql-9.2-1003.jdbc4.jar) to lib.
Step 4. Add the content of lib to your project's classpath[9]. Right-click on the project's root folder and Build Path -> Configure Build Path... -> Add JARs..., then select all the JAR files in lib.
Step 5. Create a new Java class Person.java in org.peaceandcode.domain. This class represents the object that will be stored in the database. It would be best if the class is a Plain Old Java Object (POJO)[10] such that each property represents a column in the table mapped to this class.
Step 6. Create a new XML file in org.peaceandcode.domain and name it Person.hbm.xml. This will be the mapping file between a Person object and a row in the PERSON table from the database.
Step 7. Create an XML file in src and name it hibernate.cfg.xml. This is the hibernate configuration file which could be, in our example, divided into three main parts:
- Database connection settings.
- Hibernate settings.
- Mapping resources.
Step 8. Create a new Java class PersonAccess.java in org.peaceandcode. In this class, we will define our CRUD[11] operations.
Step 9. Create another Java class HelloHibernate.java in org.peaceandcode. Here, we will sample the CRUD operations.
Step 10. Run org.peaceandcode.HelloHibernate.java as a Java Application and check the output. The listed output is obtained by setting show_sql to false.
---------------- Add few people ---------------- List people Person(1): Taqi al-Din al-Maqrizi - 78 Person(2): Kurt Gödel - 71 Person(3): Saif ad-Din Qutuz - 63 Person(4): Beatrix Potter - 77 Person(5): Yamaoka Kotaro - 56 Person(6): John Ronald Reuel Tolkien - 81 Person(7): Baruch Spinoza - 44 Person(8): Nemo Lambda - 82 ---------------- Get people with odd ID Person(1): Taqi al-Din al-Maqrizi - 78 Person(3): Saif ad-Din Qutuz - 63 Person(5): Yamaoka Kotaro - 56 Person(7): Baruch Spinoza - 44 ---------------- Set age of people with even ID to 30 ---------------- List people after UPDATE Person(1): Taqi al-Din al-Maqrizi - 78 Person(3): Saif ad-Din Qutuz - 63 Person(5): Yamaoka Kotaro - 56 Person(7): Baruch Spinoza - 44 Person(2): Kurt Gödel - 30 Person(4): Beatrix Potter - 30 Person(6): John Ronald Reuel Tolkien - 30 Person(8): Nemo Lambda - 30 ---------------- Delete 8th person ---------------- List people after DELETE Person(1): Taqi al-Din al-Maqrizi - 78 Person(3): Saif ad-Din Qutuz - 63 Person(5): Yamaoka Kotaro - 56 Person(7): Baruch Spinoza - 44 Person(2): Kurt Gödel - 30 Person(4): Beatrix Potter - 30 Person(6): John Ronald Reuel Tolkien - 30
Happy O/R mapping[12]!
---
External links:
[1] http://en.wikipedia.org/wiki/Hibernate_%28Java%29
[2] http://en.wikipedia.org/wiki/KISS_principle
[3] http://en.wikipedia.org/wiki/Eclipse_%28software%29
[4] http://en.wikipedia.org/wiki/PostgreSQL
[5] http://www.wikivs.com/wiki/MySQL_vs_PostgreSQL
[6] http://en.wikipedia.org/wiki/JDBC
[7] http://en.wikipedia.org/wiki/Relational_database_management_system
[8] http://en.wikipedia.org/wiki/Data_Definition_Language
[9] http://en.wikipedia.org/wiki/Classpath_%28Java%29
[10] http://en.wikipedia.org/wiki/Plain_Old_Java_Object
[11] http://en.wikipedia.org/wiki/CRUD
[12] http://en.wikipedia.org/wiki/Object-relational_mapping
No comments:
Post a Comment