CS 160: Software Engineering November 5 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Distributed Application Development B. Ramamurthy.
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
Session-02.
Session-01. Hibernate Framework ? Why we use Hibernate ?
An Introduction to Hibernate Matt Secoske
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
The Easiest Way To Learn Hibernate: YouTube Videos and the Online Tutorial
Object Persistence using Hibernate
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Agenda What is Hibernate Spring Integration Questions Overview
CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Basics Programming Concepts. Basics A computer program is a set of instructions to tell a computer what to do Machine language = circuit level language.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
CS 160: Software Engineering October 8 Class Meeting
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
CS 157B: Database Management Systems II January 30 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Robert Greiner CSE7330 Southern Methodist University Getting Things Done with Hibernate.
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
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CS 157B: Database Management Systems II February 6 Class Meeting Department of Computer Science San Jose State University Spring 2013 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 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
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.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
Object-Relational Mapping with Hibernate Brian Sam-Bodden Principal Partner Integrallis Software, LLC. August 1 - 5, 2005.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
MCS 270 Spring 2014 Object-Oriented Software Development.
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.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
v110912Java Persistence: EntityManager2 Overview Earlier versions of EJB Specification defined the persistence layer –javax.ejb.EntityBean Java EE 5 moved.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Introduction – ORM, Helloworld Application
CS 174: Web Programming November 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.
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 15 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
CS 160 and CMPE/SE 131 Software Engineering May 12 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Современные технологии баз данных Лекция 7. Объектно-реляционное отображение (ORM) Технология отображения объектов какого- либо объектно-ориентированного.
HIBERNATE/Java Overview of technology for Hibernate by محمد حسن کاظمی پوران Master : M.M.Nematollahi.
Chengyu Sun California State University, Los Angeles
A very brief introduction
Java Data Persistence Using Hibernate
Lecture 3: The J# Language
Hibernate Bayu Priyambadha, S.Kom.
CMPE/SE 131 Software Engineering March 9 Class Meeting
Chengyu Sun California State University, Los Angeles
CMPE/SE 131 Software Engineering March 7 Class Meeting
Presentation transcript:

CS 160: Software Engineering November 5 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 2 Unofficial Field Trip  Computer History Museum in Mt. View  Saturday, November 8, 11:30 – closing time

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 3 Legacy Software  Contains necessary functionality.  May be difficult to use. Often the user interface is the command line.  May be difficult to rewrite. Original programmers long gone. May contain many modifications made over time.  Made by different programmers with varying levels of competence. Written in an archaic programming language that no one knows any more. Nobody dares touch the code lest it break.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 4 Reuse  Lower development effort and cost. Don’t reinvent the wheel.  Lower risk. It’s always worked!  Less training. May be difficult to use, but the concepts are familiar.  Take advantage of past expertise and policies. Overcome the NIH (Not Invented Here) syndrome.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 5 Rewrite  Too difficult or costly to understand the old code.  The old code has become obsolete or dangerous. Example: The Y2K crisis.  New code will be easier to maintain.  Reuse vs. rewrite? Similar to the classic Make vs. Buy decision.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 6 What to Do with Legacy Software  An old command-line program. Keyboard input (standard in) Terminal output (standard out) Still has useful functionality. Nobody wants (or dares) to rewrite it.  Example: An old FORTRAN IV program.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak What to Do with Legacy Software, cont’d 7 C SQUARE ROOT PROGRAM C DOUBLE PRECISION NUMBER, ROOT C C READ NUMBER C READ (5,100) NUMBER 100 FORMAT (F5.0) C C COMPUTE SQUARE ROOT C ROOT = DSQRT(NUMBER) C C WRITE SQUARE ROOT C WRITE (6,200) NUMBER, ROOT 200 FORMAT ('THE SQUARE ROOT OF ', F5.1, ' IS ', F15.10) C STOP END

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 8 What to Do with Legacy Software  One solution: Wrap it inside a modern Java program.  If the Java program is a servlet, you can deploy the legacy app to the web! Use a web form to supply input to the app. Display the app's output on a web page. LegacyDemo Java servlet process Legacy application process standard out standard in

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 9 Shortcomings of JDBC  Java programmers must understand relational databases. The database is very visible. The Java programmer must know SQL.  JDBC is not fully object-oriented. A Java program accesses field values individually from a result set. You need statements to create Java objects from the result set values.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 10 Object-Relational Mapping  Goal: Overcome the paradigm mismatch between relational data from an RDBMS and object-oriented programming.  Object-oriented languages such as Java would much rather deal with classes and objects. Java programmers do not want to write SQL. Java programmers do not want to want to deal with database concepts such as one-to-many, one-to-many, many-to-many, etc. Java programmers do not want to understand normalization.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 11 Object-Relational Mapping  Solution: Add a layer of software between the database and the Java application that does object-relational mapping.  Relational data being read from the database are automatically converted to objects.  Objects being persisted (saved) to the database are automatically converted to relational data.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 12 Object-Relational Mapping  The Java application manipulates objects (create, update, search, delete) and these operations are automatically translated into the corresponding database operations.  The Java programmer can mostly forget that there is an underlying database.  Database platform independence.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 13 Hibernate  A very popular open-source object-relational persistence and query service for Java. Download from  Replace the JDBC API with the Hibernate API.  Java annotations describe in detail the mapping between the Java classes and the relational database tables. _

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 14 Hibernate, cont’d  Import Java packages: Hibernate Hibernate annotations Log4J import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Column; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport;

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 15 hibernate.cfg.xml  Hibernate configuration file Modify as necessary. Must be in your application’s classpath. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" " com.mysql.jdbc.Driver jdbc:mysql://localhost/school_demo root sesame org.hibernate.dialect.MySQL5Dialect true

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 16 Hibernate: public class Student { private long id; private String firstName; private String lastName; public Student() {} public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }... } The annotations provide metadata for the Java compiler and the Java run time.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 17 Hibernate: public class @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; public String getFirstName() { return firstName; } public void setFirstName(String name) { this.firstName = name; public String getLastName() { return lastName; } public void setLastName(String name) { this.lastName = name; }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 18 Student.java  Main method (for testing) public static void main(String args[]) { // Configure Hibernate and add the Student class. Configuration config = new Configuration(); config.addAnnotatedClass(Student.class); config.configure(); // Create the database table. (new SchemaExport(config)).create(true, true); // Create a session. SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession();... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 19 Student.java public static void main(String args[]) {... // Create a session. SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession(); // Load the Student table in a transaction. Transaction tx = session.beginTransaction(); { session.save(new Student("Mary", "Jane")); session.save(new Student("Kim", "Smith")); session.save(new Student("John", "Doe")); session.save(new Student("Tim", "Novak")); session.save(new Student("Leslie", "Klein")); } tx.commit(); session.close(); } SchoolDemo1

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 20 HibernateContext.java  Goal: Move all the Hibernate bookkeeping code into a shared class.

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 21 HibernateContext.java import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null; /** * Set the configuration if it is null. */ private static void setConfiguration() { if (config == null) { config = new Configuration(); config.configure(); } }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 22 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Set the factory if it is null. */ private static void setFactory() { if (factory == null) { setConfiguration(); factory = config.buildSessionFactory(); }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 23 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Open a session from the factory. a session. */ public static Session getSession() { setFactory(); return factory.openSession(); }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 24 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Create a new schema (database) from the configuration. */ public static void createSchema() { setConfiguration(); (new SchemaExport(config)).create(true, true); }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 25 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Add a new class object to the database. klass the class object. */ public static void addClass(Class klass) { setConfiguration(); config.addAnnotatedClass(klass); }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 26 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Add a list of class objects to the database. klasses the list of class objects. */ public static void addClasses(Class klasses[]) { for (Class klass : klasses) { addClass(klass); }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 27 public class Student {... public static Student find(long id) { Session session = HibernateContext.getSession(); Query query = session.createQuery("from Student where id = :idvar"); query.setLong("idvar", id); Student student = (Student) query.uniqueResult(); session.close(); return student; }... } Hibernate Query Language (HQL)

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 28 public class Student {... public static Student find(String lastName) { Session session = HibernateContext.getSession(); Query query = session.createQuery("from Student where lastName = :name"); query.setString("name", lastName); Student student = (Student) query.uniqueResult(); session.close(); return student; }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 29 public class Teacher { private long id; private String firstName; private String lastName; public Teacher () {} public Teacher(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }... } Similar to Student.java

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 30 public class @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; public String getFirstName() { return firstName; } public void setFirstName(String name) { this.firstName = name; public String getLastName() { return lastName; } public void setLastName(String name) { this.lastName = name; }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 31 public class Teacher {... /** * Load the Teacher table. */ public static void load() { Session session = HibernateContext.getSession(); Transaction tx = session.beginTransaction(); { session.save(new Teacher("Tom", "Rogers")); session.save(new Teacher("Art", "Thompson")); session.save(new Teacher("John", "Lane")); session.save(new Teacher("Mabel", "Flynn")); } tx.commit(); session.close(); System.out.println("Teacher table loaded."); }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 32 public class Teacher {... /** * List all the teachers. */ public static void list() { Session session = HibernateContext.getSession(); Query query = session.createQuery("from Teacher"); System.out.println("All teachers:"); for (Teacher teacher : (List ) query.list()) { teacher.print(); } session.close(); } }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 33 public class Teacher {... /** * Print teacher attributes. */ public void print() { System.out.printf("%d: %s %s\n", id, firstName, lastName); }... }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 34 SchoolDemo.java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class SchoolDemo { private static final String HELP_MESSAGE = "*** Commands: create, load, find, students, teachers, quit"; public static void main(String args[]) { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String command; Class klasses[] = {Student.class, Teacher.class}; HibernateContext.addClasses(klasses);... } }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 35 SchoolDemo.java public class SchoolDemo {... public static void main(String args[]) {... do { System.out.print("\nCommand? "); try { command = stdin.readLine(); } catch (java.io.IOException ex) { command = "?"; } String parts[] = command.split(" ");... } while (!command.equalsIgnoreCase("quit")); }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 36 SchoolDemo.java public class SchoolDemo {... public static void main(String args[]) {... do {... if (command.equalsIgnoreCase("create")) { HibernateContext.createSchema(); } else if (command.equalsIgnoreCase("load")) { Student.load(); Teacher.load(); }... } while (!command.equalsIgnoreCase("quit")); } }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 37 SchoolDemo.java public class SchoolDemo {... public static void main(String args[]) {... do {... else if (command.equalsIgnoreCase("teachers")) { Teacher.list(); } else if (command.equalsIgnoreCase("students")) { Student.list(); }... } while (!command.equalsIgnoreCase("quit")); }

Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 38 SchoolDemo.java public class SchoolDemo {... public static void main(String args[]) {... do {... else if (parts[0].equalsIgnoreCase("find") && (parts.length >= 2)) { long id = Long.parseLong(parts[1]); Student student = Student.find(id); if (student != null) { student.print(); } else { System.out.printf("*** No student with id %d\n", id); }... } while (!command.equalsIgnoreCase("quit")); } SchoolDemo2