Hibernate: Simple - Example: Vinay@careeraxis - in

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

#include <stdio.

h> int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); } int main() { int i = 15; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; } When the above code is compiled and executed, it produces the following result: Factorial of 15 is 2004310016

[email protected] Login

Home About MET Academics Extras


o o o o o

Activities Online Tutorials Staff Blog Showcase Forum

Agenda

Hibernate: Simple_Example
This section is divided into two parts. The first part is a step-by-step to create a very simple Hibernate application that inserts a new record in the Database. The second part is to understand the code you have written in the application.

In this tutorial we are using Eclipse (Release 3.4.0) as our IDE.

You can download eclipse by choosing "Eclipse IDE for Java EE Developers" from www.eclipse.org/downloads/ or directly from here

Part One | The Example steps:


First of all, we need to create the Database, and the table we are going to insert in. 1. Prepare your Datasource and name it "UniversityDS" 1. Create a new Database. To be consistent with the tutorial, name it "University". 2. Create the table "Lecturer" with columns ID (PK, int, not null) FName (nvarchar(50), null) LName (nvarchar(50), null) you can use this query

Create Table Lecturer ( ID int, FName nvarchar(50), LName nvarchar(50), Primary Key (ID))

3. Make your Database to a Datasource. To be consistent with the tutorial, name it "UniversityDS". Go to Control Panel / Administrative Tools / Dara Sources / Add / SQL Server / Finish. Name: UniversityDS Server: your SQL Server Name / Next. Enter your SQL Server Authentication info / Next. Check 'Change the default database to': Choose the DB 'University' / Next. Finish / Ok / Ok. 2. Create a Java Project and name it "HibernateHelloWorld" If this is your first time using eclipse, follow these steps: 1. Open Eclipse. Go to the folder where you saved eclipse and double-click 'eclipse.exe' 2. Choose your workspace. Browse to the directory that you need to use as a workspace. A workspace is the place where your projects are saved. You may use an already existing folder to function as a workspace, or create a new one by browsing to the directory that you need your workspace to be placed in (C for example), and then writing the

workspace name you like (the folder will be created automatically). 3. Go to the workbench. If the Welcome page is opened, you can reach the workbench by clicking this icon. 4. Create the project. Click file in the menu-bar / New / Project . Select Java / Java Project Name it "HibernateHelloWorld" Click Finish 3. Add Hibernate Jar files. To use Hibernate, Your application needs to use some Jar files that are available here. 1. Unzip the downloaded file. 2. In the Project Explorer view (at the left-side of eclipse. If not there, Click: Window / Show view / Project Explorer) RClick your project / Build Path / Configure Build Path. 3. Add External JARs / Go to the unzipped folder - Select All / Open / Ok. Reminder:

In the Files and Classes section, we said that To build a Hibernate application, we need: 4. Persistent classes. Those are classes representing the Database entity tables. 5. Mapping files. Defining persistent classes and their attributes, and their corresponding database and columns. 6. Hibernate Configurations settings. That shows hibernate how to obtain and manage the connections with the database. Now we are going to create these three files. 4. Create The Persistent Class for the entity 'Lecturer'. 1. Create a class. In the Project Explorer view, R-Click your project / New / Class. 2. Name it 'Lecturer'. 3. Click Finish. 4. Place this code: 5. 6. public class Lecturer { 7. 8. private int ID; 9. private String firstName; 10. private String lastName; 11. 12. public int getID() { 13. return ID; 14. } 15. public void setID(int id) { 16. ID = id; 17. } 18. 19. public String getFirstName() { 20. return firstName; 21. } 22. public void setFirstName(String firstName) { 23. this.firstName = firstName; 24. } 25. 26. public String getLastName() { 27. return lastName; 28. } 29. public void setLastName(String lastName) { 30. this.lastName = lastName; 31. } 32. 33. }

5. Create The Mapping File for the Table 'Lecturer' 1. Create an XML file. In the Project Explorer view, R-Click your project / New / File. 2. Name it 'Lecturer.hbm.xml'. 3. Click Finish. 4. Open it with Text Editor. R-Click 'Lecturer.hbm.xml' / Open with / Text Editor. 5. Place this code: 6. 7. <?xml version="1.0"?> 8. <!DOCTYPE hibernate-mapping PUBLIC 9. "-//Hibernate/Hibernate Mapping DTD//EN" 10. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 11. 12. <hibernate-mapping> 13. 14. <class name="Lecturer" table="Lecturer"> 15. 16. <id name="ID" type="int"> 17. <generator class="increment"/> 18. </id> 19. 20. <property name="firstName" column="FName" type="string"/> 21. <property name="lastName" column="LName" type="string"/> 22. 23. </class> 24. 25. </hibernate-mapping>

6. Create an XML for Hibernate Configurations 'hibernate.cfg.xml'. 1. Create an XML file. 2. Name it 'hibernate.cfg.xml'. 3. Click Finish. 4. Open it with Text Editor. 5. Place this code: 6. 7. <?xml version='1.0' encoding='UTF-8'?> 8. <!DOCTYPE hibernate-configuration PUBLIC 9. "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 10. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 11. 12. <hibernate-configuration> 13. 14. <session-factory> 15. 16. <!-- ________________ To be Edited _________________ --> 17. 18. <property name="connection.url">jdbc_URL</property> 19. <property name="connection.username">jdbc_Username</property> 20. <property name="connection.password">jdbc_Password</property> 21. 22. <!-- _____________ End of To be Edited ______________ --> 23. 24. 25. <property name="connection.driver_class"> 26. sun.jdbc.odbc.JdbcOdbcDriver 27. </property> 28. <property name="dialect"> 29. org.hibernate.dialect.SQLServerDialect 30. </property> 31. <property name="current_session_context_class">thread</property> 32. 33. 34. <!-- _________ Defining the Mapping Files ___________ --> 35. 36. <mapping resource="Lecturer.hbm.xml" /> 37. 38. </session-factory> 39. </hibernate-configuration>

40.Replace jdbc_URL, jdbc_Username, and jdbc_Password, with their appropriate values. The jdbc_URL should look like 'jdbc:odbc:UniversityDS'. The Username, and Password are used for SQL Authentication. They are those you input to connect the SQL server. The Username is most probably 'sa'. 7. Finally Test your Application 1. Create a class. 2. Name it 'SimpleTest'. 3. Click Finish. 4. Place this code: 5. 6. import org.hibernate.Session; 7. import org.hibernate.SessionFactory; 8. import org.hibernate.Transaction; 9. import org.hibernate.cfg.Configuration; 10. 11. public class SimpleTest { 12. 13. public static void main(String[] args) { 14. 15. SessionFactory sessionFactory = new 16. Configuration().configure().buildSessionFactory(); 17. Session session = sessionFactory.getCurrentSession(); 18. Transaction tx = session.beginTransaction(); 19. 20. Lecturer lecturer1 = new Lecturer(); 21. lecturer1.setFirstName("Fatma"); 22. lecturer1.setLastName("Meawad"); 23.

24. 25. 26. 27. 28. 29. 30. 31. 32.

session.save(lecturer1); tx.commit(); System.out.println ("The lecturer " + lecturer1.getFirstName()+ " " + lecturer1.getLastName()+" is successfully added to your database"); } }

33. Run your application by pressing Ctrl + F11 / Java Application 34. Check your Database. Hopefully, you are having a running Hibernate Application now. Proceed with reading to understand the code you have been pasting.

next

LaTeX Flex JSP - Servlets Hibernate Acknowledgments About_Hibernate Files_and_Classes Simple_Example Advanced_Example References
GUC2008 RSS Feeds Credits

Rolimno Ads

Trust Rating

79%
met.guc.edu.eg

Related Searches:

? University Of Engineering And Technology

Faculty Positions College Faculty Engineering And Technology Engineering Education Undergraduate Classes Cairo University Engineering And Technology Education Psychology Program

You might also like