1 / 61

Object-Oriented Persistence Techniques Using OSS

Object-Oriented Persistence Techniques Using OSS. By: Josh Schendel. Overview. Understanding the Problem Database Refresher Object-Oriented Persistence: In .NET In Java (Hibernate). Understanding the Problem. “Software exists to solve a problem!”

keziah
Download Presentation

Object-Oriented Persistence Techniques Using OSS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Persistence Techniques Using OSS By: Josh Schendel

  2. Overview • Understanding the Problem • Database Refresher • Object-Oriented Persistence: • In .NET • In Java (Hibernate)

  3. Understanding the Problem “Software exists to solve a problem!” ~Every SE professor at this school Image from plg.uwaterloo.ca/ ~migod/fun/

  4. The Good Old Days • In the early days of programming: • Hardware presented limitations • Fewer clock cycles to solve the problem • Make best use of RAM and other resources • Efficiency was critical! • Programming teams were smaller • Many programs written by a single person • Maintenance generally consisted of bug-fixes Image from www.upss.com/menus.htm

  5. Software Evolved… • All these GOTO’s are hard to follow! • Structural programming presents a more understandable flow of logic • This main() is too long! • Functional programming allows code to be split up into smaller parts • I want to reuse parts of this program! • Object-oriented programming abstracts data and methods into highly reusable data structures

  6. Transitioning to OO:Why did it happen? • Maintenance is a growing concern • Project groups are getting larger • Code not always maintained by original author • Integrated, robust software in demand • Efficiency is good, but… • Current hardware alleviates many efficiency concerns • “Tricks” made in the interest of increased efficiency are often unintelligible and hinder maintainability

  7. The Point • The evolution of software has stressed maintainability over efficiency by employing objects • Not all attributes simple, can contain other objects • Polymorphism • The fastest programs are NOT object-oriented!

  8. The Point • The evolution of relational database software has stressed parallelization and scalability over object-oriented support • Transactional-based processing to allow multiprocessing without corruption of data • Many database packages charge based on multiprocessor utilizations • Can only store SIMPLE data types!! • Strings • Dates • Integers • …

  9. The Problem • Object-oriented programming solutions • Efficiency-based database solutions How can we persist our complex, polymorphic objects in these relational database systems in a maintainable, object-oriented fashion?

  10. Overview • Understanding the Problem • Database Refresher • Object-Oriented Persistence: • In .NET • In Java (Hibernate)

  11. Umm… What’s Persistence? • 3 types of data in an application • Transient • Persistent • Detached Information from http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html

  12. Transient Data • Does not need to be “saved” or retained after program exits • Does not contribute significant amount of state to the object • Often temporary or derived information, and often local to a function

  13. Persistent Data • Needs to be “saved” or retained after program exits • Contributes a significant amount of state to the object • Often all private class variable for a data object contain persistent data • Persistent data used to reconstruct the saved object after it has been saved

  14. Detached Data • Data that was loaded from a database but has since been changed or copied • “Out of synch” with the database • Can create undesirable states

  15. K, What’s a Relational Database? • “A relational database is a collection of data items organized as a set of formally-described tables from which data can be accessed or reassembled in many different ways without having to reorganize the database tables.”~http://searchoracle.techtarget.com/sDefinition/0,,sid41_gci212885,00.html

  16. A Database Looks Like…

  17. Relational Database Structure • Server: A running instance of the database software. Each server has multiple • Databases: Contain a set of isolated stored procedures, functions, and multiple • Tables: (next page)

  18. Relational Database Structure • Table: An entity that stores a set of data. • Columns: Each stores a single attribute, or field • Rows:Each row is a “record”, or an instance of data in its corresponding table

  19. Tables…

  20. Normalization • Not all information about a system can be persisted in a single table • Not all information about a system is unrelated • Information is normalized across several tables • Related information is linked via keys

  21. Relationships… Image from www.koffice.org/kexi/

  22. Database Terms • Connection String: A string of characters that is used in establishing a connection to a database • SQL: “Structured Query Language”, a protocol used to specify information desired from a database • Join: A means of combining the contents of records from different tables based on certain criteria • Key: A field or set of fields on a table for which the table is constrained in such a way that no two records can coexist in the table with the same values Terms from Hibernate in Action

  23. Database Terms • Record: One row of a table or resultset. • Query: An instance of a SQL statement that has been sent to a database to be performed • Resultset: The results of a query. All desired fields of all records that fit the criteria in the query are returned • Serialization: The conversion of an object into a meaningfully formatted string for which the object could use to reload its state at the time of conversion Terms from Hibernate in Action

  24. Overview • Understanding the Problem • Database Refresher • Object-Oriented Persistence: • In .NET • In Java (Hibernate)

  25. 3-Tiered Development • Presentation Layer • GUI • Business Logic Layer • “Policy” layer • Monkeys! • Data/Services Layer • Database interactions *Focus is on making Data Layer as object-oriented as the rest of the system Picture and terms from http://www.officewizard.com/books/clientserver/ClientServerComputing.htm

  26. Challenges of an OO Data Layer • Normalization • Tables are split up to reduce redundant data • Challenges developer’s capacity to treat each table as an object • Composition • Objects contain other objects • Contained objects must be stored in separate table • Inheritance • Store inherited objects in • Separate table? • Same table, separate table for additional attributes? • Same table, make table include attributes for all descendents?

  27. Pre-.NET Database Interfacing • ADO • “ActiveX Data Objects” • Supplied with VB6 for classic ASP applications • Set of objects used in interfacing with a database • Link to database remained open while iterating through the returned data • Implemented using DOM Terms from http://www.vbwm.com/articles/2002/tcole/adonet1/

  28. .NET Database Interfacing • ADO.NET heavily revamps traditional ADO • Disconnected • On a transaction a connection is opened, data is retrieved, and connection is automatically closed • Saves server and database resources! • Implemented using XML • VERY easy for ADO.NET objects to read XML or to serialize contents into XML • Doesn’t assume “one size fits all” • DataReader • DataSet Terms from http://www.vbwm.com/articles/2002/tcole/adonet1/

  29. .NET Database Interfacing • DataReader • “Lightweight and quick” • Should be used for “quick and easy” database queries • Able to quickly and efficiently iterate over a resultset • Read-only • Often used to bind datagrids Terms from http://www.vbwm.com/articles/2002/tcole/adonet1/

  30. .NET Database Interfacing • DataSet • “Heavyweight but powerful” • Each query places the resultset in a “table” in the DataSet • Can join tables within the DataSet • Can “query” the DataSet directly to filter records • Quick and easy conversion to and from XML • Great for XML parsing and persistence! Terms from http://www.vbwm.com/articles/2002/tcole/adonet1/

  31. .NET Database Interfacing • DataAdapter • Mediates connection between DataReader/DataSet and the database • Stores important database information • Connection string • SQL query to be sent • Name of stored procedure (to be discussed) • Parameters Terms from http://www.vbwm.com/articles/2002/tcole/adonet1/

  32. Code! Sub LoadCustomers() Dim myConnection As New OleDb.OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program” _ “Files\Microsoft Visual”) Dim myDataAdapter As New OleDb.OleDbDataAdapter( _ "Select * from Customers", myConnection) Dim myDataSet As New DataSet() ' Fill DataSet with table, call it 'Customers' myDataAdapter.Fill(myDataSet, "Customers")   ' Display first CompanyName field of first row in Customers table MessageBox.Show(myDataSet.Tables("Customers").Rows(0)("CompanyName")) End Sub Code from from http://www.developer.com/net/vb/article.php/10926_1540311_7

  33. But Josh Doesn’t Like It Sub LoadCustomers() Dim myConnection As New OleDb.OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program” _ “Files\Microsoft Visual”) • Connection String • Hard coded • Connection Strings are volatile • Change when switch from development to production databases • Make private constant for the class? • Not good enough – Connection strings are generally constant throughout a project • Will end up redefining the same constant in every class • Solution: Project-wide constant Code from from http://www.developer.com/net/vb/article.php/10926_1540311_7

  34. But Josh Doesn’t Like It Sub LoadCustomers() Dim myConnection As New OleDb.OleDbConnection(CONSTANTS.CONN_STRING) Dim myDataAdapter As New OleDb.OleDbDataAdapter( _ "Select * from Customers", myConnection) Dim myDataSet As New DataSet() ' Fill DataSet with table, call it 'Customers' myDataAdapter.Fill(myDataSet, "Customers")   ' Display first CompanyName field of first row in Customers table MessageBox.Show(myDataSet.Tables("Customers").Rows(0)("CompanyName")) End Sub • Replaced hard-coded connection string Code from from http://www.developer.com/net/vb/article.php/10926_1540311_7

  35. But Josh Doesn’t Like It Sub LoadCustomers() Dim myConnection As New OleDb.OleDbConnection(CONSTANTS.CONN_STRING) Dim myDataAdapter As New OleDb.OleDbDataAdapter( _ "Select * from Customers", myConnection) • Query mixed in with the code! • Modern databases offer stored procedures to aid in partitioning the Data Layer • Stored procedures offer many advantages over formulating a query in-code Code from from http://www.developer.com/net/vb/article.php/10926_1540311_7

  36. Stored Procedures • Set of pre-written SQL statements that reside in the database • Code references the stored procedure instead of building the SQL statement • Advantages • Compilation • Maintainability Terms from http://www.vbwm.com/articles/2002/tcole/adonet1/

  37. Stored Procedures • Compilation • “Compilation” of stored procedures does NOT produce byte code! • Syntax checking done when saving the stored procedure • You will not be allowed to save a stored procedure that does not compile • Catch query errors at “compile” time rather than run time! • Bonus! Compilation allows speeds up queries

  38. Stored Procedures • Maintainability • Stored procedures can be reused throughout an application • If the database changes, only need to update each affected query once in the stored procedure • Readability enhanced • Syntax highlighting • Don’t need to escape as many characters since the procedure does not need to be encased in a string

  39. Stored Procedures

  40. But Josh Doesn’t Like It Sub LoadCustomers() Dim myConnection As New OleDb.OleDbConnection(CONSTANTS.CONN_STRING) Dim myDataAdapter As New OleDb.OleDbDataAdapter( _ “GET_CUSTOMERS” myConnection) myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure; Dim myDataSet As New DataSet() ' Fill DataSet with table, call it 'Customers' myDataAdapter.Fill(myDataSet, "Customers")   ' Display first CompanyName field of first row in Customers table MessageBox.Show(myDataSet.Tables("Customers").Rows(0)("CompanyName")) End Sub • Replaced SQL with stored procedure Code from from http://www.developer.com/net/vb/article.php/10926_1540311_7

  41. But Josh Doesn’t Like It Dim myConnection As New OleDb.OleDbConnection(CONSTANTS.CONN_STRING) Dim myDataAdapter As New OleDb.OleDbDataAdapter( _ “GET_CUSTOMERS” myConnection) myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure; Dim myDataSet As New DataSet() ' Fill DataSet with table, call it 'Customers' myDataAdapter.Fill(myDataSet, "Customers")   • This code is reusable and non-OO • Developers care about objects, not records in a DataSet! • Instead of repeating the code every time we want the customers in the database, let’s make a function that returns the resultset as objects of the expected type Code from from http://www.developer.com/net/vb/article.php/10926_1540311_7

  42. But Josh Doesn’t Like It Function getCustomer(ByVal id As Integer) As Customer Dim myConnection As New OleDb.OleDbConnection(CONSTANTS.CONN_STRING) Dim myDataAdapter As New OleDb.OleDbDataAdapter( _ “GET_CUSTOMER” myConnection) myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure; myDataAdapter.Parameters.Add(“@id”, id) Dim myDataSet As New DataSet() ' Fill DataSet with table, call it 'Customers' myDataAdapter.Fill(myDataSet, "Customers")   String CompanyName = myDataSet.Tables(“Customers”) _ .Rows(0).Item(“CompanyName”) … Customer customer = new Customer (CompanyName, …) Return customer End Function • This code is reusable and non-OO • Developers care about objects, not records in a DataSet! • Instead of repeating the code every time we want the customers in the database, let’s make a function that returns the resultset as objects of the expected type Code from from http://www.developer.com/net/vb/article.php/10926_1540311_7

  43. Object-Oriented Persistence in .NET • .NET offers powerful ADO.NET objects • By defining constants, employing stored procedures, and isolating Data Layer operations into object-oriented functions, we can emulate an object-oriented database from a relational one

  44. Overview • Understanding the Problem • Database Refresher • Object-Oriented Persistence: • In .NET • In Java (Hibernate)

  45. Java Persistence • Wouldn’t it be great if all you had to do to save an object to the database was say session.save(customer);

  46. Introducing Hibernate! • Hibernate • Open-source framework • Sits between “Policy” and “Data” layer • Transforms the frontend of a relational database into an object-oriented database Information from Professional Hibernate

  47. How Does it Work? • Installation • JAR file with classpath • Supporting files • hibernate.properties • hibernate.cfg.xml Information from Professional Hibernate

  48. How Does it Work? • hibernate.properties • Non-XML format • Discrepancies between this and hibernate.cfg.xml lose out in favor of hibernate.cfg.xml • Will likely be depracated in the near future Information from Professional Hibernate

  49. How Does it Work? • hibernate.cfg.xml • XML format • Contains all the information needed to run Hibernate • This includes: • Connection String / JDBC driver information • Need only be in ONE place in the project! • Database dialect and other settings • Class-to-table mappings or locations to class-to-table mapping files Information from Professional Hibernate

  50. How Does it Work? • Database dialect • Dialect: A form of SQL that is constrained or contains proprietary keywords or formats that are specific to certain databases • Since Hibernate takes care of much of the SQL, it must know correct dialect for the database Information from Professional Hibernate

More Related