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

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

NMED 3850 A Advanced Online Design February 25, 2010 V. Mahadevan.
CS 185C/286: The History of Computing October 31 Class Meeting Department of Computer Science San Jose State University Fall 2011 Instructor: Ron Mak
SPRING 2004CENG 3521 The Relational Model Chapter 3.
CS 160: Software Engineering August 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
ACCESS CHAPTER 1. OBJECTIVES Tables Queries Forms Reports Primary and Foreign Keys Relationship.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
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
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.
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.
Concepts and Terminology Introduction to Database.
CS 157B: Database Management Systems II April 29 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
RELATIONSHIPS Generally there are two main database types: flat-file and relational.
Robert Greiner CSE7330 Southern Methodist University Getting Things Done with Hibernate.
CMPE 226 Database Systems September 16 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
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 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 January 28 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CSCI 3140 Module 3 – Logical Database Design for the Relational Model Theodore Chiasson Dalhousie University.
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
OK lets start, who am I ?. Nayden Gochev (a.k.a. JOKe) Java Spring Android Hybris GWT EJB JSF RMI JAX-RS Struts JMS JPA Hibernate C# ASP.NET TestStudio.
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.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
Hibernate Introduction - 1 Present by Eric Yu. BeanSoft | 2 Content Understanding object/relational persistence Introduction Architecture The core interfaces.
The Digital Archive Database Tool Shih Lin Computing Center Academia Sinica.
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 2 BACKNEXTEND 2-1 LINKS TO OBJECTIVES Creating Related Tables Creating Related Tables Determining.
CS 151: Object-Oriented Design September 5 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
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.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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
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.
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
v110912Java Persistence: EntityManager2 Overview Earlier versions of EJB Specification defined the persistence layer –javax.ejb.EntityBean Java EE 5 moved.
Java Persistence API (JPA) Relationships. Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics)
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
CS 174: Web Programming November 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Concepts of Database Management Seventh Edition Chapter 4 Keys and Relationship.
CS 157B: Database Management Systems II April 22 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Working with MySQL A290/A590, Fall /07/2014.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
Hibernates - Many to One Association. May 12, 2011 What is Association? Association is the relation between two objects. One class holds a reference of.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
CS 160 and CMPE/SE 131 Software Engineering March 10 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 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 157B: Database Management Systems II January 23 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS6320 – Java Persistence API
Chengyu Sun California State University, Los Angeles
Advanced Java Programming
Hibernate (JPA) Code First Entity Relations
SQL DATA CONSTRAINTS.
CMPE/SE 131 Software Engineering March 9 Class Meeting
SQL NOT NULL Constraint
CS 151: Object-Oriented Design October 8 Class Meeting
Presentation transcript:

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

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 2 Referential Integrity  Referential integrity is database-speak for “No dangling pointers.” In other words, a foreign key value should always be a valid reference to a record in the target table (or the key value should be null). _

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 3 Maintaining Referential Integrity  Recall our one-to-one table association:  The Teacher table has a foreign-key constraint. Field contact_id should always reference a valid record in the Contact_Info table, or the field should be null. _ IdLastFirstContact_id 7003RogersTom ThompsonArt LaneJohn FlynnMabel856 Id _address TeacherContact_Info

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 4 Maintaining Referential Integrity IdLastFirstContact_id 7003RogersTom ThompsonArt LaneJohn FlynnMabel856 Id _address CREATE TABLE `teacher` ( `Id` int(10) unsigned NOT NULL auto_increment, `Last` varchar(45) NOT NULL, `First` varchar(45) NOT NULL, `contact_id` int(10) unsigned NOT NULL, PRIMARY KEY (`Id`), KEY `FK_teacher_1` (`contact_id`), FOREIGN KEY (`contact_id`) REFERENCES `contact_info` (`id`) ) MySQL TeacherContact_Info

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 5 Maintaining Referential Integrity  You cannot insert a new Teacher record whose contact_id value does not match a Contact_Info.id value.  You cannot change a Teacher.contact_id value to a value that does not match a Contact_Info.id value.  You cannot change a Contact_Info.id value to a value that does not match a Teacher.contact_id value.  You cannot delete a Contact_Info record without updating the Teacher table. IdLastFirstContact_id 7003RogersTom ThompsonArt LaneJohn FlynnMabel856 Id _address TeacherContact_Info Demo

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 6 Maintaining Referential Integrity  Update a Contact_Info.id value  the corresponding Teacher.contact_info value is updated automatically.  Delete a Contact_Info record  the corresponding Teacher record is deleted. What happens to Contact_Info if you delete a Teacher record? CREATE TABLE `teacher` ( `Id` int(10) unsigned NOT NULL auto_increment, `Last` varchar(45) NOT NULL, `First` varchar(45) NOT NULL, `contact_id` int(10) unsigned NOT NULL, PRIMARY KEY (`Id`), KEY `FK_teacher_1` (`contact_id`), FOREIGN KEY (`contact_id`) REFERENCES `contact_info` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) cascade policy Also: ON DELETE SET NULL Demo

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 7 Hibernate Cascade public class Student {... private ContactInfo public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo info) { this.contactInfo = info; }... }

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 8 Hibernate Cascade Types  CascadeType.PERSIST Tell a session to save() a Teacher object, and the corresponding ContactInfo object is saved automatically.  CascadeType.REMOVE Tell a session to delete() a ContactInfo object, and the corresponding Teacher object is deleted automatically.  CascadeType.REFRESH Tell a session to refresh() a Teacher object, and the corresponding ContactInfo object is refreshed automatically.  CascadeType.MERGE Tell a session to merge() a Teacher object, and the corresponding ContactInfo object is refreshed automatically.  CascadeType.ALL Do all of the above. _

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 9 Hibernate Reminders  Hibernate accesses the underlying database within the context of a session.  Two common errors: Attempting to access a Java object whose corresponding database record has not yet be fetched, likely due to lazy fetching.  Attach the object to a new session to force Hibernate to fetch the record. Taking a Java object already attached to a session and attempting to attach it to another session.  Did you forget to close the first session? _

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 10 One-to-Many / Many-to-One Associations  The Teacher table has a one-to-many association with the Class table.  The Class table has a many-to-one association with the Teacher table. _ IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel CodeTeacher_idSubject Data structures Java programming Compilers Software engineering Operating systems TeacherClass

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 11 public class Teacher {... private List targetEntity=Klass.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER) public List getKlasses() { return klasses; } public void setKlasses(List klasses) { this.klasses = klasses; }... }

Department of Computer Science Spring 2013: January 30 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; }... }

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 13 Klass.java public static void load() { Session session = HibernateContext.getSession(); Teacher rogers = Teacher.find("Rogers"); Teacher thompson = Teacher.find("Thompson"); Teacher lane = Teacher.find("Lane"); Teacher flynn = Teacher.find("Flynn"); Klass java = new Klass("Java programming"); java.setTeacher(rogers); Klass ds = new Klass("Data structures"); ds.setTeacher(thompson); Klass se = new Klass("Software engineering"); se.setTeacher(lane); Klass os = new Klass("Operating systems"); os.setTeacher(lane); Klass compilers = new Klass("Compilers"); compilers.setTeacher(flynn);... } Find each teacher. Create classes and assign teachers to them.

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 14 Klass.java public static void load() {... Transaction tx = session.beginTransaction(); { session.save(java); session.save(ds); session.save(se); session.save(os); session.save(compilers); } tx.commit(); session.close(); System.out.println("Class table loaded."); } Persist the objects. SchoolDemo4

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 15 Many-to-Many Association IdLastFirst 1001DoeJohn 1005NovakTim 1009KleinLeslie 1014JaneMary 1021SmithKim CodeTeacher_idSubject Data structures Java programming Compilers Software engineering Operating systems Student_idClass_code Student Class Student_Class Class Student Student-Class

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 16 public class Student {... private List klasses = new public List getKlasses() { return klasses; } public void setKlasses(List klasses) { this.klasses = klasses; }... } Hibernate will automatically create and populate the Student_Class join table. Student_idClass_code Student_Class

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak public class Klass {... private List students = new public List getStudents() { return students; } public void setStudents(List students) { this.students = students; }... } Student_idClass_code Student_Class

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 18 Klass.java public static void load() {... Student doe = Student.find("Doe"); Student jane = Student.find("Jane"); Student novak = Student.find("Novak"); Student smith = Student.find("Smith"); java.getStudents().add(smith); java.getStudents().add(doe); ds.getStudents().add(doe); ds.getStudents().add(novak); se.getStudents().add(doe); os.getStudents().add(novak); os.getStudents().add(smith); compilers.getStudents().add(smith); compilers.getStudents().add(jane);... } SchoolDemo5 Find each student. Assign students to classes.

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 19 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  First four teams to send me an . Points can be added to your project score.

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 20 Overcoming the Paradigm Mismatch  What is a major feature of the object-oriented data model that is not in the relational data model? inheritance  How can we model an inheritance hierarchy with relational database tables? Hibernate uses three strategies:  one table per class or subclass of the inheritance hierarchy  joined tables across the inheritance hierarchy  a single table per inheritance hierarchy _

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 21 Example Inheritance Hierarchy firstName : String lastName : String Person sjsuId : String SjsuPerson gpa : float SjsuStudent

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 22 public class Person { private long id; private String firstName; private String lastName; public @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; }... } firstName : String lastName : String Person sjsuId : String SjsuPerson gpa : float SjsuStudent

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 23 public class SjsuPerson extends Person { private String sjsuId; public SjsuPerson() public String getSjsuId() { return sjsuId; } public void setSjsuId(String id) { this.sjsuId = id; } } firstName : String lastName : String Person sjsuId : String SjsuPerson gpa : float SjsuStudent

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 24 public class SjsuStudent extends SjsuPerson { private float gpa; public SjsuStudent() public float getGpa() { return gpa; } public void setGpa(float gpa) { this.gpa = gpa; } } firstName : String lastName : String Person sjsuId : String SjsuPerson gpa : float SjsuStudent

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 25 InheritanceDemo.java public class InheritanceDemo { public static void main(String[] args) { Class klasses[] = {Person.class, SjsuPerson.class, SjsuStudent.class}; HibernateContext.addClasses(klasses); HibernateContext.createSchema();... } }

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 26 InheritanceDemo.java public class InheritanceDemo { public static void main(String[] args) {... Person person = new Person(); person.setFirstName("John"); person.setLastName("Doe"); SjsuPerson sjsuPerson = new SjsuPerson(); sjsuPerson.setFirstName("Mary"); sjsuPerson.setLastName("Jane"); sjsuPerson.setSjsuId(" "); SjsuStudent sjsuStudent = new SjsuStudent(); sjsuStudent.setFirstName("Leslie"); sjsuStudent.setLastName("Smith"); sjsuStudent.setSjsuId(" "); sjsuStudent.setGpa(3.71f);... } } firstName : String lastName : String Person sjsuId : String SjsuPerson gpa : float SjsuStudent

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 27 InheritanceDemo.java public class InheritanceDemo { public static void main(String[] args) {... Session session = HibernateContext.getSession(); Transaction tx = session.beginTransaction(); { session.save(person); session.save(sjsuPerson); session.save(sjsuStudent); } tx.commit(); session.close(); } }

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 28 Inheritance Strategy: One Table public class Person { private long id; private String firstName; private String lastName; public = public long getId() { return id; } public void setId(long id) { this.id = id; }... } InheritanceDemo1 Top of the inheritance hierarchy

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 29 Inheritance Strategy: One Table per Class  Disadvantages of one table per class strategy: Duplicated fields in the subclass tables. Changes to a superclass field value will require multiple table updates.

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 30 Inheritance Strategy: public class Person { private long id; private String firstName; private String lastName; public @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; }... } InheritanceDemo2

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 31 Inheritance Strategy: Joined Tables  Disadvantages of the joined table strategy: Table joins are inefficient. Poor performance with deep class hierarchies.

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 32 Inheritance Strategy: public class Person { private long id; private String firstName; private String lastName; public @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; }... } InheritanceDemo3

Department of Computer Science Spring 2013: January 30 CS 157B: Database Management Systems II © R. Mak 33 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.  You should not need to download and install Hibernate. NetBeans: Hibernate is built in. Eclipse: Use my zipped jar files. Command line: Put the jar files in your class path.  Why are you using the command line instead of an IDE? _