CS 157B: Database Management Systems II February 6 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.

Slides:



Advertisements
Similar presentations
Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Advertisements

CS 185C/286: The History of Computing October 31 Class Meeting Department of Computer Science San Jose State University Fall 2011 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering August 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Session-01. Hibernate Framework ? Why we use Hibernate ?
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
ASP.NET Programming with C# and SQL Server First Edition
CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
CS 157B: Database Management Systems II May 8 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
CS 160: Software Engineering October 8 Class Meeting
CS 157B: Database Management Systems II January 30 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 157B: Database Management Systems II April 29 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools.
CS 174: Web Programming September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 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.
CS 157B: Database Management Systems II January 28 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering October 1 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering October 15 Class Meeting
CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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.
CS 1308 Computer Literacy and the Internet
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design September 5 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
CS 157B: Database Management Systems II April 3 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
DT228/3 Web Development Databases. Querying a database: Partial info Search engines, on-line catalogues often need to allow user to search a database.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
CS 157B: Database Management Systems II February 11 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
CS 157B: Database Management Systems II February 20 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
CS 157B: Database Management Systems II February 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 160: Software Engineering December 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 157B: Database Management Systems II April 22 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Interacting with LexEVS 5.0 LexEVS in a Distributed Environment November 2009.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
CS 160 and CMPE/SE 131 Software Engineering March 10 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
1 Management Information Systems M Agung Ali Fikri, SE. MM.
CS 160 and CMPE/SE 131 Software Engineering March 15 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Rationale Databases are an integral part of an organization. Aspiring Database Developers should be able to efficiently design and implement databases.
CS 157B: Database Management Systems II January 23 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Chengyu Sun California State University, Los Angeles
CS 153: Concepts of Compiler Design September 14 Class Meeting
ATS Application Programming: Java Programming
Reflection API, JDBC, Hibernate, RMI
A very brief introduction
CS 174: Server-Side Web Programming February 12 Class Meeting
CMPE/SE 131 Software Engineering March 9 Class Meeting
CMPE 152: Compiler Design February 21/26 Lab
CS 144 Advanced C++ Programming February 21 Class Meeting
CMPE 152: Compiler Design March 19 Class Meeting
CMPE/SE 131 Software Engineering March 7 Class Meeting
CS 151: Object-Oriented Design October 8 Class Meeting
Presentation transcript:

CS 157B: Database Management Systems II February 6 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 2 Hibernate Oral Presentations  Next Monday, February 11.  Four teams volunteer to each give a quick 15-minute presentation and demo of its project. What data did you use. Logical design of your database. How you used Hibernate. Demo of your application. Q & A

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 3 Hibernate Oral Presentations  Monday, February 11  Section 1 Team Cosmos Team VoidStarStar  Section 2 Team Xeon

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 4 Internet Connection Required  Recall the configuration file hibernate.cfg.xml :  The URL is for the Document Type Definition (DTD). Hibernate can’t parse the configuration file without it. Therefore, you need a working Internet connection to execute a Hibernate program! <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 5 No Internet Connection Required  Solution: Download that DTD and put it somewhere local, such as in your source folder. Modify your hibernate.cfg.xml accordingly: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "file:///G:/NetBeansProjects/SchoolDemo5/src/hibernate-configuration-3.0.dtd">...

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 6 Queries Without SQL ... or anything that resembles SQL.  Hibernate offers an object-oriented way to formulate and execute database queries.  Criteria API Build nested, structured query expressions in Java. Compile-time syntax checking.  Unlike embedded SQL. Like Hibernate itself, it isn’t perfect.

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 7 Get a List of Objects of a Class  Sort them: Session session = HibernateContext.getSession(); Criteria criteria = session.createCriteria(Student.class); List students = (List ) criteria.list(); criteria.addOrder(Order.asc("lastName")); List students = criteria.list();  Paginate: Get only the first 5 objects. criteria.setFirstResult(0); criteria.setMaxResults(5); List students = (List ) criteria.list(); Also: Order.desc()

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 8 Query by Example  Query by Example (QBE) is a simple query technique. Create a prototype object. Set one or more of the prototype object’s field values. Use the prototype object to make an example object. Use the example object in a query to fetch all the objects whose field values match those of the example. Student prototype = new Student(); prototype.setLastName("Smith"); Example example = Example.create(prototype); Session session = HibernateContext.getSession(); Criteria criteria = session.createCriteria(Student.class); criteria.add(example); Student student = (Student) criteria.uniqueResult(); QBE doesn’t seem to work with the primary key field.

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 9 Query by Example  Ignore zero-valued fields when matching: Example example = Example.create(... ); example.excludeZeroes();  Don’t ignore null fields. The default is to ignore null fields when matching. Also enables matching on zero-valued fields. Example example = Example.create(... ); example.excludeNone();  Ignore a particular field: Example example = Example.create(... ); example.excludeProperty("firstName");

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 10 Query by Example  Fuzzy matches: Get all students whose addresses end in.com  MatchMode ANYWHERE START END EXACT Student prototype = new Student(); prototype.set Address(".com"); Example example = Example.create(prototype); example.enableLike(MatchMode.END); Session session = HibernateContext.getSession(); Criteria criteria = session.createCriteria(Student.class); criteria.add(example); List students = (Student) criteria.list();

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 11 Restrictions  Query by example has some obvious limitations. What about the query “fetch all students with GPA > 3.0”?  Use the Criteria API’s Restrictions class. This class has many static methods. See: Session session = HibernateContext.getSession(); Criteria criteria = session.createCriteria(Student.class); criteria.add(Restrictions.gt("gpa", 3.0f)); List smarties = (List ) criteria.list();

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 12 Restrictions  You can “and” together multiple restrictions: Criterion crit1 = Restrictions.gt("gpa", 3.0f); Criterion crit2 = Restrictions.le("gpa", 3.5f); Criterion crit3 = Restrictions.isNotNull(" Address"); Student prototype = new Student(); prototype.set Address(".com"); Example example = Example.create(prototype); example.enableLike(MatchMode.END); example.ignoreCase(); Session session = HibernateContext.getSession(); Criteria criteria = session.createCriteria(Student.class); criteria.add(crit1); criteria.add(crit3); criteria.add(example); List students = (List ) criteria.list();

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 13 Restrictions  You can also “or” together multiple restrictions. Fetch all students whose GPA is less that 3.0 or greater than 3.5 Criterion crit1 = Restrictions.lt("gpa", 3.0f); Criterion crit2 = Restrictions.gt("gpa", 3.5f); Disjunction disjunction = Restrictions.disjunction(); disjunction.add(crit1); disjunction.add(crit2); Session session = HibernateContext.getSession(); Criteria criteria = session.createCriteria(Student.class); criteria.add(disjunction); List students = (List ) criteria.list();

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 14 Criteria and Associations  What classes are taught by John Lane? Sorted by subject.  “Chain” criteria across associated objects. Use the reference fields. IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel CodeTeacher_idSubject Data structures Java programming Compilers Software engineering Operating systems TeacherClass

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak public class Klass {... private public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; }... }  What classes are taught by John Lane?

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 16 Criteria and Associations IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel CodeTeacher_idSubject Data structures Java programming Compilers Software engineering Operating systems TeacherClass Session session = HibernateContext.getSession(); Criteria classCriteria = session.createCriteria(Klass.class); Criteria teacherCriteria = classCriteria.createCriteria("teacher"); teacherCriteria.add(Restrictions.eq("firstName", "John")).add(Restrictions.eq("lastName", "Lane")); classCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); classCriteria.addOrder(Order.asc("subject")); List klasses = (List ) classCriteria.list();

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 17 public class Student {... private List klasses = new public List getKlasses() { return klasses; } public void setKlasses(List klasses) { this.klasses = klasses; }... }  Who are the students taught by John Lane?

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 18 Criteria and Associations  Who are the students taught by John Lane? Session session = HibernateContext.getSession(); Criteria studentCriteria = session.createCriteria(Student.class); Criteria classCriteria = studentCriteria.createCriteria("klasses"); Criteria teacherCriteria = classCriteria.createCriteria("teacher"); teacherCriteria.add(Restrictions.eq("firstName", "John")).add(Restrictions.eq("lastName", "Lane")); studentCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); studentCriteria.addOrder(Order.asc("lastName")); List students = (List ) studentCriteria.list(); SchoolDemo6 Why is this line necessary?

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 19 Project #1  Now you should be able to use Hibernate to: Map Java classes to relational database tables. Specify one-to-one, one-to-many, and many-to- many associations, and handle class hierarchies. Create and populate your database tables.  Use sample datasets downloaded from the Internet, or make up your own data. Do queries using the Criteria API. _

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 20 Hibernate Query Language (HQL)  Looks like SQL.  However, queries are based on Java classes and their fields, not database tables. Insulate your Java code from the database.  Value injection with named parameters.  Example: public static Teacher find(String lastName) { Session session = HibernateContext.getSession(); Query query = session.createQuery("from Teacher where lastName = :name"); query.setString("name", lastName); Teacher teacher = (Teacher) query.uniqueResult(); session.close(); return teacher; }

Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 21 Native SQL  As a last resort, Hibernate allows native SQL.  Example: The result will be an array of field values.  Similar to a JDBC result set. Session session = HibernateContext.getSession(); SQLQuery query = session.createSQLQuery( "SELECT * FROM Teacher WHERE last_name = '" + lastName + "'"); Object result = query.uniqueResult();