Robert Greiner CSE7330 Southern Methodist University Getting Things Done with Hibernate.

Slides:



Advertisements
Similar presentations
Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Advertisements

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
ODBC and JDBC O/R Mapping David Rabinowitz. June 2nd, 2004 Object Oriented Design Course 2 Objects And Databases Most of the applications today are written.
ODBC and JDBC O/R Mapping Amit Shabtay. June 2nd, 2004 Object Oriented Design Course 2 Objects And Databases Most of the applications today are written.
Chapter 22 Sets and Maps. Chapter Scope The Java API set and map collections Using sets and maps to solve problems How the Java API implements sets and.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Session-02.
An Introduction to Hibernate Matt Secoske
Enterprise Applications Development & Integration Andrew Stratton Hibernate Case Study.
The Easiest Way To Learn Hibernate: YouTube Videos and the Online Tutorial
Object Persistence using Hibernate
Fresher Technology Specific - JAVA Stream Hibernate Framework
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSC3170 Introduction to Database Systems
M1G Introduction to Programming 2 4. Enhancing a class:Room.
Agenda What is Hibernate Spring Integration Questions Overview
EJB Entity Beans “Modeling your data”.
CS 157B: Database Management Systems II January 30 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Introduction to Object-Oriented Programming
Dr. Magdi AMER Unit 2 Introduction to Database. Intro Many programs need to save information on disk. The role of DB system is to provide a layer of abstraction.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
CS 157B: Database Management Systems II February 6 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 160: Software Engineering November 5 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 157B: Database Management Systems II February 4 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
1 21 COP 3540 Data Structures with OOP Overview: Chapter 1.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CS 157B: Database Management Systems II January 28 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Chapter 13 Sets and Maps Modified. Chapter Scope The Java API set and map collections Using sets and maps to solve problems How the Java API implements.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
A U.S. Department of Energy Office of Science Laboratory Operated by The University of Chicago Argonne National Laboratory Office of Science U.S. Department.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Hibernate Thuy, Le Huu Pentalog HAN. What is Hibernate? Hibernate is a free, open source Java package that makes it easy to work with relational databases.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
ODS – Introduction to Web Services and BPEL Vakgroep Informatietechnologie Web Services & BPEL Design of Distributed Software.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
Database Management Systems.  Instructor: Yrd. Doç. Dr. Cengiz Örencik   Course material.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
MANY-TO-MANY MAPPING. May 12, 2011  Implemented using a Set java collection  Mediator table is mandatory – Join table  Join table contains foreign.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Современные технологии баз данных Лекция 7. Объектно-реляционное отображение (ORM) Технология отображения объектов какого- либо объектно-ориентированного.
HIBERNATE/Java Overview of technology for Hibernate by محمد حسن کاظمی پوران Master : M.M.Nematollahi.
Database Management Systems
A very brief introduction
Server Concepts Dr. Charles W. Kann.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Java Data Persistence Using Hibernate
Lecture 3: The J# Language
CSC 113 Tutorial QUIZ I.
Hibernate Bayu Priyambadha, S.Kom.
slides created by Ethan Apter
Making Text for the Web part 6
Presentation transcript:

Robert Greiner CSE7330 Southern Methodist University Getting Things Done with Hibernate

Introduction – A quick Quiz.  How many of you are proficient in:

Quiz Results: How did you do?  Did you answer “Yes” to all of the databases?  If not, this presentation is for You!

Hibernate’s Most Important Features  Object to Relational Mapping Tool.  Relational databases do not map well to the real world  Takes Java Objects and maps them to relational tables Not easy!  Works with any relational database  Handles Database Transactions Automatically  Possible through mapping files  Programmers don’t need to worry about writing SQL FACT: Programmers are not typically proficient in SQL but still need to interact with databases on a regular basis.  Another tool to help you get things done.  Because that’s what gets you hired and keeps you employed

What Hibernate Isn’t?  The answer to all of your database problems.  No amount of awesome code will substitute for good design.  At the end of the day, data is stored in a relational model

Example  Suppose you have a system that needs to keep track of students and courses (easy)  Students Table  Courses Table  How do we express the m:n relationship for students that need to enroll in several courses? (not as easy)  Enrolled Table (This does not map well to an object)  Can Hibernate help with this?  Yes!

Let’s see some code

The Anatomy of a Hibernate Project  Java Classes  Student.java  Course.java  Hibernate Mapping File  Student.hbm.xml  Course.hbm.xml  Hibernate Files  hibernate.cfg.xml  HibernateUtil.java  StudentManager.java main() Regular Java Objects

Student.java public class Student { private long sid; private String firstName; private String lastName; private double gpa; Student() {} //Default constructor public Student(String firstName, String lastName, double gpa) { this.firstName = firstName; this.lastName = lastName; this.gpa = gpa; } public void setSid(long sid) { this.sid = sid; } public long getSid() { return this.sid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; }... } Just a regular everyday Java class Need a getter/setter for each database value

Course.java public class Course { long cid; String name; Set students = new HashSet(); public Course() {} public Course(String name) { this.name = name; }... public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; } public void addStudent(Student student) { this.students.add(student); } Each course object holds a collection of the Students enrolled in it.

Student.hbm.xml Teach Hibernate how to map objects to the database <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "

Course.hbm.xml <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "

Hibernate.cfg.xml <!DOCTYPE hibernate-configuration SYSTEM " org.hsqldb.jdbcDriver jdbc:hsqldb:hsql://localhost sa org.hibernate.dialect.HSQLDialect false true

HibernateUtil.java – The Setup public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static Session session = HibernateUtil.getSessionFactory().openSession(); private static SessionFactory buildSessionFactory() { try { return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { System.out.println("Exception!!!!"); throw new ExceptionInInitializerError(ex); } public static SessionFactory getSessionFactory() { return sessionFactory; }...

HibernateUtil.java – Insert Students/Courses public static void insertStudents(List students) { //Inserts a list of students into the database Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); for (Student s : students) { Long sid = (Long) session.save(s); } tx.commit(); session.close(); } public static void insertCourses(List courses) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); for (Course c : courses) { Long sid = (Long) session.save(c); } tx.commit(); session.close(); }

HibernateUtil.java – Read Students/Courses private static List getStudents() { List students = new ArrayList (); students = session.createCriteria(Student.class).list(); return students; } private static List getCourses() { List courses = new ArrayList (); courses = session.createCriteria(Course.class).list(); return courses; }

HibernateUtil.java – Print Collections public static void printDatabaseState() { List students = getStudents(); List courses = getCourses(); Set studentsInCourse = new HashSet(); for (Student s : students) { System.out.println(s.getSid() + " - " + s.getFirstName() + " " + s.getLastName()); } for (Course c : courses) { studentsInCourse = c.getStudents(); System.out.println(c.getName()); for (Object s : studentsInCourse) { System.out.println(" " + ((Student)s).getFirstName() + " " + ((Student)s).getLastName()); }

StudentManager.java public class StudentManager { List students = new ArrayList (); List courses = new ArrayList (); public static void main(String[] args) { //Initialize and call each method } public void addStudents() { this.students.add(new Student("Albert", "Einstein", 4.0)); this.students.add(new Student("Carl", "Sagan", 2.5)); this.students.add(new Student("Alan", "Turing", 4.0)); this.students.add(new Student("Ken", "Thompson", 4.0)); this.students.add(new Student("Bill", "Gates", 0.0)); this.students.add(new Student("Steve", "Jobs", 3.5)); HibernateUtil.insertStudents(this.students); } public void addCourses() { this.courses.add(new Course("CSE7330")); this.courses.add(new Course("CSE5330")); this.courses.add(new Course("CSE7314")); this.courses.add(new Course("CSE8313")); this.courses.get(0).addStudent(this.students.get(0)); this.courses.get(0).addStudent(this.students.get(1)); this.courses.get(0).addStudent(this.students.get(2)); this.courses.get(1).addStudent(this.students.get(3)); this.courses.get(1).addStudent(this.students.get(5)); this.courses.get(2).addStudent(this.students.get(0)); this.courses.get(2).addStudent(this.students.get(1)); this.courses.get(3).addStudent(this.students.get(2)); this.courses.get(3).addStudent(this.students.get(3)); this.courses.get(3).addStudent(this.students.get(5)); HibernateUtil.insertCourses(this.courses); }

Results Program Output 1 - Albert Einstein 2 - Carl Sagan 3 - Alan Turing 4 - Ken Thompson 5 - Bill Gates 6 - Steve Jobs CSE7330 Alan Turing Albert Einstein Carl Sagan CSE5330 Steve Jobs Ken Thompson CSE7314 Albert Einstein Carl Sagan CSE8313 Alan Turing Steve Jobs Ken Thompson

Summary  It can be difficult to express real life objects as relations  Hibernate is an Object to Relational mapping tool  Example: No 3 rd Enrolled object needed Allows for a more Object Oriented approach  Hibernate allows you to interact with a database without having to know platform-specific SQL  Did you see one line of SQL in this presentation?

Future Applications  Web frameworks are moving towards ORM.  Rails (Ruby)  Django (Python)  Hibernate for other languages  nHibernate (.NET)

The 5 W’s of Hibernate  Who: Hibernate is maintained by Red Hat  What: Object to Relational Mapping Tool  When: Your next project that uses a database  Where: Project Homepage:  Why: Get things done!

References  Harnessing Hibernate  Dr. James Elliot  O'Reilly Media (April 1, 2008)  Amazon Link Amazon Link  Java Persistence with Hibernate  Christian Bauer  Manning Publications; Revised edition (November 24, 2006)  Amazon Link Amazon Link   Official Hibernate Project Site  Red Hat  11/11/2009   Hibernate Tutorials  Bushan Dongare  Good intro tutorial to hibernate  11/11/2009   Stack Overflow Tag for Hibernate Questions  Great community resource for getting your questions answered  Community Authored and Licensed 