Download presentation
Presentation is loading. Please wait.
Published byRandell Smith Modified over 9 years ago
1
CS 157B: Database Management Systems II February 6 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak
2
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
3
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
4
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" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">...
5
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">...
6
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.
7
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()
8
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.
9
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");
10
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 email addresses end in.com MatchMode ANYWHERE START END EXACT Student prototype = new Student(); prototype.setEmailAddress(".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();
11
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(); http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/criterion/Restrictions.html
12
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("emailAddress"); Student prototype = new Student(); prototype.setEmailAddress(".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();
13
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();
14
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 9087008Data structures 9267003Java programming 9317051Compilers 9517012Software engineering 9747012Operating systems TeacherClass
15
Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 15 Klass.java @Entity @Table(name="Class") public class Klass {... private Teacher teacher;... @ManyToOne @JoinColumn(name="teacher_id") public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; }... } What classes are taught by John Lane?
16
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 9087008Data structures 9267003Java programming 9317051Compilers 9517012Software engineering 9747012Operating 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();
17
Department of Computer Science Spring 2013: February 6 CS 157B: Database Management Systems II © R. Mak 17 Student.java @Entity public class Student {... private List klasses = new ArrayList ();... @ManyToMany @JoinTable(name="Student_Class", joinColumns={@JoinColumn(name="student_id")}, inverseJoinColumns={@JoinColumn(name="class_code")}) public List getKlasses() { return klasses; } public void setKlasses(List klasses) { this.klasses = klasses; }... } Who are the students taught by John Lane?
18
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?
19
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. _
20
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; }
21
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();
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.