Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Easiest Way To Learn Hibernate: YouTube Videos and the Online Tutorial

Similar presentations


Presentation on theme: "The Easiest Way To Learn Hibernate: YouTube Videos and the Online Tutorial"— Presentation transcript:

1 The Easiest Way To Learn Hibernate: YouTube Videos and the Online Tutorial http://www.theserverside.com/discussions/thread.tss?thread_id=60191 http://www.allapplabs.com/hibernate/getting_started_with_hibernate.htm http://stackoverflow.com/questions/2880206/java-beginner-wants-to-learn-hibernate Java Persistence with Hibernate (Christian Bauer and Gavin King) http://facestutorials.icefaces.org/tutorial/hibernate-tutorial.html#stepstocreate http://www.allapplabs.com/hibernate/introduction_to_hibernate.htm http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ http://www.javatpoint.com/hibernate-architecture http://www.javatpoint.com/collection-mapping

2 Installing Hibernate and Creating your first class You need to install maven in eclipse > start eclipse >help > Install new software You need to create a maven project You need to add the pom.xml in the project root directory. You need to add the following directories to the project src/main/java, src/main/resources and src/main/webapp THE PERSISTENT CLASS - You need to create a JavaBean and place it in the following path: src/main/java/org/hibernate/tutorial/hibernatetutorial/Event.java You need to create a table in the database called EVENTS with the columns matching with the above class. THE MAPPING FILE - You need to create a mapping file which maps the javabean fields with the columns in the table created above. Since the class file is Event the mapping file is named as Event.hbm.xml. This file is placed in the following location: src/main/resources/org/hibernate/tutorial/hibernatetutorial/Event.hbm.xml THE HIBERNATE CONFIGUATION: You need to create a configuration file for the jdbc connectivity and place it in the following path: src/main/resources /hibernate.cfg.xml NOTE: The mapping resource in your configuration file should point to the mapping file Event.hbm.xml To compile your project: Right click your project> Run As > Maven Build > The edit Configuration screen will show up. In the Goals field type “clean install” > Apply > Run The compiled classes are generated inside the target folder NOTE:The jar files are automatically downloaded when you build your project and are located in the folder Maven dependencies Create HibernateUtil.java helper class at the following location:src/main/java/org/hibernate/tutorial/util/HibernateUtil.java copy log4j.properties from the Hibernate distribution in the etc/ directory to your src directory, next to hibernate.cfg.xml

3 Hibernate Architecture The Hibernate architecture includes many objects persistent object, session factory, session, transaction, transaction factory, connection factory, etc. There are 4 layers in hibernate architecutre: java application layer, hibernate framework layer, backhand api layer and database layer.Let's see the diagram of hibernate architecture:

4 Hibernate Architecture

5 Objects used by Hibernate Framework

6 Elements of Hibernate Architecture For creating the first hibernate application, we must know the elements of Hibernate architecture. They are as follows: SessionFactory The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session. SessionFactory factory = new Configuration().configure().buildSessionFactory(); Session session = factory.openSession(); Session The session object provides an interface between the application and data stored in the database. It is a short-lived object and wraps the JDBC connection. It is factory of Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria. save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances are detected at flush time and also result in an SQL UPDATE. saveOrUpdate() and replicate() result in either an INSERT or an UPDATE. Transaction The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction interface provides methods for transaction management. ConnectionProvider It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource. It is optional. TransactionFactory It is a factory of Transaction. It is optional.

7 Session Object The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states: transient: never persistent, not associated with any Session persistent: associated with a unique Session detached: previously persistent, not associated with any Session Transient instances may be made persistent by calling save(), persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete(). Any instance returned by a get() or load() method is persistent. Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge(). save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances are detected at flush time and also result in an SQL UPDATE. saveOrUpdate() and replicate() result in either an INSERT or an UPDATE. It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory. A Session instance is serializable if its persistent classes are serializable.

8 Transaction A typical transaction should use the following idiom: Session sess = factory.openSession(); Transaction tx; try { tx = sess.beginTransaction(); //do some work... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); }

9 SessionFactory,Session,Transaction Objects You have to startup Hibernate by building a global org.hibernate.SessionFactory object and storing it somewhere for easy access in application code The org.hibernate.SessionFactory is a thread-safe global object that is instantiated once. A org.hibernate.SessionFactory is used to obtain org.hibernate.Session instances A org.hibernate.Session represents a single-threaded unit of work. A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes.

10 HibernateUtil helper class package org.hibernate.tutorial.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } public static SessionFactory getSessionFactory() { return sessionFactory; } }

11 1.1.7. Loading and storing objects We are now ready to start doing some real work with Hibernate. Let's start by writing an EventManager class with a main() method: package org.hibernate.tutorial; import org.hibernate.Session; import java.util.*; import org.hibernate.tutorial.domain.Event; import org.hibernate.tutorial.util.HibernateUtil; public class EventManager { public static void main(String[] args) { EventManager mgr = new EventManager(); if (args[0].equals("store")) { mgr.createAndStoreEvent("My Event", new Date()); } HibernateUtil.getSessionFactory().close(); } private void createAndStoreEvent(String title, Date theDate) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); session.getTransaction().commit(); } }

12 Instance of mapped Entity classes may exist in one of the following three states at a given point in time: transient: A new instance of a a persistent class which is not associated with a Session and has no representation in the database and no identifier value is considered transient by Hibernate. persistent: You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value and is associated with a Session. detached: Once we close the Hibernate Session, the persistent instance will become a detached instance.

13 A typical transaction should use the following idiom: Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // do some work... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } NOTE: If the Session throws an exception, the transaction must be rolled back and the session must be discarded.

14 Hibernate Persistant Class The entire concept of Hibernate is to take the values from Java class attributes and persist them to a database table A mapping document helps Hibernate in determining how to pull the values from the classes and map them with table and associated fields Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate There are following main rules of persistent classes, however, none of these rules are hard requirements. All Java classes that will be persisted need a default constructor. All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database. This property maps to the primary key column of a database table. All attributes that will be persisted should be declared private and have getXXX and setXXX methods defined in the JavaBean style. A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods. All classes that do not extend or implement some specialized classes and interfaces required by the EJB framework.

15 Hibernate Mapping File An Object/relational mappings are usually defined in an XML document. This mapping file instructs Hibernate how to map the defined class or classes to the database tables. There would be one table corresponding to each object you are willing to provide persistence. Suppose you are mapping the class Employee having the instance variables: int id, String firstName, String lastName, int salary with the table Employee having id(primary key), firstName, lastName, salary The mapping document is an XML document having as the root element which contains all the elements. The elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute. The element is optional element and can be used to create the class description. The element maps the unique ID attribute in class to the primary key of the database table. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. The element within the id element is used to automatically generate the primary key values. Set the class attribute of the generator element is set to native to let hibernate pick up either identity, sequence or hilo algorithm to create primary key depending upon the capabilities of the underlying database. The element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.

16 The Hibernate Mapping File or.hbm.xml This class contains the employee detail. NOTE:You should save the mapping document in a file with the format.hbm.xml.

17 The hibernate.cfg.xml or hibernate configuration file <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/testdb root 1 org.hibernate.dialect.MySQLDialect thread org.hibernate.cache.internal.NoCacheProvider --> true update

18 Hibernate Mapping Types When you prepare a Hibernate mapping document, we have seen that you map Java data types into RDBMS data types. The types declared and used in the mapping files are not Java data types; they are not SQL database types either. These types are called Hibernate mapping types, which can translate from Java to SQL data types and vice versa. This chapter lists down all the basic, date and time, large object, and various other builtin mapping types.

19 Hibernate Mapping Types Primitive types:

20 Hibernate Mapping Types Date and time types:

21 Hibernate Mapping Types Binary and large object types:

22 Hibernate Mapping Types JDK-related types:

23 Learnings on 01/02/2014 While constructng the hibernate mapping file ensure that you use the full qualified class name(i.e. package.class) instead of just the class name While writing the hibernate configuration file make sure that you commentou the “cache.provider_class” property as it throws exceptions

24 Create Application Class To Add An Employee(insert) package org.hibernate.tutorials.hibernatetutorial; import java.util.List; import java.util.Date;import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration; public class ManageEmployee { private static SessionFactory factory; public static void main(String[] args) { try{ factory = new Configuration().configure().buildSessionFactory(); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } ManageEmployee ME = new ManageEmployee(); /* Add few employee records in database */ Integer empID1 = ME.addEmployee("Zara", "Ali", 1000); Integer empID2 = ME.addEmployee("Daisy", "Das", 5000); Integer empID3 = ME.addEmployee("John", "Paul", 10000); /* Method to CREATE an employee in the database */ public Integer addEmployee(String fname, String lname, int salary){ Session session = factory.openSession(); T ransaction tx = null; Integer employeeID = null; try{ tx = session.beginTransaction(); Employee employee = new Employee(fname, lname, salary); employeeID = (Integer) session.save(employee); tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } return employeeID; }

25 Create Application Class To Update An Employee(update) package org.hibernate.tutorials.hibernatetutorial; import java.util.List; import java.util.Date;import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration; public class ManageEmployee { private static SessionFactory factory; public static void main(String[] args) { try{ factory = new Configuration().configure().buildSessionFactory(); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } ManageEmployee ME = new ManageEmployee(); /* Update employee's records */ ME.updateEmployee(empID1, 5000); /* Method to UPDATE salary for an employee */ public void updateEmployee(Integer EmployeeID, int salary ){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); Employee employee = (Employee)session.get(Employee.class, EmployeeID); employee.setSalary( salary ); session.update(employee); tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } }

26 Create application to read all employee package org.hibernate.tutorials.hibernatetutorial; import java.util.List; import java.util.Date;import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration; public class ManageEmployee { private static SessionFactory factory; public static void main(String[] args) { try{ factory = new Configuration().configure().buildSessionFactory(); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } ManageEmployee ME = new ManageEmployee(); /* List down all the employees */ ME.listEmployees(); /* Method to READ all the employees */ public void listEmployees( ){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); List employees = session.createQuery("FROM Employee").list(); for (Iterator iterator = employees.iterator(); iterator.hasNext();){ Employee employee = (Employee) iterator.next(); System.out.print("First Name: " + employee.getFirstName()); System.out.print(" Last Name: " + employee.getLastName()); System.out.println(" Salary: " + employee.getSalary()); } tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } }

27 Delete an employee from the database package org.hibernate.tutorials.hibernatetutorial; import java.util.List; import java.util.Date;import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration; public class ManageEmployee { private static SessionFactory factory; public static void main(String[] args) { try{ factory = new Configuration().configure().buildSessionFactory(); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } ManageEmployee ME = new ManageEmployee(); /* Delete an employee from the database */ ME.deleteEmployee(empID2);} /* Method to DELETE an employee from the records */ public void deleteEmployee(Integer EmployeeID){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); Employee employee = (Employee)session.get(Employee.class, EmployeeID); session.delete(employee); tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } }

28 Generator classes in Hibernate The subelement of id used to generate the unique identifier for the objects of persistent class. There are many generator classes defined in the Hibernate Framework. All the generator classes implements the org.hibernate.id.IdentifierGenerator interface. The application programmer may create one's own generator classes by implementing the IdentifierGenerator interface. Hibernate framework provides many built-in generator classes:interface assigned increment sequence hilo native identity seqhilo uuid guid select foreign sequence-identity

29 Generator classes in Hibernate 1) assigned It is the default generator strategy if there is no element. In this case, application assigns the id. For example: 2) increment It generates the unique id only if no other process is inserting data into this table. It generates short, int or long type identifier. The first generated identifier is 1 normally and incremented as 1. Syntax:

30 Generator classes in Hibernate 3) sequence It uses the sequence of the database. if there is no sequence defined, it creates a sequence automatically e.g. in case of Oracle database, it creates a sequence named HIBERNATE_SEQUENCE. In case of Oracle, DB2, SAP DB, Postgre SQL or McKoi, it uses sequence but it uses generator in interbase. Syntax: For defining your own sequence, use the param subelement of generator. your_sequence_name 4) hilo It uses high and low algorithm to generate the id of type short, int and long. Syntax: 5) native It uses identity, sequence or hilo depending on the database vendor. Syntax:

31 Generator classes in Hibernate 6) identity It is used in Sybase, My SQL, MS SQL Server, DB2 and HypersonicSQL to support the id column. The returned id is of type short, int or long. 7) seqhilo It uses high and low algorithm on the specified sequence name. The returned id is of type short, int or long. 8) uuid It uses 128-bit UUID algorithm to generate the id. The returned id is of type String, unique within a network (because IP is used). The UUID is represented in hexadecimal digits, 32 in length. 9) guid It uses GUID generated by database of type string. It works on MS SQL Server and MySQL. 10) select It uses the primary key returned by the database trigger. 11) foreign It uses the id of another associated object, mostly used with association. 12) sequence-identity It uses a special sequence generation strategy. It is supported in Oracle 10g drivers only. download the example of assigned generatordownload the example of assigned generator

32 SQL Dialects in Hibernate For connecting any hibernate application with the database, you must specify the SQL dialects. There are many Dialects classes defined for RDBMS in the org.hibernate.dialect package. They are as follows

33 Hibernate Table Per Hierarchy using xml file By this inheritance strategy, we can map the whole hierarchy by single table only. Here, an extra column (also known as discriminator column) is created in the table to identify the class. RDBMSDialect Oracle (any version)org.hibernate.dialect.OracleDialect Oracle9iorg.hibernate.dialect.Oracle9iDialect Oracle10gorg.hibernate.dialect.Oracle10gDialect MySQLorg.hibernate.dialect.MySQLDialect MySQL with InnoDBorg.hibernate.dialect.MySQLInnoDBDialect MySQL with MyISAMorg.hibernate.dialect.MySQLMyISAMDialect DB2org.hibernate.dialect.DB2Dialect DB2 AS/400org.hibernate.dialect.DB2400Dialect DB2 OS390org.hibernate.dialect.DB2390Dialect Microsoft SQL Serverorg.hibernate.dialect.SQLServerDialect Sybaseorg.hibernate.dialect.SybaseDialect Sybase Anywhereorg.hibernate.dialect.SybaseAnywhereDialect PostgreSQLorg.hibernate.dialect.PostgreSQLDialect SAP DBorg.hibernate.dialect.SAPDBDialect Informixorg.hibernate.dialect.InformixDialect HypersonicSQLorg.hibernate.dialect.HSQLDialect Ingresorg.hibernate.dialect.IngresDialect Progressorg.hibernate.dialect.ProgressDialect Mckoi SQLorg.hibernate.dialect.MckoiDialect Interbaseorg.hibernate.dialect.InterbaseDialect Pointbaseorg.hibernate.dialect.PointbaseDialect FrontBaseorg.hibernate.dialect.FrontbaseDialect Firebirdorg.hibernate.dialect.FirebirdDialect

34 Hibernate O/R Mapping

35 Code Snipped for Hibernate SQL insert public class StoreData { public static void main(String[] args) { //creating configuration object Configuration cfg=new Configuration(); cfg.configure("hibernate.cfg.xml"); //populates the data of the configuration file //creating seession factory object SessionFactory factory=cfg.buildSessionFactory(); //creating session object Session session=factory.openSession(); //creating transaction object Transaction t=session.beginTransaction(); Employee e1=new Employee(); e1.setId(115); e1.setFirstName("sonoo"); e1.setLastName("jaiswal"); session.persist(e1); //persisting the object t.commit();//transaction is commited session.close(); System.out.println("successfully saved"); } }

36 MISC Also note that everything in src/main/resources will be "on the classpath", i.e., it all is copied to my-webapp/WEB- INF/classes when the war is built. So that's a good place to put your configuration files, for example, log4j.xml / logback.xml,

37 How to configure your hibernate maven project to run on server – to be done when building a web project Simply go into the project properties, make the projet faceted.. tick Dynamic Web Module and you'll get run on server as an option after that. This will enable the "Run on Server" option, however your problems don't finish there as when you then run it, you'll get a 404. To fix this, go back into project properties, Deployment assembly. Delete the WebContent entry (and you can delete the folder in the project later too), and make sure you have the src-main-webapp & src-main-resource folders added. Bingo.. run the app and it should be fine.


Download ppt "The Easiest Way To Learn Hibernate: YouTube Videos and the Online Tutorial"

Similar presentations


Ads by Google